Not executing if statement? Unexpected results.

I was reading and learning c++ from the book "Programming principles and practice using c++, 2nd ed" and i ran into a bit of code (first block one below) and couldn't really understand the while (cin>>current) bit (I was under the impression while statement required a boolean statment [unless it is a boolean statment and I'm just not seeing it]) so in an effort to try and understand the expression I put some pring statements in some places that i thought would help (second block down below). Now the program doesn't do anything I can understand. When i give input, it just prints it back to me and asks for more input, but when i input more words or characters separated by spaces it moves the output it parrots form me a space to the right equal to the number of total spaces in the string i inputed. I am having a real hard time understanding what is going on, any help would be appreciated!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  
#import "std_lib_facilities.h"

int main()
{
	string previous = " ";
	string current;
	while (cin>>current)
		{
		if (previous == current)
			cout << "Repeated word: " << current << endl;
		previous = current;
		}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import "std_lib_facilities.h"

int main()
{
	string previous = " ";
	string current;
	cout << previous << endl;
	cout << current << endl;
	while (cin>>current)
	cout << previous;
	cout << current;
		{
		if (previous == current)
			cout << "Repeated word: " << current << endl;
		previous = current;
		}
}
couldn't really understand the while (cin>>current) bit (I was under the impression while statement required a boolean statment [unless it is a boolean statment and I'm just not seeing it])

http://stackoverflow.com/questions/8117566/why-istream-object-can-be-used-as-a-bool-expression

And you've forgotten to revise the position of the brackets making up the compound statement that is the body of the while loop. Only line 10 is governed by the while loop in the second snippet of code.
Last edited on
oh my gowd, i can't believe i overlooked something so simple. Also, is there any cmd command to kill a program? To my understanding the loop cant be broken because all inputs will be considered strings. Also, thank you for the link, i did some basic googling but couldn't really find much.
You can use whatever is used to signify end of file on your command line. Some operating systems (Windows - ctrl+z) require that it be at the beginning of a new line.
ty buddy
Topic archived. No new replies allowed.