Thread: while statement

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    while statement

    THis almost works, but i'm stuck. It terminates, I want it after the user does a problem, returns to the top list.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <math.h>
    
    int main()
         
    {
      float addition, add1, add2, add3;
      float subtract, sub1, sub2;
      float multiply, mult1, mult2, mult3;
      float division, div1, div2;
      float modulate, smod1, smod2;
      char E, choice;
    
      do
        {
      printf("Please type in one of the following letters:");
      printf("\nA to add  three numbers: ");
      printf("\nS to Subtract two numbers: ");
      printf("\nM to Multiply three number: ");
      printf("\nD to Divide two number: ");
      printf("\nO to Modulate two number: ");
      printf("\nE to Exit program: ");
      printf("\n>:\n");
      scanf("%c", &choice);
      fflush(0);
      choice=toupper(choice);
        }while (choice != 'A' && choice != 'S' && choice != 'M' && choice != 'D' && choice != 'O' && choice != 'E');
     
      switch (choice)
    
         {
        case 'A':
         printf("\nPlease type the first number: ");
         scanf("%f", &add1);
         printf("\nPlease type the second number: ");
         scanf("%f", &add2);
         printf("\nPlease type the third number: ");
         scanf("%f", &add3);
         addition = add1+add2+add3;
         printf("%.2f + %.2f + %.2f equals %.2f\n", add1, add2, add3, addition);
         break;
     
       case 'S':
         printf("\nPlease type the first number: ");
         scanf("%f", &sub1);
         printf("\nPlease type the second number: ");
         scanf("%f", &sub2);
         subtract = sub1-sub2;
         printf("%.2f minus %.2f equals %.2f\n", sub1, sub2, subtract);
         break;
     
       case 'M':
         printf("\nPlease type the first number: ");
         scanf("%f", &mult1);
         printf("\nPlease type the second number: ");
         scanf("%f", &mult2);
         printf("\nPlease type the third number: ");
         scanf("%f", &mult3);
         multiply = mult1*mult2*mult3;
         printf("%.2f x %.2f x %.2f = %.2f\n", mult1, mult2, mult3, multiply);
         break;
    
        case 'D':
         printf("\nPlease type the first number: ");
         scanf("%f", &div1);
         printf("\nPlease type the second number: ");
         scanf("%f", &div2);
         division = div1/div2;
         printf("%.2f divided by %.2f equals %.2f\n\n", div1, div2, division);
         break;
    
       case 'O':
         printf("\nPlease type the first number: ");
         scanf("%f", &smod1);
         printf("\nPlease type the second number: ");
         scanf("%f", &smod2);
         modulate = fmod(smod1, smod2); 
         printf("The modulus of %.2f and %.2f leaves a remainder of %.2f\n\n", smod1, smod2, modulate);
         break;
    
       case 'E':
         printf("\n\nThank You and Goodbye!: %c", E);
         scanf("%c", &E);
         break;
    
        default:
          printf("\nInvalid choice, please try again");
          fflush(0);
          choice=toupper(choice);
      }
      return 0;
    }

    PS: This is the format we have to use that our instructer told us.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You need a second while() / do...while() loop on the outside of your code while (choice != 'E').

    under default: choice=toupper(choice) is redundant, not needed, and repeatitive.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    fflush(0);
    Ohhhhh fflush used only for clearing/flushing the output buffer(standard) not the input buffer. See FAQ

    ssharish2005

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by ssharish2005
    Code:
    fflush(0);
    Ohhhhh fflush used only for clearing/flushing the output buffer(standard) not the input buffer. See FAQ

    ssharish2005
    this is what he told us to use and how he said to use it

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by wonderpoop
    this is what he told us to use and how he said to use it
    It's just fine. We see more fflush(stdin);'s than we can shake a stick at -- sometimes we miss ones like these and lump them in with the wrong crowd.

    [edit]On the downside, your instructor is still having you take user input with scanf. So it makes such conclusions more likely. I wish it would be avoided.
    Last edited by Dave_Sinkula; 10-23-2006 at 07:52 PM. Reason: ...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by Dave_Sinkula
    It's just fine. We see more fflush(stdin);'s than we can shake a stick at -- sometimes we miss ones like these and lump them in with the wrong crowd.

    [edit]On the downside, your instructor is still having you take user input with scanf. So it makes such conclusions more likely. I wish it would be avoided.
    this while statement is killing me, i tried it at the end, but it doesn't work right still. I can't think of any other while statement to put at the end, because it works better at the beginning. I put the fflush at the end with the choice=toupper(choice). Now if i type in the wrong letter i get the top menu, but if i type in the right letter it gives me the choice, but then exits. I want the user to exit only when they hit 'E'.
    Last edited by wonderpoop; 10-23-2006 at 08:12 PM.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    At the risk of the lambasting that will follow...
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
       float addition, add1, add2, add3;
       float subtract, sub1, sub2;
       float multiply, mult1, mult2, mult3;
       float division, div1, div2;
       float modulate, smod1, smod2;
       char E, choice;
    
       for ( ;; )
       {
          printf("Please type in one of the following letters:");
          printf("\nA to add  three numbers: ");
          printf("\nS to Subtract two numbers: ");
          printf("\nM to Multiply three number: ");
          printf("\nD to Divide two number: ");
          printf("\nO to Modulate two number: ");
          printf("\nE to Exit program: ");
          printf("\n>:\n");
          scanf("%c", &choice);
          fflush(0);
          choice=toupper(choice);
    
          switch ( choice )
          {
          case 'A':
             printf("\nPlease type the first number: ");
             scanf("%f", &add1);
             printf("\nPlease type the second number: ");
             scanf("%f", &add2);
             printf("\nPlease type the third number: ");
             scanf("%f", &add3);
             addition = add1+add2+add3;
             printf("%.2f + %.2f + %.2f equals %.2f\n", add1, add2, add3, addition);
             break;
    
          case 'S':
             printf("\nPlease type the first number: ");
             scanf("%f", &sub1);
             printf("\nPlease type the second number: ");
             scanf("%f", &sub2);
             subtract = sub1-sub2;
             printf("%.2f minus %.2f equals %.2f\n", sub1, sub2, subtract);
             break;
    
          case 'M':
             printf("\nPlease type the first number: ");
             scanf("%f", &mult1);
             printf("\nPlease type the second number: ");
             scanf("%f", &mult2);
             printf("\nPlease type the third number: ");
             scanf("%f", &mult3);
             multiply = mult1*mult2*mult3;
             printf("%.2f x %.2f x %.2f = %.2f\n", mult1, mult2, mult3, multiply);
             break;
    
          case 'D':
             printf("\nPlease type the first number: ");
             scanf("%f", &div1);
             printf("\nPlease type the second number: ");
             scanf("%f", &div2);
             division = div1/div2;
             printf("%.2f divided by %.2f equals %.2f\n\n", div1, div2, division);
             break;
    
          case 'O':
             printf("\nPlease type the first number: ");
             scanf("%f", &smod1);
             printf("\nPlease type the second number: ");
             scanf("%f", &smod2);
             modulate = fmod(smod1, smod2); 
             printf("The modulus of %.2f and %.2f leaves a remainder of %.2f\n\n", smod1, smod2, modulate);
             break;
    
          case 'E':
             printf("\n\nThank You and Goodbye!: %c", E);
             scanf("%c", &E);
             goto done;
    
          default:
             printf("\nInvalid choice, please try again");
             fflush(0);
             choice=toupper(choice);
          }
       }
    done:
       return 0;
    }
    Breaking a loop from a switch is one of the few places where a goto may be the better candidate.

    Let the argument ensue...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Heh, personally I agree with you, but I have to wonder (and I'd wonder with my compiler if I had it handy)...what if you initalized choice to anything other than 'E', made (choice != 'E') the condition of the for loop, and used continue instead of goto?

    [edit] Or, of course, the do-while loop that was originally suggested? It helps to read the whole thread.
    Last edited by pianorain; 10-23-2006 at 08:36 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by Dave_Sinkula
    At the risk of the lambasting that will follow...
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
       float addition, add1, add2, add3;
       float subtract, sub1, sub2;
       float multiply, mult1, mult2, mult3;
       float division, div1, div2;
       float modulate, smod1, smod2;
       char E, choice;
    
       for ( ;; )
       {
          printf("Please type in one of the following letters:");
          printf("\nA to add  three numbers: ");
          printf("\nS to Subtract two numbers: ");
          printf("\nM to Multiply three number: ");
          printf("\nD to Divide two number: ");
          printf("\nO to Modulate two number: ");
          printf("\nE to Exit program: ");
          printf("\n>:\n");
          scanf("%c", &choice);
          fflush(0);
          choice=toupper(choice);
    
          switch ( choice )
          {
          case 'A':
             printf("\nPlease type the first number: ");
             scanf("%f", &add1);
             printf("\nPlease type the second number: ");
             scanf("%f", &add2);
             printf("\nPlease type the third number: ");
             scanf("%f", &add3);
             addition = add1+add2+add3;
             printf("%.2f + %.2f + %.2f equals %.2f\n", add1, add2, add3, addition);
             break;
    
          case 'S':
             printf("\nPlease type the first number: ");
             scanf("%f", &sub1);
             printf("\nPlease type the second number: ");
             scanf("%f", &sub2);
             subtract = sub1-sub2;
             printf("%.2f minus %.2f equals %.2f\n", sub1, sub2, subtract);
             break;
    
          case 'M':
             printf("\nPlease type the first number: ");
             scanf("%f", &mult1);
             printf("\nPlease type the second number: ");
             scanf("%f", &mult2);
             printf("\nPlease type the third number: ");
             scanf("%f", &mult3);
             multiply = mult1*mult2*mult3;
             printf("%.2f x %.2f x %.2f = %.2f\n", mult1, mult2, mult3, multiply);
             break;
    
          case 'D':
             printf("\nPlease type the first number: ");
             scanf("%f", &div1);
             printf("\nPlease type the second number: ");
             scanf("%f", &div2);
             division = div1/div2;
             printf("%.2f divided by %.2f equals %.2f\n\n", div1, div2, division);
             break;
    
          case 'O':
             printf("\nPlease type the first number: ");
             scanf("%f", &smod1);
             printf("\nPlease type the second number: ");
             scanf("%f", &smod2);
             modulate = fmod(smod1, smod2); 
             printf("The modulus of %.2f and %.2f leaves a remainder of %.2f\n\n", smod1, smod2, modulate);
             break;
    
          case 'E':
             printf("\n\nThank You and Goodbye!: %c", E);
             scanf("%c", &E);
             goto done;
    
          default:
             printf("\nInvalid choice, please try again");
             fflush(0);
             choice=toupper(choice);
          }
       }
    done:
       return 0;
    }
    Breaking a loop from a switch is one of the few places where a goto may be the better candidate.

    Let the argument ensue...

    It has to be a do while statement. I don't even know anything about goto done yet. If you test the program you will see is no problem breaking the loop, since i can't get one to work yet anyways
    Last edited by wonderpoop; 10-23-2006 at 08:41 PM.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by wonderpoop
    It has to be a do while statement. I don't even know anything about goto done yet. If you test the program you will see is no problem breaking the loop, since i can't get one to work yet anyways
    There's no loop to break, thus "no problem".

    You want the menu in a loop -- including the action -- right? Put it in a loop.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by Dave_Sinkula
    There's no loop to break, thus "no problem".

    You want the menu in a loop, right? Put it in a loop.
    That's what the while statement is for. If i type in the wrong letter, it goes back to the menu just fine. But i can't figure out how to get it to go back to that same menu each time the person types in ASMDO does the problem, then back to the menu, unless they type E for exit and it exits the program.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    *sigh*

    The "whole thing in a loop".
    Code:
       do
       {
          printf("Please type in one of the following letters:");
          printf("\nA to add  three numbers: ");
          printf("\nS to Subtract two numbers: ");
          printf("\nM to Multiply three number: ");
          printf("\nD to Divide two number: ");
          printf("\nO to Modulate two number: ");
          printf("\nE to Exit program: ");
          printf("\n>:\n");
          scanf("%c", &choice);
          fflush(0);
          choice=toupper(choice);
    
          if ( choice == 'E' )
          {
             puts("\n\nThank You and Goodbye!");
             break;
          }
    
          switch ( choice )
          {
          case 'A':
             printf("\nPlease type the first number: ");
             scanf("%f", &add1);
             printf("\nPlease type the second number: ");
             scanf("%f", &add2);
             printf("\nPlease type the third number: ");
             scanf("%f", &add3);
             addition = add1+add2+add3;
             printf("%.2f + %.2f + %.2f equals %.2f\n", add1, add2, add3, addition);
             break;
    
          case 'S':
             printf("\nPlease type the first number: ");
             scanf("%f", &sub1);
             printf("\nPlease type the second number: ");
             scanf("%f", &sub2);
             subtract = sub1-sub2;
             printf("%.2f minus %.2f equals %.2f\n", sub1, sub2, subtract);
             break;
    
          case 'M':
             printf("\nPlease type the first number: ");
             scanf("%f", &mult1);
             printf("\nPlease type the second number: ");
             scanf("%f", &mult2);
             printf("\nPlease type the third number: ");
             scanf("%f", &mult3);
             multiply = mult1*mult2*mult3;
             printf("%.2f x %.2f x %.2f = %.2f\n", mult1, mult2, mult3, multiply);
             break;
    
          case 'D':
             printf("\nPlease type the first number: ");
             scanf("%f", &div1);
             printf("\nPlease type the second number: ");
             scanf("%f", &div2);
             division = div1/div2;
             printf("%.2f divided by %.2f equals %.2f\n\n", div1, div2, division);
             break;
    
          case 'O':
             printf("\nPlease type the first number: ");
             scanf("%f", &smod1);
             printf("\nPlease type the second number: ");
             scanf("%f", &smod2);
             modulate = fmod(smod1, smod2); 
             printf("The modulus of %.2f and %.2f leaves a remainder of %.2f\n\n", smod1, smod2, modulate);
             break;
    
             /* note stuff not here */
    
          default:
             printf("\nInvalid choice, please try again");
             fflush(0);
          }
       } while(1);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by Dave_Sinkula
    *sigh*

    The "whole thing in a loop".
    Code:
       do
       {
          printf("Please type in one of the following letters:");
          printf("\nA to add  three numbers: ");
          printf("\nS to Subtract two numbers: ");
          printf("\nM to Multiply three number: ");
          printf("\nD to Divide two number: ");
          printf("\nO to Modulate two number: ");
          printf("\nE to Exit program: ");
          printf("\n>:\n");
          scanf("%c", &choice);
          fflush(0);
          choice=toupper(choice);
    
          if ( choice == 'E' )
          {
             puts("\n\nThank You and Goodbye!");
             break;
          }
    
          switch ( choice )
          {
          case 'A':
             printf("\nPlease type the first number: ");
             scanf("%f", &add1);
             printf("\nPlease type the second number: ");
             scanf("%f", &add2);
             printf("\nPlease type the third number: ");
             scanf("%f", &add3);
             addition = add1+add2+add3;
             printf("%.2f + %.2f + %.2f equals %.2f\n", add1, add2, add3, addition);
             break;
    
          case 'S':
             printf("\nPlease type the first number: ");
             scanf("%f", &sub1);
             printf("\nPlease type the second number: ");
             scanf("%f", &sub2);
             subtract = sub1-sub2;
             printf("%.2f minus %.2f equals %.2f\n", sub1, sub2, subtract);
             break;
    
          case 'M':
             printf("\nPlease type the first number: ");
             scanf("%f", &mult1);
             printf("\nPlease type the second number: ");
             scanf("%f", &mult2);
             printf("\nPlease type the third number: ");
             scanf("%f", &mult3);
             multiply = mult1*mult2*mult3;
             printf("%.2f x %.2f x %.2f = %.2f\n", mult1, mult2, mult3, multiply);
             break;
    
          case 'D':
             printf("\nPlease type the first number: ");
             scanf("%f", &div1);
             printf("\nPlease type the second number: ");
             scanf("%f", &div2);
             division = div1/div2;
             printf("%.2f divided by %.2f equals %.2f\n\n", div1, div2, division);
             break;
    
          case 'O':
             printf("\nPlease type the first number: ");
             scanf("%f", &smod1);
             printf("\nPlease type the second number: ");
             scanf("%f", &smod2);
             modulate = fmod(smod1, smod2); 
             printf("The modulus of %.2f and %.2f leaves a remainder of %.2f\n\n", smod1, smod2, modulate);
             break;
    
             /* note stuff not here */
    
          default:
             printf("\nInvalid choice, please try again");
             fflush(0);
          }
       } while(1);
    Now what happens if the user types in R?

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Bad things. You're using scanf.

    Fully uglified, this is what you are supposed to churn out (more or less).
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
       float addition, add1, add2, add3;
       float subtract, sub1, sub2;
       float multiply, mult1, mult2, mult3;
       float division, div1, div2;
       float modulate, smod1, smod2;
       char choice;
       int cleanup;
    
       do
       {
          puts("Please type in one of the following letters:");
          puts("A to add  three numbers: ");
          puts("S to Subtract two numbers: ");
          puts("M to Multiply three number: ");
          puts("D to Divide two number: ");
          puts("O to Modulate two number: ");
          puts("E to Exit program: ");
          printf(">:");
          fflush(stdout);
          scanf("%c", &choice);
          choice=toupper(choice);
          while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
    
          if ( choice == 'E' )
          {
             puts("Thank You and Goodbye!");
             break;
          }
    
          switch ( choice )
          {
          case 'A':
             printf("Please type the first number: ");
             fflush(stdout);
             scanf("%f", &add1);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             printf("Please type the second number: ");
             fflush(stdout);
             scanf("%f", &add2);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             printf("Please type the third number: ");
             fflush(stdout);
             scanf("%f", &add3);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             addition = add1+add2+add3;
             printf("%.2f + %.2f + %.2f equals %.2f\n", add1, add2, add3, addition);
             break;
    
          case 'S':
             printf("Please type the first number: ");
             fflush(stdout);
             scanf("%f", &sub1);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             printf("Please type the second number: ");
             fflush(stdout);
             scanf("%f", &sub2);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             subtract = sub1-sub2;
             printf("%.2f minus %.2f equals %.2f\n", sub1, sub2, subtract);
             break;
    
          case 'M':
             printf("Please type the first number: ");
             fflush(stdout);
             scanf("%f", &mult1);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             printf("Please type the second number: ");
             fflush(stdout);
             scanf("%f", &mult2);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             printf("Please type the third number: ");
             fflush(stdout);
             scanf("%f", &mult3);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             multiply = mult1*mult2*mult3;
             printf("%.2f x %.2f x %.2f = %.2f\n", mult1, mult2, mult3, multiply);
             break;
    
          case 'D':
             printf("Please type the first number: ");
             fflush(stdout);
             scanf("%f", &div1);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             printf("Please type the second number: ");
             fflush(stdout);
             scanf("%f", &div2);
             division = div1/div2;
             printf("%.2f divided by %.2f equals %.2f\n", div1, div2, division);
             break;
    
          case 'O':
             printf("Please type the first number: ");
             fflush(stdout);
             scanf("%f", &smod1);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             printf("Please type the second number: ");
             fflush(stdout);
             scanf("%f", &smod2);
             while ( (cleanup = getchar()) != '\n' && cleanup != EOF );
             modulate = fmod(smod1, smod2); 
             printf("The modulus of %.2f and %.2f leaves a remainder of %.2f\n", smod1, smod2, modulate);
             break;
    
          default:
             puts("Invalid choice, please try again");
             fflush(stdout);
          }
       } while ( 1 );
       return 0;
    }
    Happy bad coding. Let's see if you get to the better ways and can get away with it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM