|
-
April 5th, 2011, 08:22 PM
#1
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?
-
April 5th, 2011, 08:38 PM
#2
Re: Problem with re-running a program
 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 )
-
April 5th, 2011, 08:40 PM
#3
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.
-
April 5th, 2011, 08:43 PM
#4
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>
-
April 5th, 2011, 08:43 PM
#5
Re: Problem with re-running a program
 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?
-
April 5th, 2011, 08:45 PM
#6
Re: Problem with re-running a program
 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.
-
April 5th, 2011, 08:48 PM
#7
Re: Problem with re-running a program
 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.
-
April 5th, 2011, 09:13 PM
#8
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.
-
April 6th, 2011, 07:24 AM
#9
Re: Problem with re-running a program
 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.
-
April 6th, 2011, 08:56 AM
#10
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.
-
April 6th, 2011, 09:13 AM
#11
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|