Overload >> operator using both character and integer

Hi all,

I'm trying to overload my >> operator for a dynamic array class. My code works, but I would like to alter it so that my function can accept another type (such as a character or string like "stop") to act as a way to terminate the function. Right now, I'm using the value -1 to stop the loop from accepting new integers, but would like to use a character or string instead. Any help is greatly appreciated. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
istream& operator>>(istream& in_stream, Chain & input) {
    //in progress
    //char bracket; could I define a char variable and place it in the loop, 
   //so that if a character is entered, the loop will terminate?

    int temp_input;
        while (in_stream >> temp_input && temp_input != -1) {

            input.Add(temp_input);
        }

    return in_stream;
}
Last edited on
When (in which situations) does the first part of the condition (in_stream >> temp_input) on line 7 fail?
I'm trying to overload my cin >> operator


There is no "cin >>" operator, "cin" is an object of type istream, and the istream class has a member input operator "std::istream::operator>>". The global input operator (std::operator>> (istream)) is also available with overloads for various types of input.

What you want to do is overload the input operator ">>".

I'm honestly not trying to be pedantic douche-bag just for the sake of it, I'm one of those guys who used to talk about "the cin operator" meaning ">>", and I suffered for it multiple times. You certainly don't want to use that phrase in an exam or a job interview.
Last edited on
Thank you for the replies.

@keskiverto in_stream >> temp_input doesn't necessarily fail in my case, but if I wanted to have my array accept negative numbers in addition to positive numbers, then I would not be able to use -1 to stop the loop since it could be a potential item the user might put in the array.

@tipaye No harm done. Would I have to change the signature of my function in order to implement the global input operator? I've never actually implemented it before, so I'm unsure as to how I would overload the various types of inputs?
I tried tweaking my code a little bit and came up with these:

1
2
3
4
5
6
7
8
9
10
11
istream& operator>>(istream& in_stream, Chain & input) {
    //in progress
    string stop;

    int temp_input;
        while (in_stream >> temp_input && stop != "stop") {
            input.Add(temp_input);
        }

    return in_stream;
}


1
2
3
4
5
6
7
8
9
10
11
istream& operator>>(istream& in_stream, Chain & input) {
    //in progress
    char letter;

    int temp_input;
        while (in_stream >> temp_input && letter != 'a') {
            input.Add(temp_input);
        }

    return in_stream;
}


Both work like a charm, but could they potentially cause errors with certain integer inputs? Thanks.
Last edited on
It appears what you're doing is overloading the global >> operator.
The alternative would be to make it a member of Chain, in which case, as you said the signature will change, since you would then only need to pass the istream &.
I could be wrong, but I think overloading the non-member form as you've done is the preferred way.
In your latest code, I don't see where you're storing anything in "stop" or "letter"
I would be tempted to just read a string and then process it, also I'm never sure about precedence rules, so parentheses:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
istream& operator>>(istream& in_stream, Chain & input) 
{
	string stop;
	int temp_input = 0;	
	while ((in_stream >> stop) && (stop != "stop"))
	{
		try
		{
			temp_input = stoi(stop);
			input.Add(temp_input);
		}
		catch(const invalid_argument &ia)
		{
			cerr << "Invalid argument: " << ia.what() << endl;
		}
		catch(const out_of_range &oor)
		{
			cerr << "Out of Range error: " << oor.what() << endl;
		}
	}

	return in_stream;
}


I'm just trying to be fancy with the try-catch block, i'm not sure it makes sense for what you're doing, and I've typed this without verification so, take with a pinch of salt

Addendum/Errata: I may have programmed an infinite loop up there, you'll need to break out of the while loop from within the catch statements; otherwise, you'd be stuck in the loop unless instream >> fails or the user types "stop"
Last edited on
@keskiverto in_stream >> temp_input doesn't necessarily fail in my case

Yes it will. Just give "stop" as input. (Actually, anything that does not convert to int.)
The input will fail and the loop will stop.

Naturally, you would have to clear the failbit and discard the offending text from the in_stream before returning from the function.
Thanks again for the replies. I've decided just to go with using a string to store the numbers and then use atoi to convert the string to an int. Just to be sure, would this cause any errors in the long run? Also, I'm pretty sure this is obvious, but would I need to put #include <string> at the top of my .cpp file if I am to use atoi? Thanks again.

1
2
3
4
5
6
7
8
9
10
11
12
istream& operator>>(istream& in_stream, Chain & input) {
    //in progress
    string user_input; //variable to stop the loop
    
    cout << "Enter numbers one by one. Type 'stop' to stop " << endl;
    
    while (in_stream >> user_input && user_input != "stop") {
        int item = atoi(user_input.c_str());
        input.Add(item);
    }
    return in_stream;
}
That's actually what I've done above, except I used stoi, not atoi.

You need #include <string> for using string
You need #include <cstdlib> for using atoi()
Great. Thanks everyone!
Topic archived. No new replies allowed.