C Do while help !!

+1 Sharad Gupta · September 6, 2014
Guys, I am trying out this basic program in which I have to add, subtract, multiply or divide two numbers based on user request.

I am facing an issue, which is, when the user enters an alphabet, this program runs into INFINITE LOOP and I am not able to see the message 'Enter the proper choice idiot !'

So, What should be the modification in this C code? PLEASE help me guys. Thanks in advance. :)

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

int main()
{
    float a,b,ans=0;
    int op;
    do
    {

    printf("MENU: \n");
    printf("1. Addition \n2. Subtraction \n3. Multiplication \n4. Division \n5. Exit \n");
    printf("What operation do you want to perform? (1/2/3/4/5) \n");
    scanf(" %d", &op);

    if( (isdigit(op)>5) || (isalpha(op)!=0) )
    {
        goto reenter;
    }

    if(op==5)
    {
        goto quit;
    }

    if(op==1 || op==2 || op==3 || op==4)
    {
    printf("Enter the two real numbers 'a' and 'b' \n");
    scanf(" %f %f", &a, &b);
    }

    switch(op)
    {
    case 1: ans = a + b; printf("Answer = %0.2f \n", ans); break;
    case 2: ans = a - b; printf("Answer = %0.2f \n", ans); break;
    case 3: ans = a * b; printf("Answer = %0.2f \n", ans); break;
    case 4: ans = a / b; printf("Answer = %0.2f \n", ans); break;
    quit: case 5: printf("Thank you dude! You are now out of the process!"); break;
    reenter: default: printf("Enter the proper choice idiot ! \n"); break;
    }

    }while(op !=5);
    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
+2 M .A.K · September 8, 2014
This is my modification check it out!:D

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> //don't need this anymore

/*add #include <conio.h> and use getch() to receive op
otherwise user can input 12345678910... and first option will be selected!*/

int main(){

    float a,b,ans=0;
    char op,choice; //char takes less space than int's and is easier to process while working with inputs

    while(1){

    printf("\nMENU: \n");//this will be printed \n makes it rock!
    printf("1. Addition \n2. Subtraction \n3. Multiplication \n4. Division \n5. Exit \n");
    printf("What operation do you want to perform? (1/2/3/4/5) \n");

    scanf(" %c", &op);//use getch here i'm running on codeblock and it doesn't have conio.h here!

    if(op=='5'){
        printf("Thank you dude! You are now out of the process!"); break;
    }

    if(op=='1' || op=='2' || op=='3' || op=='4'){
    printf("Enter the two real numbers 'a' and 'b' \n");
    scanf(" %f %f", &a, &b);

    //add some code here to check numbers are valid or wrong space,tab,etc
    //first rule : user is dumb!drank!sleepy!... what ever...

    switch(op){
    case '1': ans = a + b; printf("Answer = %0.2f \n", ans); break;
    case '2': ans = a - b; printf("Answer = %0.2f \n", ans); break;
    case '3': ans = a * b; printf("Answer = %0.2f \n", ans); break;
    case '4': ans = a / b; printf("Answer = %0.2f \n", ans); break;
    }//this breaks out of if not while(1)

    }else{
        //it's better to clear the screen here.
        printf("\nEnter the proper choice idiot !"); continue;//really?!
    }

   //it's better to ask here once more
    printf("Do you wanna continue?(y to continue)");
    scanf(" %c", &choice);

    if(choice == 'y'){
//It will be nice to clear screen here as well...
        continue; //Does n't really need this!but seems better hang in there.
    }else{
       printf("Thank you dude! You are now out of the process!"); break;//used this twice think about it
        break;//but we need this one it breaks out of program
    }
    }

    return 0;
}


plus as friends said avoid using goto where ever you can ... and sorry for indention ...
0 Tommy Phan · September 8, 2014
it runs to an infinite loop when you enter a letter because your while statement-(op !=5) is saying, that the loop will run as long as op is not equal to 5 and a letter is not equal to 5.

Solution: just change your while loop to while(isdigit(op)).
  • 1

C

107,257 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator