Trying to loop my program or reset it Dev c++

This is a discussion on Trying to loop my program or reset it Dev c++ within the C++ Programming forums, part of the General Programming Boards category; I just started c++ yesterday and i'm trying to make a calculator. I'm using switch and cases to make +, ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    22

    Post Trying to loop my program or reset it Dev c++

    I just started c++ yesterday and i'm trying to make a calculator.
    I'm using switch and cases to make +, - , *, and / and to get the answers but when i get the answer all i can make it do is close the program. How can i make the program reset whenever i need it to?

    Thanks ~GameGenie

    If it helps this is the code for the prgram....
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    //Declaring variables.
    int calc;
    int add1;
    int add2;
    int sub1;
    int sub2;
    int mul1;
    int mul2;
    int div1;
    int div2;
    
    
    //Print the selections
    cout<< "Selection 1: +\n";
    cout<< "Selection 2: -\n";
    cout<< "Selection 3: *\n";
    cout<< "Selection 4: /\n";
    
    //Get the selection.
    cin>> calc;
    
    
    //Make the selections.
    switch ( calc ) {
           case 1:
                cout<< "You have choosen to add...\n";
                cout<< "Enter the first number you wish to add:\n";
                cin>> add1;
                cout<< "Enter the second number you wish to add:\n";
                cin>> add2;
                cout<< "The answer is: " << ( add1 + add2 );     
                break;
                
           case 2:
                cout<< "You have choosen to subtract...\n";
                cout<< "Enter the first number you wish to subtract:\n";
                cin>> sub1;
                cout<< "Enter the second number you wish you subtract:\n";
                cin>> sub2;
                cout<< "The answer is: " << ( sub1 - sub2 );
                break;
                
           case 3:
                cout<< "You have choosen to multiply...\n";
                cout<< "Enter the first number you wish to multiply:\n";
                cin>> mul1;
                cout<< "Enter the second number you wish to multiply:\n";
                cin>> mul2;
                cout<< "The answer is: " << ( mul1 * mul2 ); 
                break;
                
           case 4:
                cout<< "You have choosen to divide...\n";
                cout<< "Enter the first number you wish to divide:\n";
                cin>> div1;
                cout<< "Enter the second number you wish to divide:\n";
                cin>> div2;
                cout<< "The answer is: " << ( div1 / div2 );
                break;
                
           default:
                cout<<  "Sorry, thats not one of the choices. \n";
                break;
      }
      cin.get();
      cin.ignore();
    }

  2. #2
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    use a loop.

    I won't rewrite your program for you, but you can use something like this

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(){
    
    int count = 0;
    char c;
    
    while ( cin >> c ){
    
               count++;
    
    }
    
    
    cout << "You exited the loop." << endl;
    cout << "You entered " << count << " characters." << endl;
    
    }
    
    //or even something like this
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    
    string choice = "y";
    
    while ( choice == "y" ){
    
    cout << "You are in the loop" << endl;
    
    cout << "Loop again?" << endl;
    cin >> choice;
    
    }
    
    cout << "You exited the loop" << endl;
    
    }
    This is how the above code works:

    1. The loop checks its condition to see if it's true. In this case, it's to read a character from cin.

    2. If the condition is true, it executes the code block, which in this case increments count by 1.

    3. When you input an end-of-file character into cin (ctrl+z for windows, ctrl+d for linux), or anything other than a value that is valid type for char it puts the error flag in cin. This means that you can no longer input anything into cin until you clear the stream with cin.clear() therefore invalidating the loop condition and exiting it.

    The second one is self-explanatory.

    Find some way to implement this into your code. =)
    Last edited by dra; 07-21-2005 at 02:49 AM.

  3. #3
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    just because im into game development i like to use:
    Code:
    while (!done)
    {
       ....
       cout << "play again? "; if (getch() != 'y') { done = 1; }
    }
    what does signature stand for?

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    Thanks alot for the help, but i'm still having a problem i made a function to decide if it should quit or restart. It never reads the function just skips it all together. I'll make the code shorter and post it.

    Code:
    #include "CalcHeader.h"
    
    using namespace std;
    
    int main()
    {
    //Declaring variables.
    float m1;
    float m2;
    
    c.ex = 1;
    while ( c.ex == 1 ) //This should make the program restart
    {
          
    cout<< "Restart worked!!";    
          
    menu();
    
    
    //Make the selections.
    switch ( calc ) {
           case 1:
                cout<< "You have choosen to add...\n";
                cout<< "Enter the first number you wish to add:\n";
                cin>> m1;
                cout<< "Enter the second number you wish to add:\n";
                cin>> m2;
                cout<< "The answer is: " << ( m1 + m2 ) << endl;
                system( "pause" );
                rst();
                break;
    
           default:
                cout<<  "Sorry, thats not one of the choices.\n";
                system( "pause" );
                rst();
                break;
      }
      cls();
     }
    return 0;
    }
    
    void rst()
    {     
         c.chk = 1;
         while ( c.chk == 1 )
         {
               cout<< "Press 'r' to restart or 'q' to quit." << endl;
               cin >> c.r;
               
               if ( c.r == 'r' )
                    c.ex = 1;
               else if ( c.r == 'q' )
               {
                     exit();
               }
               else 
                     cout<< "That is not 1 of the choices..." << endl;
         }
    }
    void exit()
    {
         c.ex = 0;
    }
    void menu()
    {
         cout<< "Selection 1: +" << endl;
         cout<< "Selection 2: -" << endl;
         cout<< "Selection 3: *" << endl;
         cout<< "Selection 4: /" << endl;
         cin>> c.calc;
         cls();
    }
    Ok...it's not very short.
    Last edited by GameGenie; 07-21-2005 at 11:21 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    [edit]Need coffee. My bad.[/edit]

    On an aside, your use of globals really detract from being able to follow just what's going on in your program. For one thing, you seem to have declared all of your variables in a header file, which you don't show us. For another, you should, assuming you still have some reason to even use globals, be declaring them in your .cpp file, and not in a header. You'll discover why later on when you actually need a header file for use with multiple .cpp files.

    Oh, and your indentation is horrible.

    [edit2]Consider the following:
    Code:
    int main( )
    {
        int somevar = 1;
        int choice = 0; // some invalid choice
    
        do
        {
            displaymenu( );
            choice = getchoicefromuser( );
            switch( choice )
            {
                case 'a': dovalidchoicea( ); break;
                case 'b': dovalidchoiceb( ); break;
                ... and so on ...
                case 'q': displaygoodbye( ); somevar = 0; break;
    
                default: showinvalidselectionmessage() ;
            }
        } while( somevar == 1 )
    
        return 0;
    }
    [/edit2]

    Quzah.
    Last edited by quzah; 07-21-2005 at 11:44 AM.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    Sorry, i forgot to put the header file, i put the variables like that because i just wanted to try it. I'm trying to learn c++ this is like my third day...i'm sure i'll figure all the perfect stuff out...hopefully.

    Code:
    #include <iostream>
    #include <windows.h>
    
    void rst();  //Restart the program
    void exit();  // Exit the program
    void menu();  //Program menu
    void cls();  //Clears the screen
    
    
    class variable
    {
    public:
           int menu; //For selecting which menu choose you want.
           char r;  //Holds 'r' or 'q' to find if you wish to quit or restart
           int chk;  //Check when you start the restart function
           int ex;  //If you press 'r' to restart this will = 1 else it will be 0
    };
    class variable c;

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >class variable c;

    should be just

    variable c;

    Now c is a global variable of type variable in your program. To set the members of class variable objects to default values as you indicated you would like to do in your comments, you would use a constructor, if you haven't left it out of the post for brevities sake. There are several other errors as well, but I won't go there as, IMO, learning how to use control loops and classes simultaneously is a recipe for disaster. Your enthusiasm is understandable, but you really will learn more, faster by focusing on one topic at a time.
    You're only born perfect.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    >class variable c;

    should be just

    variable c;
    Same thing . . . assuming variable is a class.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    Ok, thanks for the help everyone.

    [edit] I finally figured it out..i was trying to hard to figure it out it was simple. I just changed the variable in each of my cases.

    Code:
    loop = 1;
    
    while ( loop == 1 ) 
    {
        switch ( loop )
        {
            case 1:
                //keep on looping...
                loop = 1;
            break;
    
            case 2:
                //Exit the loop.
                loop = 0;
            break;
         }
    }
    It even works with my class variables. [/edit]
    Last edited by GameGenie; 07-21-2005 at 02:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use FILE, loop, and if-else Program
    By Cyberman86 in forum C Programming
    Replies: 5
    Last Post: 04-01-2009, 03:14 PM
  2. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  3. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  4. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 05:06 PM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21