Problem with re-running a program
I like to put a validation loop around my class assignments that asks the user if they would like to run the program, then ask again if they'd like to re-run the program. However, while working on an assignment, I noticed that my loop works for everything I've written before - but not strings. WHY!?!? Here are two examples of what I'm talking about... first is my loop that works:
Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char rerun;
cout << "Would you like to run this program? (y/n): ";
cin >> rerun;
while (rerun == 'y' ||rerun == 'Y')
{
char test[81];
cout << "Type something: ";
cin >> test;
cout << "You typed : " << test << endl;
cout << "Would you like to run this program again? (y/n): ";
cin >> rerun;
}
return 0;
}
Now, here is what I was TRYING to do - getline a string:
Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char rerun;
cout << "Would you like to run this program? (y/n): ";
cin >> rerun;
while (rerun == 'y' ||rerun == 'Y')
{
string test;
cout << "Type something: ";
getline(cin, test);
cout << "You typed : " << test << endl;
cout << "Would you like to run this program again? (y/n): ";
cin >> rerun;
}
return 0;
}
When I run my program and answer the first question with 'y', it does this:
Would you like to run this program (y/n)?: y
Type something: You typed:
Would you like to run this program again (y/n)?:
Press any key to continue...
Why doesn't this second program run?
Re: Problem with re-running a program
Quote:
Originally Posted by
LogicWavelength
Code:
string test;
cout << "Type something: ";
getline(cin, test);
wait, shouldnt it be:
Code:
string test;
cout << "Type something: ";
cin.getline(test, SIZE);
where SIZE=whatever size you want the string to be?
Check out this link: http://www.cplusplus.com/reference/i...tream/getline/
(PS: you might want to add some \n and/or << endl to clean it up a bit :) )
Re: Problem with re-running a program
Why would you ask the user if they want to run the program? The fact that it's running ought to indicate that they do. From a design perspective, that's kind of goofy.
Re: Problem with re-running a program
This is a common problem when mixing operator >> and getline().
consider :
Code:
char c;
string s;
cin >> c;
getline(cin,s);
The "cin >> c" gets the next character from the input stream.
The "return ('\n')" that the user enters is still in the input stream.
getline() starts with the current position in the input stream and
reads until it a '\n' is encountered. However, the '\n' is still
in the buffer from the "cin >> c".
Code:
cin >> c;
cin.ignore(1000,'\n'); // add this line to get rid of the new-line character
getline(cin,s);
Usually you do not hardwire a length such as 1000 ...
Code:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // #include <limits>
Re: Problem with re-running a program
Quote:
Originally Posted by
GCDEF
Why would you ask the user if they want to run the program? The fact that it's running ought to indicate that they do. From a design perspective, that's kind of goofy.
well, i did when i first started programming, pretty much for extra practice, and i also added the rerun option after the program finished.
--maybe that's why?
Re: Problem with re-running a program
Quote:
Originally Posted by
iiSoMeGuY 7x
well, i did when i first started programming, pretty much for extra practice, and i also added the rerun option after the program finished.
--maybe that's why?
Maybe it's time to rethink that. Run it once, then when it's done, ask if they want to rerun it.
Re: Problem with re-running a program
Quote:
Originally Posted by
GCDEF
Maybe it's time to rethink that. Run it once, then when it's done, ask if they want to rerun it.
lol, i know. i didnt use it for programs i expected other people to use, just for practice programs i was experimenting with or whatever i was doing at the time. i dont do it anymore, just when i first started since i self-taught myself and needed the extra practice.
Re: Problem with re-running a program
I think what he is getting at is that it should run once, and then after all that is done, ask if you'd like to rerun. Basically instead of having the ask logic at the beginning of the loop, put it at the end. Initialize the rerun variable to true or 'y', etc so that it automatically enters the loop the first time. Or, use a do while loop.
Re: Problem with re-running a program
Quote:
Originally Posted by
Alterah
I think what he is getting at is that it should run once, and then after all that is done, ask if you'd like to rerun. Basically instead of having the ask logic at the beginning of the loop, put it at the end. Initialize the rerun variable to true or 'y', etc so that it automatically enters the loop the first time. Or, use a do while loop.
Exactly. Thank you.
Re: Problem with re-running a program
I actually had that realization in a dream last night. Why am I asking them if the want to run it? "They" opened my compiler and ran it. I am going to move the logic to the end only.
Re: Problem with re-running a program
I actually had that realization in a dream last night. Why am I asking them if the want to run it? "They" opened my compiler and ran it. I am going to move the logic to the end only.
**EDIT**
I simply can't mix cin >> and getline. I converted my loop to run on strings. Here it is:
Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string rerun = "y";
while (rerun == "y" || rerun == "Y")
{
string test;
cout << "Type something: ";
getline(cin, test);
cout << "You typed : " << test << endl;
bool valid = false;
while (valid == false)
{
cout << "Would you like to run this program again? (y/n): ";
getline(cin, rerun);
if (rerun == "y" || rerun == "Y" || rerun == "n" || rerun == "N")
valid = true;
}
}
return 0;
}