Not Outputing Second Integer From A "scanf" To A "printf"
Author |
Message |
ClayWall
|
Posted: Tue Dec 16, 2008 7:46 pm Post subject: Not Outputing Second Integer From A "scanf" To A "printf" |
|
|
Cannot figure this out, it's straight from a book claiming you can enter a number then a space then a second number press enter and it will output the 2 numbers, but it outputs the first number then then the max integer allowed, I keep looking it over, I can't find anything mistyped.
c: |
#include <stdio.h>
int main ()
{
char character; int a,b;
printf("Enter any one keyboard character: ");
scanf ( "%c", &character );
printf("Enter 2 integers seperated by a space: ");
scanf ( "%d, %d", &a, &b );
printf("The letter entered was %c\n", character );
printf ("Integers entered were %d and %d\n", a, b );
return 0;
}
|
Also, when I compile my code it says "warning: no newline at end of file", but this isn't as big a problem as it still compiles and runs.
Mod Edit: Remember to use syntax tags! Thanks code: | [syntax="c"]Code Here[/syntax] |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Vertico
|
Posted: Tue Dec 16, 2008 9:55 pm Post subject: Re: Not Outputing Second Integer From A "scanf" To A "printf" |
|
|
c: | #include <stdio.h>
int main ()
{
char character;
int a,b;
printf("Enter any one keyboard character: ");
scanf ( "%c", &character );
printf("Enter 2 integers seperated by a space: ");
scanf ( "%d %d", &a, &b );
printf("The letter entered was %c\n", character );
printf ("Integers entered were %d and %d\n", a, b );
return 0;
} |
I took out the , between the two inputs and it worked fine.
c: | scanf( "%d %d", &a, &b ); |
|
|
|
|
|
|
btiffin
|
Posted: Wed Dec 17, 2008 2:46 am Post subject: Re: Not Outputing Second Integer From A "scanf" To A "printf" |
|
|
Yep, or enter the numbers with a comma and zero or more whitespaces and the original will work.
scanf is a fairly powerful but highly fragile function.
Get used to defensive programming;
c: |
#include <stdio.h>
int main ()
{
char character;
int a,b, res;
printf("Enter any one keyboard character: ");
res = scanf ( "%c", &character );
printf("The letter entered was %c with %d\n", character, res );
printf("Enter 2 integers seperated by a space: ");
res = scanf ( "%d, %d", &a, &b );
switch(res ) {
case 1:
printf("Only one integer: %d\n", a );
break;
case 2:
printf("Two integers: %d and %d\n", a, b );
break;
default:
printf("%s", "other than numbers and a comma typed\n");
break;
}
return 0;
}
|
And because of the "%d, %d" in the scanf (space after the comma), that means match a number, a comma, zero or whitespaces, and a number. So you can hit enter till your heart's content if nothing else is entered as scanf attempts to chew through the whitespaces looking for a match.
The %c will feed extra characters to the next scanf so x123,123 entered on the first line will fulfill both scanf's etc... etc... Again, scanf is very fragile.
Cheers |
|
|
|
|
|
ClayWall
|
Posted: Wed Dec 17, 2008 11:51 am Post subject: Re: Not Outputing Second Integer From A "scanf" To A "printf" |
|
|
Thank you, I just had another look at the book and sure enough there was no comma between the two inputs. I do not look forward to future bug fixes with my programs, as I can see already how difficult it will be ... |
|
|
|
|
|
gianni
|
Posted: Wed Dec 17, 2008 3:19 pm Post subject: RE:Not Outputing Second Integer From A "scanf" To A "printf" |
|
|
You may also want to look into input validation as C can be quite nasty if users don't enter what you're expecting. |
|
|
|
|
|
ClayWall
|
Posted: Wed Dec 17, 2008 10:18 pm Post subject: RE:Not Outputing Second Integer From A "scanf" To A "printf" |
|
|
Sure will, cause I know it's hard to find the faults test running yourself. You know how the program is to run, but getting someone to wait while you fix every bug they find can be a chore! |
|
|
|
|
|
wtd
|
Posted: Wed Dec 17, 2008 10:22 pm Post subject: RE:Not Outputing Second Integer From A "scanf" To A "printf" |
|
|
Write test scripts that simulate weird weird input. |
|
|
|
|
|
|
|