448,837 Members | 1,637 Online
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

# Where is the mistake?

 P: n/a Hope someone can help me to overcome this problem. Case 1 is working perfectly working with one number entry. Case 2 is the problem when I tried with 2 numbers entry. Case 1. #include int main () { int a,i; for (i=1;i<=3;i++) { scanf ("%d",&a); printf ("%d\n",a); } return 0; } /* Result 5 5 15 15 234 234 Press any key to continue */ As the result shows, I enter 5 and computer prints 5, enter 15 and computer prints 15 , 234 and prints 234. No problem with this. The problem comes when I deal with 2 numbers, a and b. Case 2 #include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d,%d",&a,&b); printf ("%d,%d\n",a,b); } return 0; } /* Result 1 2 1,-858993460 2,-858993460 3 4 3,-858993460 Press any key to continue */ As you can see from the above result, when I enter 1 2, I get weird result. I suspect the computer is printing "space" and "enter key". How do I overcome this problem? Thanks Khoon. Dec 3 '05 #1
12 Replies

 P: n/a Tiny Tim wrote: *Hope someone can help me to overcome this problem.* *Case 1 is working perfectly working with one number entry.* *Case 2 is the problem when I tried with 2 numbers entry.* *Case 1. * [snip] *Case 2* *#include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d,%d",&a,&b); scanf ("%d %d",&a,&b); Bjørn [snip] Dec 3 '05 #2

 P: n/a Tiny Tim wrote: Hope someone can help me to overcome this problem. Case 1 is working perfectly working with one number entry. Case 2 is the problem when I tried with 2 numbers entry. Case 1. #include int main () { int a,i; for (i=1;i<=3;i++) { scanf ("%d",&a); printf ("%d\n",a); } return 0; } /* Result 5 5 15 15 234 234 Press any key to continue */ As the result shows, I enter 5 and computer prints 5, enter 15 and computer prints 15 , 234 and prints 234. No problem with this. The problem comes when I deal with 2 numbers, a and b. Case 2 #include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d,%d",&a,&b); scanf("%d%d", &a, &b); printf ("%d,%d\n",a,b); } return 0; } /* Result 1 2 1,-858993460 2,-858993460 3 4 3,-858993460 Press any key to continue */ As you can see from the above result, when I enter 1 2, I get weird result. I suspect the computer is printing "space" and "enter key". How do I overcome this problem? Thanks Khoon. Dec 3 '05 #3

 P: n/a Tiny Tim wrote: Hope someone can help me to overcome this problem. Case 1 is working perfectly working with one number entry. Case 2 is the problem when I tried with 2 numbers entry. Case 1. #include int main () { int a,i; for (i=1;i<=3;i++) { scanf ("%d",&a); printf ("%d\n",a); } return 0; } /* Result 5 5 15 15 234 234 Press any key to continue */ As the result shows, I enter 5 and computer prints 5, enter 15 and computer prints 15 , 234 and prints 234. No problem with this. The problem comes when I deal with 2 numbers, a and b. Case 2 #include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d,%d",&a,&b); printf ("%d,%d\n",a,b); } return 0; } /* Result 1 2 1,-858993460 2,-858993460 3 4 3,-858993460 Press any key to continue */ As you can see from the above result, when I enter 1 2, I get weird result. I suspect the computer is printing "space" and "enter key". How do I overcome this problem? Don't enter 1 2. That's not what you programmed. Instead, enter 1,2 (which is "%d,%d"). If instead you intend to enter 1 2 do "%d %d". Dec 3 '05 #4

 P: n/a On Sat, 3 Dec 2005 19:46:19 +0800, in comp.lang.c , "Tiny Tim" wrote: scanf ("%d,%d",&a,&b); remove the comma in the format string. Also, don't use scanf. Please read the FAQ for a discussion of why but in summary its a dangerous function thats tricky to safely use. You already found one problem. -- Mark McIntyre CLC FAQ CLC readme: ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =---- Dec 3 '05 #5

 P: n/a "Peter" wrote in message news:11**********************@g47g2000cwa.googlegr oups.com... Tiny Tim wrote: scanf ("%d,%d",&a,&b); scanf("%d%d", &a, &b); Thanks but Sorry, it does not work as suggested. I have removed the commas between %d. Please see result below. #include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d %d",&a,&b); scanf ("%d %d",&a,&b); printf ("%d %d\n",a,b); } return 0; } /* Result 1 2 3 4 3 4 5 6 7 8 7 8 9 10 11 12 11 12 Press any key to continue */ It works only on alternative entries, which is not correct. Regards, Khoon. Dec 4 '05 #6

 P: n/a "Tiny Tim" writes: "Peter" wrote in message news:11**********************@g47g2000cwa.googlegr oups.com... Tiny Tim wrote: scanf ("%d,%d",&a,&b); scanf("%d%d", &a, &b); Thanks but Sorry, it does not work as suggested. I have removed the commas between %d. Please see result below. #include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d %d",&a,&b); scanf ("%d %d",&a,&b); printf ("%d %d\n",a,b); } return 0; } /* Result 1 2 3 4 3 4 5 6 7 8 7 8 9 10 11 12 11 12 Press any key to continue */ It works only on alternative entries, which is not correct. Yes, it is. Well, it may not be correct, but it's what you asked for. You have two calls to scanf. The first reads the values of a and b, and the second reads them again, clobbering whatever values you read the first time. BTW, where does the "Press any key to continue" message come from? -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Dec 4 '05 #7

 P: n/a Tiny Tim wrote: "Peter" wrote in message news:11**********************@g47g2000cwa.googlegr oups.com... Tiny Tim wrote: scanf ("%d,%d",&a,&b); scanf("%d%d", &a, &b); Thanks but Sorry, it does not work as suggested. I have removed the commas between %d. Please see result below. #include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d %d",&a,&b); scanf ("%d %d",&a,&b); printf ("%d %d\n",a,b); } return 0; } /* Result 1 2 3 4 3 4 5 6 7 8 7 8 9 10 11 12 11 12 Press any key to continue */ It works only on alternative entries, which is not correct. Think about what you're doing to your variables. The error here is your logic, not the code. Try the following: #include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d %d",&a,&b); printf ("%d %d\n",a,b); scanf ("%d %d",&a,&b); printf ("%d %d\n",a,b); } return 0; } Or better yet, the following: #include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d %d",&a,&b); printf ("%d %d\n",a,b); } return 0; } Dec 4 '05 #8

 P: n/a Tiny Tim a écrit : *#include int main () { int a,b,i; for (i=1;i<=3;i++) { scanf ("%d,%d",&a,&b); printf ("%d,%d\n",a,b); } return 0; } /* Result 1 2 Wrong. The expected separator is ','. You must type 1,2 Also, you should check that scanf() returns 2 and purge stdin if not... Better to use fgest() and sscanf()... -- A+ Emmanuel Delahaye Dec 4 '05 #9

 P: n/a > BTW, where does the "Press any key to continue" message come from? Dear Keith, If you use Visual C++ as the platform for writing C, this message will automatically come out at the end of every result. This is because the result comes out in DOS mode. Pressing any key will close the DOS window and return back to the Visual C++ format. Thanks Khoon. Dec 4 '05 #10

 P: n/a On Mon, 5 Dec 2005 00:29:16 +0800, in comp.lang.c , "Tiny Tim" wrote: keith wrote BTW, where does the "Press any key to continue" message come from?If you use Visual C++ as the platform for writing C, this message willautomatically come out at the end of every result. No it won't. I've used Visual C for years, and neither VC6 nor .net exhibits this behaviour - they both simply close the window when done.This is because the result comes out in DOS mode. Pressing any key willclose the DOS window and return back to the Visual C++ format. Only if you set some option in the VC or windows gui. Keith's point was that the programme as posted didn't seem to be the code as compiled. -- Mark McIntyre CLC FAQ CLC readme: ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =---- Dec 4 '05 #11

 P: n/a Mark McIntyre writes: On Mon, 5 Dec 2005 00:29:16 +0800, in comp.lang.c , "Tiny Tim" wrote:keith wrote BTW, where does the "Press any key to continue" message come from?If you use Visual C++ as the platform for writing C, this message willautomatically come out at the end of every result. No it won't. I've used Visual C for years, and neither VC6 nor .net exhibits this behaviour - they both simply close the window when done.This is because the result comes out in DOS mode. Pressing any key willclose the DOS window and return back to the Visual C++ format. Only if you set some option in the VC or windows gui. Keith's point was that the programme as posted didn't seem to be the code as compiled. No, that wasn't my point. According to Tiny Tim, the extra output comes from the environment, not from the program itself. It's like showing a shell prompt as part of the output on a Unix-like system. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Dec 4 '05 #12

 P: n/a On Sun, 04 Dec 2005 19:56:34 GMT, in comp.lang.c , Keith Thompson wrote: No, that wasn't my point. According to Tiny Tim, the extra outputcomes from the environment, not from the program itself. It's likeshowing a shell prompt as part of the output on a Unix-like system. In that case, I misunderstood you. Sorry. -- Mark McIntyre CLC FAQ CLC readme: ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =---- Dec 5 '05 #13

### This discussion thread is closed

Replies have been disabled for this discussion.