Problem with prints twice instead of once.

M

MNQ

Hello All

Im trying to relearn ANSI C. I have written some code but cannot understand
why it is not do what i think it should. It prints the error message and
the menu twice but i cant understand why. Can any one help me please?

naveed

/*
Last modified 5 February 2004
I dont know why after selecting an option it
displays 'Enter option' twice twice*/

#include<stdio.h>

int add(int numa, int numb)//Function to add two numbers
{
int sum=0;

sum=numa+numb;

return (sum);
}

int power(int num, int power)//Function to find x to the power n
{
int ans;

if (power<0) //Negative power
ans=-1;
else //Zero power becomes 1
ans=1;

while (power>0) //Positive powers
{
ans=num*ans;
power=power-1;
}

return (ans);
}

int main(void)
{
int num1=0, num2=0, answer=0, done=0;
while (!done)

{
char option=0;
printf("\n 1) Add two numbers.");
printf("\n 2) The power of a number.");
printf("\n 3) Exit.");
printf("\n Enter option: "); //User enters option from menu
scanf("%c",&option);

switch(option)
{ //Option 1 from menu adds two numbers
case '1':printf("\n Enter two integers: ");
scanf("%d %d", &num1, &num2);
answer = add(num1, num2);
printf("%d + %d = %d\n",num1,num2,answer);
break;
//Option 2 from menu finds X to the power of N
case '2':printf("\n Enter the number and power: ");
scanf("%d %d", &num1, &num2);
answer = power(num1, num2);
printf("%d to the power of %d is
%d\n",num1,num2,answer);
break;
//Option 3 exits the program
case '3':done=1;
break;
//An option other than 1,2 or 3
default: printf("Error.\n");
break;
}
}

return 0;
}
 
V

Vijay Kumar R Zanvar

[..]
int main(void)
{
int num1=0, num2=0, answer=0, done=0;
while (!done)

{
char option=0;
printf("\n 1) Add two numbers.");
printf("\n 2) The power of a number.");
printf("\n 3) Exit.");
printf("\n Enter option: "); //User enters option from menu
scanf("%c",&option);

Somehow, this problem solves if we replace the above scanf()
with:

scanf ( "% c", &option );

But, this still remains unexplained.
 
J

Jeremy Yallop

Vijay said:
scanf ( "% c", &option );

This invokes undefined behaviour. You'll even get a warning from some
compilers with the right options.

Jeremy.
 
M

MNQ

This does not fix the problem but makes it worse! It prints the menu many
for ever and does not stop.


Vijay Kumar R Zanvar said:
[..]
int main(void)
{
int num1=0, num2=0, answer=0, done=0;
while (!done)

{
char option=0;
printf("\n 1) Add two numbers.");
printf("\n 2) The power of a number.");
printf("\n 3) Exit.");
printf("\n Enter option: "); //User enters option from menu
scanf("%c",&option);

Somehow, this problem solves if we replace the above scanf()
with:

scanf ( "% c", &option );

But, this still remains unexplained.
 
R

Richard Bos

MNQ said:
It prints the error message and the menu twice but i cant understand why.
while (!done)
{
char option=0;
printf("\n 1) Add two numbers.");
printf("\n 2) The power of a number.");
printf("\n 3) Exit.");
printf("\n Enter option: "); //User enters option from menu
scanf("%c",&option);

When you enter, say, 4, what does this scanf() call read? What does it
_not_ (yet) read? What happens with the bits it doesn't read? What does
this scanf() do next time through the loop?

Richard
 
A

AJ Mohan Rao

Hello All

Im trying to relearn ANSI C. I have written some code but cannot
understand
why it is not do what i think it should. It prints the error message and
the menu twice but i cant understand why. Can any one help me please?

naveed

/*
Last modified 5 February 2004
I dont know why after selecting an option it
displays 'Enter option' twice twice*/

#include<stdio.h>

int add(int numa, int numb)//Function to add two numbers
^^ please avoid wrong comments
{
int sum=0;

sum=numa+numb;

return (sum);
}

int power(int num, int power)//Function to find x to the power n
{
int ans;

if (power<0) //Negative power
ans=-1;
else //Zero power becomes 1
ans=1;

while (power>0) //Positive powers
{
ans=num*ans;
power=power-1;
}

return (ans);
}

int main(void)
{
int num1=0, num2=0, answer=0, done=0;
while (!done)

{
char option=0;
printf("\n 1) Add two numbers.");
printf("\n 2) The power of a number.");
printf("\n 3) Exit.");
printf("\n Enter option: "); //User enters option from menu
scanf("%c",&option);

as you are reading option as a char, after one iteration scanf tries to
put the
next available char into the variable 'option'. Thats why alternatively
option contains '\n'(carriage-return entered by user while entering the
values)
and it prints error and the loop again continues and so the second menu.

to empty all space characters use
scanf ("%c ", &option);
or make option as int
 
A

AJ Mohan Rao

to empty all space characters use
scanf ("%c ", &option);

sorry! it was
scanf (" %c", &option);
which eats up all unwanted(in this case) space chars before reading a
non-space char
 
?

=?ISO-8859-1?Q?J=FCrgen_Voss?=

Jürgen Voss said:
%c: exactly one character is read from input



%d: several digits are read from input including trailing whitespaces


Sorry, it is leading whitespaces instead .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top