Jump to content
  • Advertisement
Sign in to follow this  
Ryan Atkins

Problem getting cin.get() to work...

This topic is 2341 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

I wrote this little program the 1st day I started coding and mess around with it to try out new functions I learn occasionally. I added cin.get() to the end of it, open it up outside of my compiler, and it still closes the window before I can read the final line of text. Any ideas?


#include <iostream>

int daily(int i, int h);
int travel(int t, int d);

int main()
{
using namespace std;
cout << "What is your hourly pay?" << endl;
int i;
cin >> i;
cout << "How many days is the job?" << endl;
int d;
cin >> d;
cout << "How many hours per day(including overtime)?" << endl;
int h;
cin >> h;
cout << "Finally, how much per diem do you make per day?" << endl;
int t;
cin >> t;
cout << "You make $" << daily(i, h) * d + travel(t, d) << " gross on this job." << endl;
cin.get();
return 0;
}

I also read http://www.gidnetwork.com/b-61.html and didn't want to use system("pause") based on it.

Share this post


Link to post
Share on other sites
Advertisement
add cin.ignore() before cin.get() to clear the input stream (cin>>t will leave the return/newline in the stream and cin.get will pick that up)

For serious console apps however you don't want to pause the execution in the end as it makes it harder to get it working properly with pipes or scripts, console apps are meant to run from the console.

Share this post


Link to post
Share on other sites
That looks like a nice program for your first day. When you've got it working the way you want, you might be wondering if there is any scope for improvement. One issue with your current program is that if the user enters something that isn't a number, your program doesn't notice and the user will get some rather surprising results at the end.

You can check if the user entered invalid input using code like the following:

int variable;
if(cin >> variable)
{
// User entered an integer!
}
else
{
// Something went wrong...
// We didn't give "variable" a known value, so we cannot depend on it having any particular value here.
}

A simple exercise is to ensure the user is notified if they enter invalid input, and to not continue the program if they do. A more complicated exercise is to allow the program to recover from such a mistake. If you are familiar with loops then you might want to try this.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!