Thread: Program closing early?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Program closing early?

    Hi,

    I have a problem with my small program when run closing down before it's ran through all the stages? My code is:

    Code:
    	vector<int> numbers;
    
    	cout << "Please enter a list of numbers ";
    	int temp;
    	
    	while(cin>>temp)
    		numbers.push_back(temp);
    	
    	
    	cout << "How many elements do you wish to sum? ";
    	
    	int sum;
    	cin >> sum;
    
    	system("pause");
    It lets me enter the initial numbers, however when i exit the while loop (by entering a non integer), the program then just asks me to pres any key to exit (my system pause line), however it completely misses out the 2nd cin code line? Any ideas why this is? No error messages are coming up either.

    Thanks.
    Last edited by darren78; 04-30-2010 at 10:35 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The non-numeric value encountered in the while loop terminates the loop and puts the stream into an error state whereby no further extractions from the stream will be successful. The later cin >> sum statement therefore is effectively ignored/skipped because the stream is in this errored state. You'll need to clear the error state and purge the contents of the stream (the non-numeric data) otherwise the cin >> sum line will just put the stream back into an error state since it's expecting numeric data but there is only non-numeric data present.
    Last edited by hk_mp5kpdw; 04-30-2010 at 11:38 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks for the quick response.

    I've had a look at what you have said and have found the cin.clear() and cin.ignore() member functions. Is that the best way to achieve this? I have used both as such:

    Code:
    vector<int> numbers;
    
    	cout << "Please enter a list of numbers ";
    	int temp;
    	
    	while(cin>>temp)
    		numbers.push_back(temp);
    
    	cin.clear();
    	cin.ignore();
    	
    	
    	cout << "How many elements do you wish to sum? ";
    	
    	int sum;
    	cin >> sum;
    
    	system("pause");
    It works fine now, just wondering if that's acceptable practise?

    Thanks again.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    well, ending your while loop by breaking it with a letter is kind of goofy.

    you could have user enter all the numbers on a single line and use getline, then push them into vector using a stringstream.

    for example!
    Code:
    #include <iostream>
    #include <sstream>
    #include <vector>
    
    using namespace std;
    int main()
    {
    	string str_num;
    	int current_number;
    	vector<int> numbers;
    
    	cout << "Please enter a list of numbers ";
    
    	getline(cin , str_num);
    	 // takes a complete line (discarding newline character as well)
    
    	istringstream iss(str_num); 
    	 // inserts string into a stream (you should learn streams they are useful)
    
    	// now you can use this stream just as you would cin, which is a stream itself actually
    	while(iss >> current_number)
    		 numbers.push_back(current_number);
    
    	cout << "How many elements do you wish to sum? ";
    	
    	int sum;
    	cin >> sum;
    
    	system("pause");
    
    }
    GL!

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    68

    or this

    Code:
    int i=1;
    while(i!=0){// "enter 0 to stop the loop, or some other criterion
         cin>>i;
          elements.push_back(i);
     }
    cout<<"how many elemets to add?";
    cin>>i;
    int j(0);
    for(int k(0); k<i; k++){
        j+=elements[k];
    }
    cout<<" heres the TOTAL "<<j<<" TA DA"<<endl;
    There could be array bounds checking on stuff too, but i am lazy ok?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by rodrigorules View Post
    well, ending your while loop by breaking it with a letter is kind of goofy.

    you could have user enter all the numbers on a single line and use getline, then push them into vector using a stringstream.

    for example!
    Code:
    #include <iostream>
    #include <sstream>
    #include <vector>
    
    using namespace std;
    int main()
    {
    	string str_num;
    	int current_number;
    	vector<int> numbers;
    
    	cout << "Please enter a list of numbers ";
    
    	getline(cin , str_num);
    	 // takes a complete line (discarding newline character as well)
    
    	istringstream iss(str_num); 
    	 // inserts string into a stream (you should learn streams they are useful)
    
    	// now you can use this stream just as you would cin, which is a stream itself actually
    	while(iss >> current_number)
    		 numbers.push_back(current_number);
    
    	cout << "How many elements do you wish to sum? ";
    	
    	int sum;
    	cin >> sum;
    
    	system("pause");
    
    }
    GL!
    Thanks, will have a look at streams later today. The book i'm working through hasn't got that far yet , but will definately look them up later.

    Darren.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Early program stop problem
    By bmb_ksu in forum C Programming
    Replies: 9
    Last Post: 02-19-2006, 09:24 AM
  3. exiting and closing a program
    By major_small in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2003, 08:31 PM
  4. How to restart a program without closing it...
    By D4050 in forum C++ Programming
    Replies: 16
    Last Post: 10-31-2001, 12:38 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM