Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Help with a very simple C++ program?
I'm a 3rd semester programming student but I'm having a total blonde moment (plus c++ isn't my best language).. SO DON"T LAUGH!
All i need to do is a little mock-up for a future project.. I have the base of the mockup... display a title... let the user enter something, return a value (for display only, just a random number). Let the user continue to enter strings... but terminate the program when a blank string is entered (if the user hits enter).
Here is what i have, i just can't get it to exit on a blank line... is there a better way to do this? wtf am i missing, seriously, i have no idea why i can't get this stupid thing!
Help?
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Expression Evaluator V1.0" << endl << endl;
string mockIn;
unsigned ct = 0;
do
{
mockIn = "";
cout << ">>";
cin >> mockIn;
cout << "[" << ct << "]" << " = 4321 " << endl;
ct++;
}while (mockIn != "");
return 0;
}
You have no idea how much of an idiot I feel like, not being able to get this :S
Perfect, Michael! I knew it would be something stupid like that.... suprisingly, I've never used getLine before :S but it worked, of course!
2 Answers
- 1 decade agoFavorite Answer
instead of cin >> mockln
try getline(cin, mockln)
- ErinLv 41 decade ago
Use some other string to mean end.
Cin just doesn't work that way and will keep looking for an actual string - you can work around that, but it is probably beyond the type of code you are looking for. Just do while (mockln != "EXIT") or something.