-
Loop control
I do not understand how to pause my display loop. I thought the second scanf would do the job, but it doesn't. Here is the code
Code:
int main ()
{
int op1; // user input operand1
int op2; // user input operand2
char operator; // user input operator
int result; // answer for calculation
int status; // open or close calculator program
for (status = 0 ;status !='q'; )
{
printf("\n\t\tCALCULATOR\n\n");
printf(" Make your selection by entering 2 numbers\n");
printf(" and an operator as shown in the form below\n\n");
printf("\ti1 + i2 i1 plus i2\n");
printf("\ti1 - i2 i1 minus i2\n");
printf("\ti1 * i2 i1 multiply times i2\n");
printf("\ti1 / i2 i1 divided by i2\n");
printf("\ti1 ^ i2 i1 to the power i2\n");
printf("\ti1 G i2 the greatest common divisor (gcd) of i1 and i2\n");
printf("\ti1 L i2 the least common multiple (lcm) of i1 and i2\n");
printf("\ti1 R i2 the square root of one quarter of the sum of i1 and i2\n");
printf("\ti1 Q i2 quit the calculator program\n\n");
printf(" The above menu lists available operations on the left and a brief explanation\n");
printf(" on the right. Use the format of the operations on the left to enter a formula\n");
printf(" \n");
printf("Type NUMBER OPERATOR NUMBER and press RETURN:\n");
scanf("%d %c %d", &op1, &operator, &op2);
switch (operator)
{
case '+': result = plus(op1,op2); break;
case '-': result = minus(op1,op2); break;
case '*': result = multi(op1,op2); break;
case '/': result = divd(op1,op2); break;
case '^': result = powr(op1,op2); break;
case 'g': result = gcd(op1,op2); break;
case 'l': result = lcm(op1,op2); break;
case 'r': result = qtrsumroot(op1,op2); break;
default: printf("Please re-enter your request\n"); break;
}
printf("your formula is %d %c %d. The answer is %d\n\n", op1, operator, op2, result);
printf("Press any key to reset calculator. Press Q to quit.\n");
scanf("%c". &status);
}
}
thanks for any insight.
Keith
-
Code:
scanf("%c". &status);
[list=1][*] , not . after "%c"[*]%c corresponds to character input but status is int[/list=1]
-
revised
thanks pinko,
I updated the type of status to char and revised the notation on the scanf line. There must be a logic error. I am thinking that the for(status='0'; status !='q'; ) line at the start of the function will be updated by the scanf line at the end of the function. The loop passes by the scanf. hmmmm....
-
clear input buffer
Salem,
Thank you, I did not realize that the input stream needs to be cleared. I very much appreciate you writing -- taking time to reply. This gives me the direction needed to understand what is going on. Of course I had heard that there are better ways than scanf and that scanf has limits, but I didnt' know exactly what that means nor how it would affect me in this instance.
Thanks again for your direction,
Keith
-
scanf FIXED
These changes made a vast improvment in the functionality and operability of the display screen that caused me a problem using scanf. My thanks to SALEM for assisting with specific code and explanations of the nature of the problem using scanf.
Code:
int main ()
{
char status; // open or close calculator program
char buff[100]; // user input workspace
for (status = 0 ;status !='q'; )
{
fgets( buff, sizeof(buff), stdin );
sscanf( buff, "%d %c %d", &op1, &operator, &op2);
switch (operator)
{
}
printf("Press ENTER to reset calculator. Press Q to quit.\n");
fgets( buff, sizeof(buff), stdin );
sscanf( buff, "%c", &status);
}
}
-
Both fgets and sscanf can fail, always check their return values.
-Prelude