Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probably a stupid scanf/getchar question

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello, everybody. I'm quite a newbie in C matters, so the question that bugs me does probably have an easy solution, which nevertheless is seemingly beyond me.

I've been experimenting with some stupid program that is supposed to do basic math operations, calculating the areas of geometrical figures on customer's choice. Anyway, it produces a bug, obviously based on a logical error which i've been unable to find. So, if you could take a look..

Consider this piece of code

blablabla..
...
int r,a //basic input for the calculations, doesnt matter
char code //a switch to choose the kind of the figure
char bug //cleans the getchar buffer
.....
void circle() //definition of squarearea function
...

void main()
{
begn:clrscr();
r=a=0;
code='\0';
printf("choose your figure...\n"); //...=multiple choice
code=scanf("%c",code);
switch(code)
{
case 'c': circle();break;
....
default: printf("Learn the alphabet. \n"); exit(0); //just something to exit with
}
printf("Press any button to quite, or 'p' to try again\n");
bug=getchar();code=getchar();
if (code!='p') exit(0);
goto begn;
}

-------
Okay, so thats what happens. First time the program is run, it asks the multiple choice question okay and accept the value "c", turning over to the circle function. After its completion, we're up to the "p"/no"p" choice. If no "p" is chosen it does quit alright. If I press "p", though, it returns to the initial label, prints the multiple choice question and immediately passes to the "deafult" case of the switch operator, without offering the possibility for input and then quits (thats in the default case). This happens no matter whether I use scanf or getchar to pick the value of code after the multiple choice printing. I toggled some breakpoints and thru the Watch feature discovered that "code" assumes the value "\n" RIGHT after the multiple choice is printed. Why doesnt this happen the first time the program is run, i cant understand. The whole problem is beyond me, anyway. Why does it assumes that value? Where does it take it from? And why doesnt it offer a choice at all?


Thanks,
Jorr
 
With this part of the code
code=scanf("%c",code);

the compiler first of all scans (reads) the choice into code, which in this case is wrong syntax anyway - you forgot the ampersand &, then you reassign code a different value by having code equal to the return value of scanf, which will be one.
scanf returns the number of arguments it reads from the buffer, so since it only reads in one char value then this is the value you reassign to code. if you had scanf("%c%d",arg1,arg2); and then the user enters a char followed by an int then code would now have the value 2, understand.

Therefore your syntax for scanf is wrong unless you want to test the value returned from scanf();
scanf("%c", &code); is what you are looking for, or better still
code = getchar();
Hoping to get certified..in C programming.
 
Oh, thats my bad. I've rewritten the piece wrongly here. Thats due to the different versions i tried. In fact, it's set the way you describe. That is:


void main()
{
begn:clrscr();
r=a=0;
code='\0';
printf("choose your figure...\n"); //...=multiple choice
scanf("%c",&code);
switch(code)
{
case 'c': circle();break;
....
default: printf("Learn the alphabet. \n"); exit(0); //just something to exit with
}
printf("Press any button to quite, or 'p' to try again\n");
bug=getchar();code=getchar();
if (code!='p') exit(0);
goto begn;
}

Thats where i get the problem. Other ideas?

Jorr
 
that is because of the way the input is fed into the program.

WHen you ask for choice, you (ie scanf) waits for new line to be pressed before seeing the input. Since you are reading a 'char' only, this newline is in the input buffer and that is what your next getchar (or even scanf) will see.

You can save "cpcpcpcpq" in a file and then feed that file as input to your program. You will notice that it works as you would expect it (offcourse i am assuming that you will remove the bug= line from the code before doing this).

So, if you want to read a char only from the terminal.. then you will have to handle the newline ..

Mohan
 
But why doesnt that happen the first time the scanf is run? The first time, it works alright. When it passes thru it again, it doesnt even ASK for input. It just assumes '\n'. I've tried to deal with it the way you suggest but it didnt work, and it wont work, since the choice, the possibility to input a value isnt offered at all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top