We help IT Professionals succeed at work.

can anyone make this function work?

d_terano
d_terano asked
on
i need help with the function below, pls help me make it work properly but with the slightest alteration possible.
thanks...p.s if possilble, pls comment on what was wrong with it?
---------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>

void exit(int num1, int num2);

main()
{


 int num1,num2;
 char opr; char end;
 //char operator;

do {
  printf("Please Enter a number             :");
  scanf("%d",&num1);
  printf("Please Enter another number       :");
  scanf("%d",&num2);
  printf("");
  printf("Please Enter an operator (+,-,*,/):");
  scanf("%c",&opr) ;





  switch ( opr ) {
          case '+':
               {
               printf("The answer is                =%d\n",num1+num2);
               break;
          }
          case '-':
               {
               printf("The answer is                =%d\n",num1-num2);
               break;
          }
          case '*':
               {
               printf("The answer is                =%d\n",num1*num2);
               break;
          }
          case '/':
               {
               printf("The answer is                =%d\n",num1/num2);
               break;
          }

          default : {

               printf("Invalid Input!\n");
               break;

          }
     }
  printf("Do you want to continue?(y/n)\n");
  scanf("%c",&end);
  printf("\n");

 }while (end != 'n');

 return 0;
 }

Comment
Watch Question

CERTIFIED EXPERT
Commented:
#include <stdio.h>
#include <stdlib.h>

int main()
{
     int num1,num2;
     char opr[2]; char end[2];

     do {
           printf("Please Enter a number             :");
           scanf("%d",&num1);
           printf("Please Enter another number       :");
           scanf("%d",&num2);
           printf("Please Enter an operator (+,-,*,/):");
           scanf("%s",opr) ;

           switch(opr[0])
           {
         case '+':
         {
              printf("The answer is                =%d\n",num1+num2);
              break;
         }
         case '-':
         {
              printf("The answer is                =%d\n",num1-num2);
              break;
         }
         case '*':
         {
              printf("The answer is                =%d\n",num1*num2);
              break;
         }
         case '/':
         {
              printf("The answer is                =%d\n",num1/num2);
              break;
         }
         default :
         {
              printf("Invalid Input!\n");
              break;
         }
         }
          printf("Do you want to continue?(y/n)\n");
           scanf("%s",end);
           printf("\n");
     }while (end[0] != 'n');
     
     return 0;
}

this works okay, i think the problem lies in the fact that the \n is still in the input stream when you enter a number
CERTIFIED EXPERT

Commented:
if you call scanf twice the first call chews up the \n, like this

#include <stdio.h>
#include <stdlib.h>

int main()
{
     int num1,num2;
     char opr; char end;

     do {
           printf("Please Enter a number             :");
           scanf("%d",&num1);
           printf("Please Enter another number       :");
           scanf("%d",&num2);
           printf("Please Enter an operator (+,-,*,/):");
           scanf("%c",&opr);
           scanf("%c",&opr);

           switch(opr)
           {
         case '+':
         {
              printf("The answer is                =%d\n",num1+num2);
              break;
         }
         case '-':
         {
              printf("The answer is                =%d\n",num1-num2);
              break;
         }
         case '*':
         {
              printf("The answer is                =%d\n",num1*num2);
              break;
         }
         case '/':
         {
              printf("The answer is                =%d\n",num1/num2);
              break;
         }
         default :
         {
              printf("Invalid Input!\n");
              break;
         }
         }
          printf("Do you want to continue?(y/n)\n");
           scanf("%c",&end);
           scanf("%c",&end);
           printf("\n");
     }while (end != 'n');
     
     return 0;
}

Commented:
Add:
fflush( stdin );

After each scanf(..) call in order to clear the input stream before trying to read in again.  Its annoying but you have to do it.

This is better than the above solution because if the user types more than a character and enter, only the first extra character is ignored.

Example:
printf( "Do you want to continue (y/n)? " );
scanf( "%c", &end );
fflush( stdin );

Commented:
I would have to point out that if the user accidentally enters 'yy<Enter>', the program will still break.

fflush( stdin ); is the proper way to flush the stream.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.