Using If commands to reset program

This is a discussion on Using If commands to reset program within the C++ Programming forums, part of the General Programming Boards category; Hey im 16 and a beginner to C++ just trying to learn it on my own to get rdy for ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    22

    Using If commands to reset program

    Hey im 16 and a beginner to C++ just trying to learn it on my own to get rdy for college. Now i m writing a math quiz (lol) for exercise and im wondering how to use if commands to reset/ or continue with program. For example like this

    Code:
    cout<<"Are you ready to begin the test? Yes/No:\n";
      if ( Yes ) {
     continue with program    }
      
      if ( No ) {
      reset program }
    Its a little vague and wrong but i think you guys should know what im talking about

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Well here is an example
    Bare in mind this not a full program

    Code:
    cout << "A re you ready to begin the test? y/n: ";
    cin >> answer;
    
    if ( answer == 'y' )
    {
       cout << "Lets go!"; // test code here
    }
    
    else
    {
       cout << "\nOk, we'l quit...";
    }
    You could also use a loop, to keep asking the question untill valid input is detected
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    How about you pick up a book or look at tutorials about basic C++?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    22
    Im looking at tutorials of C++ trying to learn it. also swgh I know how to loop number but how would u loop those If statements? sry for acting like a newb lol

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Put it in a while loop.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    You can use an sentenial value. A value which is invalid, like -1;

    Code:
    cout << "Enter a number ( Enter -1 to quit ): ";
    cin >> num;
    
    while ( num != -1 )
    {
       cout << "Thanks for the number!" << endl;
    
       Enter a number ( Enter -1 ti quit ): ";
       cin >> num;
    } // break loop if -1 is used
    
    cout << "You entered: " << num << endl;
    I'm just trying to be a better person - My Name Is Earl

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    22
    this my code so far. it keeps giving me an error when i get to cin >> num part any ideas? what am I doing wrong

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    { 
      cout<<"Welcome to Kevin's Math quiz\n";
      char name[50];
      char lastname[50];
      char fullname[100];
    
      cout<<"Please enter your name: ";
      cin.getline ( name, 50 );
      cout<<"Enter your last name: ";
      cin.getline ( lastname, 50 );
      fullname[0] = '\0';            // strcat searches for '\0' to cat after
      strcat ( fullname, name );     // Copy name into full name
      strcat ( fullname, " " );      // We want to separate the names by a space
      strcat ( fullname, lastname ); // Copy lastname onto the end of fullname
      cout<<"Welcome "<< fullname <<"\n";
      cout<<"Are you ready to begin the test? ( Enter 1 if your not rdy ):\n";
      cin>> num;
      
      while ( num == 1 )
      {
         cout << "Thank You!" << endl;
        
        Are you ready to begin the test? ( Enter 1 if your not rdy )
        cin >> num
    }
        cout << "You entered: " << num << endl;
      cin.get(); 
    }

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    where is declaration of num?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    22
    you mean like int num?
    I tryed that but it gave me same error

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    This won't work:
    Code:
    while ( num == 1 )
    You want this:
    Code:
    while ( num != 1 )
    EDIT: And yes, declare int num with the rest of your variables.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Kevineze
    you mean like int num?
    I tryed that but it gave me same error
    And what error exactly?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    >You want this:
    >while ( num != 1 )

    That is identical to my while loop example, wow!
    I'm just trying to be a better person - My Name Is Earl

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Oh, and try to use C++ strings instead of C style character arrays

    Code:
    string firstName;
    string lastName;
    
    read them with getline
    
    getline ( cin, lastName );
    I'm just trying to be a better person - My Name Is Earl

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    22
    ok the loop worked i declared the num variable and made some other changes this is what i did. it shuts the program if u dont press 1 cuz of the cin.get() but im not done wit program yet check this out

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    { 
      cout<<"Welcome to Kevin's Math quiz\n";
      char name[50];
      char lastname[50];
      char fullname[100];
    
      cout<<"Please enter your name: ";
      cin.getline ( name, 50 );
      cout<<"Enter your last name: ";
      cin.getline ( lastname, 50 );
      fullname[0] = '\0';            // strcat searches for '\0' to cat after
      strcat ( fullname, name );     // Copy name into full name
      strcat ( fullname, " " );      // We want to separate the names by a space
      strcat ( fullname, lastname ); // Copy lastname onto the end of fullname
      cout<<"Welcome "<< fullname <<"\n";
      cout<<"Are you ready to begin the test? ( Enter 1 if your not rdy ):\n";
      int num;
      cin>> num;
      
      while ( num == 1 )
      {
        
        cout<<"Are you ready to begin the test? ( Enter 1 if your not rdy ):\n";
        cin >> num;
    }
        cout << "You entered: " << num << endl;
      cin.get(); 
    }

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Despite the double post of the problem with this code (swgh's original and my miss of swgh's original), you still have not fixed it:
    Code:
      while ( num == 1 )
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  5. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM

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