Sign in to follow this  

My program won't work!!!!

This topic is 3319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Whenever I build my program it has no errors, but when I try to run it, it doesn't show the get line part. I'm new, so if anyone can give help, I'd be really greatful. Here's the code: IF YOU WANT TO SEE WHAT I MEAN COPY THE PROGRAM WITHOUT THIS AND BUILD/RUN IT. IF YOU SOLVE IT PLEASE REPLY. (CAPITALS DURING THE PROGRAM ARE THINGS I PUT IN TO HELP YOU, DON'T COPY THEM) #include <iostream> #include <string> #include <istream> #include <ostream> using namespace std; using std::getline; int main() { //welcoming the user std::cout << "Welcome to the Crazy News Network!" << endl; // start of questions (might not work,if bugged check here second) std::string userName; std::cout << " Please type in your first name." << endl; std::cin >> userName; int smallNumber; cout << "How many sibiling do you have?" << endl; cin >> smallNumber; float largeNumber; cout << "How much money would you like to earn per year?" << endl; cin >> largeNumber; std::string color; std::cout << "What is your least favorite color?" << endl; std::cin >> color; //start of "getline" functions, will really get messed up, check first std::string veggie; std::cout << "Which vegetable do you think has the weirdest shape?" << endl; getline (cin,veggie); THIS IS THE PART THAT DOESNT WORK cout << veggie << endl; return 0; }

Share this post


Link to post
Share on other sites
mpipe, that was uncalled for.

Add this line after cin>>color;

cin.ignore();


The reason this is needed is because cin>>color is grabbing the integer and ignoring the newline entered when you press return. So getline sees that and thinks you're done entering the line. cin.ignore gets rid of that newline character.

Share this post


Link to post
Share on other sites
ulxlx71 - when dealing with keyboard input, this is why it is normally a pretty good idea to read a whole line of input at a time and the process the input in a stringstream:

A rough example:


template<class T> bool get_input(T &t)
{
std::string s;
std::getline(std::cin,s);

std::istringstream is(s);

T t;
is >> t;

return !is.fail();
}

void f()
{
std::cout << "What is your first name?\n";
std::string first;

get_input(first);

std::cout << "How old are you?\n";
int age;

while(!get_input(age)) std::cout << "Please enter a valid number\n";
}



This way, not only is an entire line of input consumed each time, but it makes it easier to catch conversion errors, like with the "age" example above.

And you never need to std::cin.ignore() again.

Share this post


Link to post
Share on other sites

This topic is 3319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this