Thread: Repeat this!

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    7

    Question Repeat this!

    If I have a do-while loop like this below: and then what follows, how would I repeat the whole shabang?

    Code:
    #include <iostream>
    # include <time.h>
    using namespace std
    ;int main()
    {int maximum;
    int minimum;
    int random_number;
    int req_subtraction;
    int current_total;
    int total;
    cout << "Total Limit?" << endl;   //can't go over this
    cin >> total;
    
    cout << "Max you can add?" << endl;
    cin >> maximum;
    
    cout << "Minimum you can add?" << endl;
    cin >> minimum; 
    
    cout << "Required Subtraction?" << endl;
    cin >> req_subtraction;
    
     srand(time(0));  //seed the random number
    
    int high=maximum;
    int low=minimum;
    
    do
    {random_number = rand () % (high - low + 1) + low;//random number between max and min
    
    cout << "Random Number is " << random_number << endl;
    current_total += random_number - req_subtraction;    
    if(current_total < 0)   //make so negatives equal out to zero    
    {    current_total = 0;    }
     cout << "The current total is " << current_total << endl;
    }
    while (current_total < total); 
    system("pause");
    return 0;
    }
    and then I say this

    Code:
    cout <<"Would you like to try again? (yes/no)" << endl;
     cin >> yes_no;
    if (yes_no == "yes")
    {   
     //repeat somehow?
    }
    else
    {    
    cout <<"Goodbye!" <<endl;
    }
    Last edited by skittlesaddict; 10-02-2012 at 07:31 PM.

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    The simplest way (if you don't want to change too much code) is to just set what's called a sentinel value. Essentially, you want to put all the code you want repeated inside a giant while loop that is something like
    Code:
    while(sentinel == 0)
    . Inside that if check, just have sentinel's value be changed accordingly. If sentinel is no longer 0, the program will terminate. So you might want this:
    Code:
    cout <<"Would you like to try again? (yes/no)" << endl;
     cin >> yes_no;
    if (yes_no == "yes")
    {   
     sentinel = 1;
    }
    else
    {    
    cout <<"Goodbye!" <<endl;
    }
    Of course, remember to declare and initialize the "sentinel" value correctly!
    Last edited by synhyborex; 10-02-2012 at 08:26 PM.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That's not what I would call a "sentinel". It's just a stinky old useless flag. Phew! Actually, the simplest way would be:
    Code:
    do
    {
        //...
        cout << "Would you like to try again? (yes/no)\n";
        cin >> yes_no; 
    } while (yes_no == "yes");
    cout << "Goodbye.\n";
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    7
    when I do that and it repeats it ignores one of my variables.

    I ask them to enter their name and it just puts the cout and skips the cin. It works fine the first instance though.

    Code:
    string your_name;
    
    cout <<"What is your name?" << endl;
       getline(cin,your_name);

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hint: the use of formatted I/O operator>> to read an integer leaves the newline from entering the integer text in the input buffer. getline will then read until that newline instead of the next line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    7
    So would I use your_name.clear(); ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No. That clear member function clears the error state of the I/O stream. In this case, the stream is still in a good state, so there's no point using that function. Rather, you want to ignore() characters still in the stream until the newline character. Thus, when getline is called, it will begin with the characters on the next line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You should include the cstdlib header if you are using the rand/srand/system functions. Wouldn't hurt to replace time.h with ctime as well.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wouldn't hurt to format the code properly either: IP Banned - GIDNetwork
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The loop does not repeat itself! Why?
    By rutgersmis in forum C Programming
    Replies: 5
    Last Post: 10-11-2011, 06:43 AM
  2. How to repeat program in C
    By i6472 in forum C Programming
    Replies: 2
    Last Post: 04-01-2010, 05:59 PM
  3. How to repeat a line
    By Megamanenm in forum Game Programming
    Replies: 6
    Last Post: 01-11-2009, 03:41 PM
  4. repeat until any key
    By jatoo in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 03:55 AM
  5. repeat
    By linuxdude in forum C Programming
    Replies: 4
    Last Post: 03-28-2003, 09:40 AM