Thread: Making a calculator run again!

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Question Making a calculator run again!

    I am almost done with my second program ever created in C and I need some help to make it run again after it gives me the answer! What it does, it quits and I have to run it again and again! I just want it to bring me back to selection of choice menu when it is done!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main( )
    {
    int x;
    int y;
    int choice;
    
    printf ( "Hello! This is the second version of my calculator\n" );
    printf ( "Select addition or subtraction\n" );
    printf ( "1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n");
    scanf ("&#37;d", &choice);
    
    if ( choice == 1 )
        {
        printf ( "You chose addition!\n\n" );
        printf ( "Input the first integer,x:\n" );
        scanf ("%d", &x);
        printf ( "Input the second integer,y:\n" );
        scanf ("%d", &y);
        printf ("The sum is: %d\n", x + y );
        }
    else if ( choice == 2 )
        {
        printf ( "You chose subtraction ( Note that this calculator will subtract the second term (y) from the first term (x)\n\n" );
        printf ( "Input the first integer,x:\n" );
        scanf ( "%d", &x );
        printf ( "Input the second integer,y:\n" );
        scanf ( "%d", &y );
        printf ( "The difference is: %d\n", x - y );
        }
    else if (choice == 3)
        {
        printf ("You chose Multiplication!\n\n");
        printf ("Input the first integer,x:\n");
        scanf ("%d", &x);
        printf ("Input the second integer,y:\n");
        scanf ("%d", &y);
        printf ("The product is:%d\n", x * y);
        }
    else if (choice == 4)
        {
        printf ("You chose division!\n\n");
        printf ("Input the first integer,x:\n");
        scanf ("%d", &x);
        printf ("Input the second integer, y:\n");
        scanf ("%d", &y);
        printf ("The result is:%d\n", x / y);
        }
    else
        {
        printf ("INVALID CHOICE! EXITTING!\n");
        }
    }
    I know I should have used switch statement, I will probably rewrite since that is also a good practice
    Last edited by cookie; 06-09-2007 at 01:31 AM. Reason: adding something

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Put everything you want to be repeated inside a while (1) loop (in this case, everything after your variable definitions).

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll also need the code in red, below, to be added as indicated:

    Code:
       else
           {
           printf ("INVALID CHOICE! EXITTING!\n");
           return 0;
           }
       }   /* now marks the end of the new while(1) loop */
    }   /* new end of main() */
    "Exiting" only has one 'T' in it.

    How's the program run?

  4. #4
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Unhappy It is me, cookie again!

    It makes no sense to me to put the WHOLE code in a while loop!

    That would be :
    Code:
    preprocessors
    int main ()
    {
    while(1)
    {
    the code
    }
    }
    something like this, or what!?????



    And I have another idea but, I do not know if it is right and dop not know how to do it correctly


    asign choices a function for each, then put those all functions into a big function and run it till the user decides to quit it!


    Pls correct me if I am wrong

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    There are ways of getting it done, and then there are better ways.

    Sounds like you might have the basic idea.

    I don't fully understand your idea with the functions, but one method I used was to have a loop where the confition is a function result. The result was based upon the user's decision to quit or not.
    Last edited by MacGyver; 06-09-2007 at 10:52 AM.

  6. #6
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Ok, thanks!

    I did it with the while loop, but I do not understand why u have to put the argument 1 in the
    while (1)! It annoys me because I do not understand it lol!


    Another thing I am trying to make is make a char type variable and make that char, when pressed, exit the program but I do not know how to make that, can you help??? Thanks

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by cookie View Post
    I did it with the while loop, but I do not understand why u have to put the argument 1 in the
    while (1)! It annoys me because I do not understand it lol!
    Whatever is inside while() will be evaluated as a boolean expression. This means it will be resolved as either true or false. In C, every number you may give it is true, except 0. That is the only way to achieve false.

    You could have put while(2) and it would still run in the same manner.

    All you did was start a loop that has a condition that will never be false.

    Quote Originally Posted by cookie View Post
    Another thing I am trying to make is make a char type variable and make that char, when pressed, exit the program but I do not know how to make that, can you help??? Thanks
    I don't know what you mean by this.

    What you should do when writing posts here with regard to programming, is realize that many of us do not understand what you're thinking, or what you've already tried. We just go off of the posts that you're giving us.

    For example, you refer to a "char type variable". OK, so I might think you have a varible, like this:

    Code:
    char x;
    Then you talk about pressing that char, and I get slightly confused.... pressing what? Pressing what is inside the variable x? So if x == 'b', are suggesting that the program terminate when 'b' is pressed? Do you want it to close at any given time when 'b' is pressed on the keyboard?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by cookie View Post
    I did it with the while loop, but I do not understand why u have to put the argument 1 in the
    while (1)! It annoys me because I do not understand it lol!


    Another thing I am trying to make is make a char type variable and make that char, when pressed, exit the program but I do not know how to make that, can you help??? Thanks
    OK, we're looking at the while() loop:
    Code:
    while(1)
    
    print up a header for the page  (Welcome to MyProgram, etc.)
    print up a list of the menu choices:
    #1. Play my little guessing game
    #2. Read my blog entry for yesterday
    #3. Look at pics of my dog when it got too close to a porcupine (ouch!)
    #4. Quit the Program.
    etc.
    
    print query for user's choice:
    "Please choose a number and press Enter"
    
    scanf("%d", &number); 
    
    switch number
       case 1: guessingGame();
       case 2: ReadBlog();
       case 3: DogPics();
       case 4: return() or exit();
    etc.
    So the menu will always be returned to after the user finishes one area. He can choose another choice, or he can choose to quit.

  9. #9
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Unhappy Sorry for not making myself clear!

    First of all, thanks for the while loops explanation! I get it now!
    What I meant with the unclear part is that I want to make a button that will exit the program!

    I thought I was supposed to make a char type variable! If I am right with the char type variable, how do I make a function that will exit the program when I type in that char?


    I hope I make myself clear now

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by cookie View Post
    First of all, thanks for the while loops explanation! I get it now!
    What I meant with the unclear part is that I want to make a button that will exit the program!

    I thought I was supposed to make a char type variable! If I am right with the char type variable, how do I make a function that will exit the program when I type in that char?


    I hope I make myself clear now
    Something like Control + C might work on Windows and Control + D (I think) might work on Unix....

    But again, this is just a guess.

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I know what a while loop is, but how does while(1) work? Since 1 is a constant would loop forever until a break is called, or is there something I am missing here...

    Since the option to quit is either going to be a yes or a no you could have a bool called something like 'quit' then loop the program until quit is not 0. Heres an example, its pretty crude, but anyway:
    Code:
    #include <stdio.h>
    
    bool Continue()
    {
        char ans='x';
        while(ans!='y' && ans!='n')//Loop until a y or n is entered
        {
            printf("Run this program again? y/n: ");
            ans=getchar(); //get answer
            getchar();               
        }   
        if(ans=='y') return 0; 
        else         return 1; //if answer is no then set quit to 1
    }
    
    int main()
    {
        bool quit = 0;
        while(quit == 0) //The program runs until quit is not 0
        {
            printf("Do Stuff\nDo Stuff\nDo Stuff\n");
            quit = Continue(); //Call this function to get a value for quit
        }           
    }

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    In C there is no built-in bool type. In C99 there is a standard header file <stdbool.h> which #define's true and false as 1 and 0, resp. Only in C++ is bool a built-in type.

  13. #13
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh, I did not know that. I use Dev-C++ and it works for me. Still you could just replace it with an int, short, or char and it would work fine.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Check that you're not inadvertently compiling your C in C++ mode.

  15. #15
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by mike_g View Post
    Oh, I did not know that. I use Dev-C++ and it works for me. Still you could just replace it with an int, short, or char and it would work fine.
    The stdbool.h was just an alternative for your int. If you still want to use th bool which u have already done. Then include this header.

    Or you will have to replace all booll by int. And dont replace it with char. Becuase getchar return int not a char.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 07:00 AM
  2. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 11:59 PM
  3. Making control...
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2004, 01:42 PM
  4. Replies: 2
    Last Post: 01-13-2003, 01:28 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM