why input isn't being taken as i wanted?
In this program i want to get a input where will be a string with some int.if no values were not given then, the program will just show the total amount.
But here, in the 1st case if i use a string with number,then there is 2 output.
Example input:
4
donate 100
report
donate 500
Example output:
0
100
100
0
But notice here i got chance of 3 times to input string,but output is 4 time.
So, can anyone tell me why this program acting weird?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=0;
char input1[20];
scanf("%d",&T);
while(T--)
{
scanf(" %s",input1);
val=-1;
val=atoi(input1);
total+=val;
printf("nInput="%s" val="%d"n", input1, val);
if(val != -1)
printf("%dn",total);
}
}
c string input
|
show 7 more comments
In this program i want to get a input where will be a string with some int.if no values were not given then, the program will just show the total amount.
But here, in the 1st case if i use a string with number,then there is 2 output.
Example input:
4
donate 100
report
donate 500
Example output:
0
100
100
0
But notice here i got chance of 3 times to input string,but output is 4 time.
So, can anyone tell me why this program acting weird?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=0;
char input1[20];
scanf("%d",&T);
while(T--)
{
scanf(" %s",input1);
val=-1;
val=atoi(input1);
total+=val;
printf("nInput="%s" val="%d"n", input1, val);
if(val != -1)
printf("%dn",total);
}
}
c string input
2
scanf(" %s", input1);reads till the next whitespace. firstscanfwill readdonateand the second100.
– Osiris
Nov 21 '18 at 16:16
Why not just add a line of code to log what's ininput1and what's invalright before theifand figure it out yourself?
– David Schwartz
Nov 21 '18 at 16:19
1
@mahinhossen If you want to read a string until new line you should usescanf(" %[^n]s",input1);
– Osiris
Nov 21 '18 at 16:30
1
@mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.
– David Schwartz
Nov 21 '18 at 16:40
1
@mahinhossen Yourscanfis fine. You just need to add code to ignore the input if it's not a number. Your choice ofatoiwas not a good one, since it has no error detection.
– David Schwartz
Nov 21 '18 at 17:22
|
show 7 more comments
In this program i want to get a input where will be a string with some int.if no values were not given then, the program will just show the total amount.
But here, in the 1st case if i use a string with number,then there is 2 output.
Example input:
4
donate 100
report
donate 500
Example output:
0
100
100
0
But notice here i got chance of 3 times to input string,but output is 4 time.
So, can anyone tell me why this program acting weird?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=0;
char input1[20];
scanf("%d",&T);
while(T--)
{
scanf(" %s",input1);
val=-1;
val=atoi(input1);
total+=val;
printf("nInput="%s" val="%d"n", input1, val);
if(val != -1)
printf("%dn",total);
}
}
c string input
In this program i want to get a input where will be a string with some int.if no values were not given then, the program will just show the total amount.
But here, in the 1st case if i use a string with number,then there is 2 output.
Example input:
4
donate 100
report
donate 500
Example output:
0
100
100
0
But notice here i got chance of 3 times to input string,but output is 4 time.
So, can anyone tell me why this program acting weird?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=0;
char input1[20];
scanf("%d",&T);
while(T--)
{
scanf(" %s",input1);
val=-1;
val=atoi(input1);
total+=val;
printf("nInput="%s" val="%d"n", input1, val);
if(val != -1)
printf("%dn",total);
}
}
c string input
c string input
edited Dec 5 '18 at 14:21
gsamaras
51.9k24107191
51.9k24107191
asked Nov 21 '18 at 16:12
mahin hossenmahin hossen
958
958
2
scanf(" %s", input1);reads till the next whitespace. firstscanfwill readdonateand the second100.
– Osiris
Nov 21 '18 at 16:16
Why not just add a line of code to log what's ininput1and what's invalright before theifand figure it out yourself?
– David Schwartz
Nov 21 '18 at 16:19
1
@mahinhossen If you want to read a string until new line you should usescanf(" %[^n]s",input1);
– Osiris
Nov 21 '18 at 16:30
1
@mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.
– David Schwartz
Nov 21 '18 at 16:40
1
@mahinhossen Yourscanfis fine. You just need to add code to ignore the input if it's not a number. Your choice ofatoiwas not a good one, since it has no error detection.
– David Schwartz
Nov 21 '18 at 17:22
|
show 7 more comments
2
scanf(" %s", input1);reads till the next whitespace. firstscanfwill readdonateand the second100.
– Osiris
Nov 21 '18 at 16:16
Why not just add a line of code to log what's ininput1and what's invalright before theifand figure it out yourself?
– David Schwartz
Nov 21 '18 at 16:19
1
@mahinhossen If you want to read a string until new line you should usescanf(" %[^n]s",input1);
– Osiris
Nov 21 '18 at 16:30
1
@mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.
– David Schwartz
Nov 21 '18 at 16:40
1
@mahinhossen Yourscanfis fine. You just need to add code to ignore the input if it's not a number. Your choice ofatoiwas not a good one, since it has no error detection.
– David Schwartz
Nov 21 '18 at 17:22
2
2
scanf(" %s", input1); reads till the next whitespace. first scanf will read donate and the second 100.– Osiris
Nov 21 '18 at 16:16
scanf(" %s", input1); reads till the next whitespace. first scanf will read donate and the second 100.– Osiris
Nov 21 '18 at 16:16
Why not just add a line of code to log what's in
input1 and what's in val right before the if and figure it out yourself?– David Schwartz
Nov 21 '18 at 16:19
Why not just add a line of code to log what's in
input1 and what's in val right before the if and figure it out yourself?– David Schwartz
Nov 21 '18 at 16:19
1
1
@mahinhossen If you want to read a string until new line you should use
scanf(" %[^n]s",input1);– Osiris
Nov 21 '18 at 16:30
@mahinhossen If you want to read a string until new line you should use
scanf(" %[^n]s",input1);– Osiris
Nov 21 '18 at 16:30
1
1
@mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.
– David Schwartz
Nov 21 '18 at 16:40
@mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.
– David Schwartz
Nov 21 '18 at 16:40
1
1
@mahinhossen Your
scanf is fine. You just need to add code to ignore the input if it's not a number. Your choice of atoi was not a good one, since it has no error detection.– David Schwartz
Nov 21 '18 at 17:22
@mahinhossen Your
scanf is fine. You just need to add code to ignore the input if it's not a number. Your choice of atoi was not a good one, since it has no error detection.– David Schwartz
Nov 21 '18 at 17:22
|
show 7 more comments
1 Answer
1
active
oldest
votes
Simply add logging and the answer will be obvious:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=-1;
char input1[20];
scanf("%d",&T);
while(T--)
{
scanf(" %s",input1);
val=-1;
val=atoi(input1);
total+=val;
printf("nInput='%s' val='%d'n", input1, val);
if(val != -1)
printf("%dn",total);
}
}
Then run it and give it your input:
4
donate 100
Input='donate' val='0'
0
Input='100' val='100'
100
report
Input='report' val='0'
100
donate 500
Input='donate' val='0'
100
As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416201%2fwhy-input-isnt-being-taken-as-i-wanted%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simply add logging and the answer will be obvious:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=-1;
char input1[20];
scanf("%d",&T);
while(T--)
{
scanf(" %s",input1);
val=-1;
val=atoi(input1);
total+=val;
printf("nInput='%s' val='%d'n", input1, val);
if(val != -1)
printf("%dn",total);
}
}
Then run it and give it your input:
4
donate 100
Input='donate' val='0'
0
Input='100' val='100'
100
report
Input='report' val='0'
100
donate 500
Input='donate' val='0'
100
As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.
add a comment |
Simply add logging and the answer will be obvious:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=-1;
char input1[20];
scanf("%d",&T);
while(T--)
{
scanf(" %s",input1);
val=-1;
val=atoi(input1);
total+=val;
printf("nInput='%s' val='%d'n", input1, val);
if(val != -1)
printf("%dn",total);
}
}
Then run it and give it your input:
4
donate 100
Input='donate' val='0'
0
Input='100' val='100'
100
report
Input='report' val='0'
100
donate 500
Input='donate' val='0'
100
As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.
add a comment |
Simply add logging and the answer will be obvious:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=-1;
char input1[20];
scanf("%d",&T);
while(T--)
{
scanf(" %s",input1);
val=-1;
val=atoi(input1);
total+=val;
printf("nInput='%s' val='%d'n", input1, val);
if(val != -1)
printf("%dn",total);
}
}
Then run it and give it your input:
4
donate 100
Input='donate' val='0'
0
Input='100' val='100'
100
report
Input='report' val='0'
100
donate 500
Input='donate' val='0'
100
As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.
Simply add logging and the answer will be obvious:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=-1;
char input1[20];
scanf("%d",&T);
while(T--)
{
scanf(" %s",input1);
val=-1;
val=atoi(input1);
total+=val;
printf("nInput='%s' val='%d'n", input1, val);
if(val != -1)
printf("%dn",total);
}
}
Then run it and give it your input:
4
donate 100
Input='donate' val='0'
0
Input='100' val='100'
100
report
Input='report' val='0'
100
donate 500
Input='donate' val='0'
100
As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.
answered Nov 21 '18 at 16:22
David SchwartzDavid Schwartz
138k14145230
138k14145230
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416201%2fwhy-input-isnt-being-taken-as-i-wanted%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
scanf(" %s", input1);reads till the next whitespace. firstscanfwill readdonateand the second100.– Osiris
Nov 21 '18 at 16:16
Why not just add a line of code to log what's in
input1and what's invalright before theifand figure it out yourself?– David Schwartz
Nov 21 '18 at 16:19
1
@mahinhossen If you want to read a string until new line you should use
scanf(" %[^n]s",input1);– Osiris
Nov 21 '18 at 16:30
1
@mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.
– David Schwartz
Nov 21 '18 at 16:40
1
@mahinhossen Your
scanfis fine. You just need to add code to ignore the input if it's not a number. Your choice ofatoiwas not a good one, since it has no error detection.– David Schwartz
Nov 21 '18 at 17:22