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
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