double outputs in while

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
//On-the-fly variable definitions
#include <iostream>
using namespace std;

int main()
{
	{	//begin new scope
		int q = 0;	//C requires definitions
		//define at point of use
		for(int i = 0; i < 100; i++)
		{
			q++;	//q comes from a larger scope
			//definition at the end of the scope
			int p = 12;
		}
		int p = 1;	//a different p
	}	//end scope containing q & outer p
	cout << "Type characters:" << endl;
	while(char c = cin.get() != 'q')
	{
		cout << c << " wasn't it" << endl;
		if(char x = c == 'a' || c == 'b')
			cout << "You typed a or b" << endl;
		else
			cout << "You typed " << x << endl;
	}
	cout << "Type A, B, or C" << endl;
	switch(int i = cin.get())
	{
	case 'A': cout << "Snap" << endl; break;
	case 'B': cout << "Crackle" << endl; break;
	case 'C': cout << "Pop" << endl; break;
	default: cout << "Not A, B, or C!" << endl;
	}
}


produces the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Type characters:
a b c
☺ wasn't it
You typed
☺ wasn't it
You typed
☺ wasn't it
You typed
☺ wasn't it
You typed
☺ wasn't it
You typed
☺ wasn't it
You typed


why the double outputs?

I'm not particuarly sure what you're trying to do but here are 3 points:

1. Line 19 is wrong and needs to be looked over.
2. The first line of your while loop(line 21) will always be executed as its inside the loop. Then you have an if-else statement which will also always be executed; this explains the double outputs.
3. Line 22 is also very wrong and needs to be looked over.
For line 19 while((char c = cin.get()) != 'q') should work.
I have no idea what are you trying to achieve in line 22...

edit: Sorry. It seems that you have to declare 'c' somewhere else.
1
2
char c;
while((c = cin.get()) != 'q')
Last edited on
1
2
char c;
while((c = cin.get()) != 'q')


now produces


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Type characters:
a b c
a wasn't it
You typed a or b
  wasn't it
You typed
b wasn't it
You typed a or b
  wasn't it
You typed
c wasn't it
You typed

 wasn't it
You typed


I'm just testing out some ideas
thanks,


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
//On-the-fly variable definitions
#include <iostream>
using namespace std;

int main()
{
	{	//begin new scope
		int q = 0;	//C requires definitions
		//define at point of use
		for(int i = 0; i < 100; i++)
		{
			q++;	//q comes from a larger scope
			//definition at the end of the scope
			int p = 12;
		}
		int p = 1;	//a different p
	}	//end scope containing q & outer p
	cout << "Type characters:" << endl;
	char c;
	char x;
	while((c = cin.get()) != 'q')
	{
		x = c;
		cout << c << " wasn't it" << endl;
		if(x == 'a' || c == 'b')
			cout << "You typed a or b" << endl;
		else
			cout << "You typed " << x << endl;
	}
	cout << "Type A, B, or C" << endl;
	switch(int i = cin.get())
	{
	case 'A': cout << "Snap" << endl; break;
	case 'B': cout << "Crackle" << endl; break;
	case 'C': cout << "Pop" << endl; break;
	default: cout << "Not A, B, or C!" << endl;
	}
}


still produces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Type characters:
a b c
a wasn't it
You typed a or b
  wasn't it
You typed
b wasn't it
You typed a or b
  wasn't it
You typed
c wasn't it
You typed c

 wasn't it
You typed


while((char c = cin.get()) != 'q' might seem as a useful thing to do, the problem is that != has higher precedence than =, so char c ends up a bool converted to char; but that means the first one was incorrect as well because of the smiley face character. Also, I don't see line 22 refered above as a mistake.
Last edited on
Could you explain to me the aim of the program please? I Would like to help more than my original post.
oh sorry, I'm trying to stretch C++ to see what works and what doesn't and I wouldn't use the method above for doing the while loops, but I really would like to know the truth about line 22 and if it works or if it is just bad practice, because the outputs above are the same in both instances, and if possible get to the bottom of why x doesnt output: cout << "You typed " << x << endl; except: You typed c

Last edited on
Line 22 is as wrong as line 19 was. It would be the same as char x = (c == 'a' || c == 'b'). Here x can only be 1 or 0.
About the x:
you code is
1
2
3
4
if(x == 'a' || c == 'b')
    cout << "You typed a or b" << endl;
else
    cout << "You typed " << x << endl;

this is the same as
1
2
3
4
if(x == 'a' || c == 'b')
    cout << "You typed a or b" << endl;
if(x != 'a' && c != 'b')
    cout << "You typed " << x << endl;

now, do you see why?

Also, notice that every second line is missing a char. This is because cin.get() leaves a '\n' in the input stream. remove it with cin.ignore(some_number, '\n'). (some_number should be std::numeric_limits<std::streamsize>::max(), but any number greater than 0 should do. I usually use 100, because I'm too lazy to write all that...)
thaks for your patience and response guys :)
Last edited on
Topic archived. No new replies allowed.