Program crash with cin.get() function

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!

Here is my program below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <complex>
#include <ctime>
#include <fstream>
#include <vector>

using namespace 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;
}


Sincerely,
gta100
Last edited on
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!

Sincerely,
gta100
When you say crash you mean it stops responding. I would say get rid of the While loop:

1
2
3
4
5
 if(cin.get() != '|')
{
cin>>number;
v.pushback(number);
}


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <limits>

using namespace 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');
        }
        else
            break ;
    }

    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.

gta100
Last edited on
for ( ;; ) {} is one way of writing an infinite loop.
Topic archived. No new replies allowed.