Sign in to follow this  
NUCLEAR RABBIT

Help Stop Closing Window

Recommended Posts

Hello, I created this little program to practice a little of what I've learned about vecotrs and the STL. The problem is, after I input my Selection, the results are shown for a split second, and the window closes right away. I know ways of stopping this, such as 'system("pause")' and 'getch()', but i want to create my own Exit message, so I used 'cin.get()' because I just learned this method of stopping the window from closing. I used it in a program before this, and it worked perfectly, but for some odd reason, its not working for this one. + Thank you for Any Help. Source Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int main()
{

	int counter = 0;
	int dF;

	vector<string> friends;
	friends.push_back("Jimmy");
	friends.push_back("Roger");
	friends.push_back("Doug");
	friends.push_back("Patty");
	friends.push_back("Skeeter");

	vector<string>::iterator iter;
	vector<string>::const_iterator cIter;

	cout << "Friends List:\n"
	     << "Please Select which friend to delete.\n\n";
	
        for(cIter = friends.begin(); cIter != friends.end(); ++cIter)
	{
		counter++;
		cout << counter << ": " << *cIter << endl;
	}

	cout << "\nDelete Selection: ";
	cin >> dF;

	friends.erase(friends.begin() + dF - 1);
	counter = 0;

	cout << "\nNew Friends List:\n\n";
	for(cIter = friends.begin(); cIter != friends.end(); cIter++)
	{
		counter++;
		cout << counter << ": " << *cIter << endl;
	}

	cout << endl << endl;
	cout << "Press Enter To Exit";
	cin.get();

	return 0;
}

Share this post


Link to post
Share on other sites
Besides what Fred said,

There are probably characters left over in the stdin buffer. You can try to use ignore() to discard the characters.

Maybe the newline stays in the buffer, but I'm not sure, i.e. you are entering an integer like so:


Enter a number: 1849<ENTER>


now the contents of stdin are:


these are valid numbers
| | | |
v v v v
0x31 0x38 0x34 0x39 0x0d 0x0a
^
|
this is white space


So the first four characters are removed from the buffer and transformed into an integer for your program. Now stdin may still have two characters in the buffer.

Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
Besides what Fred said,

There are probably characters left over in the stdin buffer. You can try to use ignore() to discard the characters.

Maybe the newline stays in the buffer, but I'm not sure, i.e. you are entering an integer like so:


Enter a number: 1849<ENTER>


now the contents of stdin are:


these are valid numbers
| | | |
v v v v
0x31 0x38 0x34 0x39 0x0d 0x0a
^
|
this is white space


So the first four characters are removed from the buffer and transformed into an integer for your program. Now stdin may still have two characters in the buffer.


so when the buffer clears, it only clears 4 numbers?

P.S - Thanks for all the help, everyone.

Share this post


Link to post
Share on other sites
By "valid numbers" he meant "valid digits". When you properly clear the input, it gets cleared completely, but your program does not do that. The last times before the end of the program that you read, you read in a float, and then .get() at the end of the program. The program does not proceed until the user types a newline after the float value, because of the console line buffering. The program reads in the float, and leaves the newline, because the extraction operators only grab as much data as they need to. That leaves the newline in place for the .get() call to pick up immediately.

Note that if the user had typed some garbage after the float value, then *that* could get picked up instead. And if the user doesn't type something interpretable as a float at all, then even worse things happen.

I will quote for emphasis:

Quote:

The proper way is to run your program from the console.


(And as for handling bad input more robustly, see here. But don't use char*'s for text, please. In particular, use the free function std::getline(), and not the stream member function .getline().)

Share this post


Link to post
Share on other sites

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