How to stop user input with a different variable type?

Sep 17, 2012 at 11:14pm
Hello everyone! I am having trouble stopping user input with a different variable type. In my case, the user input are numbers, but the problem specifies that I should stop the input stream with a '|' character. Below is my code with comments:

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
#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";
    while(cin >> number)
    {
       //if( "something" == '|')
        //{
        //    break;
        //}
        // Here for the "something", can I use the getline function? I tried cin.getline(number,1), but unfortunately,
        // the character '|' is not an integer type. Could you help me please? Thank you!

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


So what should I do to use '|' instead of an integer to stop the user input? Thank you in advance for your help!

Sincerely,
gta100
Sep 17, 2012 at 11:26pm
In addition, I also tried cin.peek() for "something", but unfortunately the exe file crashed after I typed the character '|'. Thank you.

gta100
Sep 17, 2012 at 11:41pm
peek() would be good.
1
2
3
4
while (cin.peek() != '|')
{
  // collect numbers
}


However, this still does not catch if you enter a non-number and non-"|".

What was the code you used there?
Sep 18, 2012 at 12:16am
Hi LowestOne! Thank you for your reply.
I used an if statement instead of a while loop like yours for my cin.peek( ) function. My code was:

1
2
3
4
 if( cin.peek()== '|')
        {
            break;
        }


Like I said, the program crashed after I typed '|'. So what do you think? I appreciate your further attention.

gta100
Sep 18, 2012 at 12:19am
I also tried your while loop approach just now, but the program does not allow me to enter a second number after I typed my first one, and the program is literally dead.

gta100
Sep 18, 2012 at 12:46am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::vector<int> read_numbers_till( char end_with = '|' )
{
    std::vector<int> result ;
    std::cout << " enter some numbers (enter a " << end_with <<  " to stop):\n" ;

    int n ;
    while( std::cout << "number? " )
    {
        if( std::cin >> n ) result.push_back(n) ;

        else
        {
            std::cin.clear() ; // clear the error state
            if( std::cin.get() == end_with ) break ;
            std::cout << "error in input \n" ;
            std::cin.ignore( 1000, '\n' ) ; // clean input buffer
        }
    }
    return result ;
}
Sep 18, 2012 at 1:02am
Hi JLBorges! Thank you for your reply. Could you also give your advice on what I should write to replace the "something" in my if statement? Thank you! I tried cin.get(), cin.peek(), but they all made my program crash after I entered the '|'. I don't know what to do...
Sep 18, 2012 at 2:07am
I'll get input to a string buffer and determine if that string buffer is a number, or contains '|' char or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while (true)
{
    char inputBuffer[20];
    cout << "Enter a number ( | to stop): ";
    cin >> inputBuffer;

    if (isint(inputBuffer))
    {
        int tempNumber = atoi(inputBuffer);  //include stdlib.h if you can't use atoi()
        //append tempNumber to vector...
    }
    else if (hasStopChar(inputBuffer, '|'))
    {
        break;
    }
    //input is not a number and doesn't have stop char -- nothing to do here...
}


1
2
3
4
5
6
7
8
bool isint(const char* str)
{
    for (const char* ptr = str; *ptr; ++ptr)
    {
        if (!isdigit(*ptr)) return false;  //include ctype.h if you can't use isdigit()
    }
    return true;
}


1
2
3
4
5
6
7
8
bool hasStopChar(const char* str, char stopChr)
{
    for (const char* ptr = str; *ptr; ++ptr)
    {
        if (*ptr == stopChr) return true;
    }
    return false;
}

Sep 18, 2012 at 2:56am
Thank you! I tried cin.get(), cin.peek(), but they all made my program crash after I entered the '|'. I don't know what to do...


Line 13 and 16 are very important in JLBorges' code. It looks like it would work fine to me.
Sep 18, 2012 at 3:01am
Hello LowestOne and tntxtxt! Thank you for your replies!

I am just a beginner in C++ so is there a simpler way of doing this? Actually I just looked up the terminologies. I think my problem is that I want to specify my very own "End-Of-File (EOF)" operator ( the '|' character in this case ) other than the default Ctrl + Z command in Windows. Hopefully there is a simpler way? I appreciate your further attention!

gta100
Sep 18, 2012 at 2:34pm
> I think my problem is that I want to specify my very own "End-Of-File (EOF)" operator
> ( the '|' character in this case ) other than the default Ctrl + Z command in Windows.
> Hopefully there is a simpler way?

AFAIK, there is no simple way to do that.
Topic archived. No new replies allowed.