|
Hello I am having trouble getting a program that I wrote to work correctly. The code worked when I used ,cin >> input, to get the string but I could not use spaces. So I tried getline(cin, input). Well it works but once it is put inside my if statement it wont run correctly. So here is the piece of my code that seems to be messing up.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int ans;
int even;
string output;
cout << "Press 1 for a for loop or press 2 for a while loop: ";
cin >> ans;
if (ans==1) // if answer = 1 then do this code.
{
string input = "";
cout <<"Enter a string: \n";
getline(cin, input); // get string
cout <<"You entered " << input << endl << endl;
}
my else statement is commented out right now.
and then this is my output - it wont let me enter my string but it would when i didn't have the first cout statement and the if statement.
Press 1 for a for loop or press 2 for a while loop: 1
Enter a string:
You entered
Please help
Last edited by HulkThrash; 09-13-2009 at 02:44 PM..
|