"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.