0

I'd also nitpick your prompts that don't end in a newline because they might not show up on all systems before scanf() blocks for input. But those are minor issues for strict conformance.

do you mean that code

printf("\nPress Q then press Enter to quit\n");
      scanf("%c",&ch);

there problem happen when i run my code and press e (for Enter)in

      printf("\nPress E then press Enter to Enter Complex Number");
      printf("\nPress Q then press Enter to quit\n");
      scanf("%c",&ch);

will go to :

     if(ch=='E'||ch=='e') // if user enter E or e from keyboard
     {
         //clrscr();
         printf("\nPlease enter the two real and imaginary complex numbers (a+jb)    :\n");
         printf("\nreal number:");
         scanf("%d",&x.real);
         printf("\nImaginary number:");
         scanf("%d", &x.imag);
     }

after enter data(real and imag)then repeat that code two time

      printf("\nPress E then press Enter to Enter Complex Number");
      printf("\nPress Q then press Enter to quit\n");
      scanf("%c",&ch);

Edited by ahmedhamdy

0

like this:

Press E then press Enter to Enter Complex Number
Press Q then press Enter to quit
Press E then press Enter to Enter Complex Number
Press Q then press Enter to quit

then wait me to enter E or Q
why this happened????????

1

do you mean that code

No, I mean this code:

printf("\nreal number:");
scanf("%d",&x.real);
printf("\nImaginary number:");
scanf("%d", &x.imag);

There's no guarantee that you'll see "real number:" before scanf() blocks for input, which means the user will have no clue what the program is waiting for. If you're not ending the prompt with a newline, I'd recommend calling fflush() to ensure that the prompt will be visible at the right time:

printf("\nreal number:");
fflush(stdout);
scanf("%d",&x.real);
printf("\nImaginary number:");
fflush(stdout);
scanf("%d", &x.imag);

why this happened????????

Probably because you're mixing single character input with formatted input. They don't mix well. To see exactly what characters are being processed, just output ch immediately after scanf() returns. I'm willing to bet you'll be surprised.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.