-
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;
}
-
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!
-
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";
-
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);
-
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.
-
So would I use your_name.clear(); ?
-
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.
-
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.
-
Wouldn't hurt to format the code properly either: IP Banned - GIDNetwork