Two input streams into a function

Very noob question: Two input streams and one output stream. It reads the characters from each of the two inputs and compares them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
using namespace std;

void Compare(istream &input1, istream &input2, ostream &output){
    char ch1, ch2;
    while(input1.get(ch1) && input2.get(ch2)){
        if(ch1 != ch2)
            output << "not equal" << endl;
        return;
    }
    if(!ch1 && !input2.get(ch2))
        output << "equal" << endl;
    else
        output << "not equal" << endl;
}

int main(int argc, const char * argv[])
{      
    Compare(cin, cin, cout);
    return 0;
}

Texteditor is xcode in Mac. It could compile but as soon as I put in a character and pressed 'enter', 'not equal' popped out.


Last edited on
> as soon as I put in a character and pressed 'enter', 'not equal' popped out.
you are extracting `Enter' as `ch2'
What I was expecting was that when I put in two different characters the code would stop and print out 'not equal'. But what really happened was it wouldn't stop until I pressed 'Enter' and then it showed 'not equal'.

I was wondering if there was something wrong with my understanding of I/O stream. For example, I tried the following code,

1
2
3
4
5
6
7
8
9
10
11
12
13
void Test(istream &input, ostream &output){
    char ch;
    output << "Get started:" << endl;
    while(input.get(ch))
        output << "go on!" << endl;
}


int main(int argc, const char * argv[])
{
    Test(cin, cout);
    return 0;
}


The output looked like this:
Get started:
a
go on!
go on!
b
go on!
go on!
a
go on!
go on!


How come it had 'go on' printed out twice?
Compare(cin, cin, cout);

input1 and input2 are not two streams in this case: they are references (additional names, aliases) to the same std::cin.

while(input1.get(ch1) && input2.get(ch2))

So here you're asking to read a character from std::cin and store it in ch1 and then to read the next character from std::cin and store it in ch2.
I think I understand that. But pardon my ignorance, how is that a problem? I'm asking it to read a character from cin and put it in ch1 and read another one and put it in ch2 and so on and so forth, where every time it reads a character, I need to put in a character for it to read. Is that understanding mistaken?
So what is your expected input and what is your expected output?

If your expected input is a<Enter>a<Enter>b<Enter>b<Enter>, then that line has to be more like while(input1 >> ch && input2 >> ch2). If your expected input is aabbccddeeff<EOF>, then you're fine for that while() statement (although the purpose of the subsequent if(!ch1... is unclear)
"It reads the characters from each of the two inputs and compares them."
For example, if I try to compare 'abc' and 'abc', what I'll do is to type in a<Enter>a<Enter>b<Enter>b<Enter>c<Enter>c<Enter>\0<Enter>\0<Enter>, and it should print out 'equal', otherwise 'not equal'. But what it actually does is that it holds out printing until I press <Enter> and then it prints out 'not equal' every time.

Also, I remember that istream::get(char& c) extracts a single character from the stream, whereas istream& operator>> extracts an input stream each time.

On an extra note, let me explain my code if it helps.
1
2
3
4
5
    while(input1.get(ch1) && input2.get(ch2)){
        if(ch1 != ch2)
            output << "not equal" << endl;
        return;
    }

This loop compares two strings by comparing every character and prints out 'not equal' if it meets two characters at the same position that aren't equal.

1
2
3
4
    if(!ch1 && !input2.get(ch2))
        output << "equal" << endl;
    else
        output << "not equal" << endl;


Now, if I end the first string, then while(input1.get(ch1) && input2.get(ch2)) terminates ( Input1 will still get one character but input2 won't since c++ will ignore the second part if the first is false.) Then input2 will get one character from the 'if' condition and if it also indicates the end of second string, then the code will print out 'equal'. Otherwise it will print out 'not equal', which involves two scenarios - one, the first string ends in 'while' and the second string doesn't end in 'if'; two, the first string doesn't end in 'while' but the second one does, in which case it will also print out 'not equal' from 'if ... else'.

Hopefully I made it clear.

Last edited on
Topic archived. No new replies allowed.