Hello everyone! My program is supposed to get user input of numbers, and stop the user input by typing '|'. However, my program always crash after I entered the '|'. Could you help me investigate my program please? I am using the Code::Blocsk IDE with the GNU C++ compiler. Thank you very much!
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <complex>
#include <ctime>
#include <fstream>
#include <vector>
usingnamespace std;
int main()
{
vector<int>v;
int number;
int N;
int sum = 0;
cout << "Pleae enter some numbers (press '|' to stop): \n";
if(cin.get() != '|')
while(cin >> number)
{
v.push_back(number);
}
cout << "Now please enter how many numbers you wish to sum, starting from the first: \n";
cin >> N;
for(int i = 0; i<N; i++)
{
sum += v[i];
}
cout << "The sum of the first " << N << " numbers: ";
for (int j = 0; j< N-1 ; j++)
{
cout << v[j] << ", " << endl;
}
cout << "and " << v[N-1]<< " is " << sum << "." << endl;
}
When using the operator >> on cin it flushes the buffer. This means that there is no more input data from the console, so when accessing the console buffer again with the code cin.get() you are reading into a memory location that does not exist, thus the segmentation fault.
Instead of cin>>number use
if (cin.get() != '|')
cin>>number;
Hello pogrady, thank you very much for your reply!
I edited my code above, but the same problem still exists. My program still crashes after I entered '|'. I appreciate your further attention investigating this problem. Thank you!
Your while loop never had a terminating condition so what I think is happening (I'm using an iPod) is that the program goes into an infinite loop which causes it to stop responding, which is why you had said it closes but continues to run.
The abovE code just checks to make sure the input isnt equal to a pipe.
Since you can't seem to put it together from the multiple hints in the other thread, here it is spelled out in code. Please see: http://www.parashift.com/c++-faq-lite/istream-and-ignore.html and you may want to check out the rest of the links while you're there.
#include <iostream>
#include <vector>
#include <limits>
usingnamespace std;
int main()
{
vector<int>v;
int number;
int N;
int sum = 0;
cout << "Pleae enter some numbers (press '|' to stop): \n";
while ( cin >> number )
v.push_back(number) ;
// cin >> number failed, therefore cin is in an error state.
// clear the state and remove the offending input from the stream.
cin.clear() ;
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Now please enter how many numbers you wish to sum, starting from the first: \n";
// a little sanity check may be in order here:
for ( ; ; )
{
while ( cin >> N && N > v.size() )
cout << "You only entered " << v.size() << " numbers! Try again: " ;
if ( !cin )
{
cout << "That wasn't a number! Try again: " ;
cin.clear() ;
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n');
}
elsebreak ;
}
for(int i = 0; i<N; i++)
sum += v[i];
cout << "The sum of the first " << N << " numbers: ";
for (int j = 0; j< N-1 ; j++)
cout << v[j] << ", " << endl;
cout << "and " << v[N-1]<< " is " << sum << "." << endl;
}
Hello cire! Thank you very much for the code!
Could you also tell me how to use the for ( ; ; ) loop outside the "sanity check" while loop? (I don't understand what the double semicolons mean. ) I tried without it and the program stops responding after I typed some random letters like 'f' instead of a number for the variable N. Thank you very much and sorry for my poor English since I am not a native English speaker.