Modifiable expression error?!

the count of the if statement says that "expression must be modifiable"...what does that mean? my format appears to be correct...obviously it is not though! Please help!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void printSentence()
{	
	int count;
	char name;

	cout << "Happy Birthday to you." << endl;
	for(count = 1 ; count <= 4; count++)
	{
		if(count <=2 || count = 4)
		{printSentence();}
		
		else (count == 3);
		{cout << printSentence();
		cout << "dear" << name ;}
		
	}
}
if(count <=2 || count = 4)
Should be:
if(count <=2 || count == 4)
Due to priorities of operators the expression in the if statement

if(count <=2 || count = 4)

in which you by mistake use the assignment operator instead of the comparision operator looks the following way

( count <=2 || count ) = 4

That is you are trying to assign number 4 to the expression ( count <=2 || count )

So change count = 4 to count == 4
Also, line 12 should be:
else if (count == 3) // Note removed ";" - it is important!
@Volatile -- thank you again for your help, sir. I must have breezed through that chapter before fully understanding the relational operators!

@vlad -- thank you for the clarification and the detailed explanation in my error.

I never thought I would get such great assistance on a forum --- thanks a lot guys! cheers
The output on this code is pretty nasty. Lines 16 and 31 --- I get an error saying that no operator matches these << operands. So I changed the like 16 to cin.get() and that seemed to work okay, but in line 31 when I want to call the string 'name' ... it doesn't work.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>

using namespace std;


void printLine()
{
	cout << "******************************************" << endl;
	cout << "******************************************" << endl;
}

void getName(string &name)
{
	cout << "Enter name of lucky person -- ";
	cin.get();
	//cin >> name;
}

void printSentence(string &name)
{	
	int count;

	for(count = 1 ; count <= 4; count++)
	{
		if(count <=2 || count == 4)
		{
			cout << "Happy Birthday to you." << endl;
		}
		else (count == 3);
		{
			cout << "Happy Birthday, dear " <<  name ;
		} 
	}
}

int main()
{
	int count;
	string name;

	getName(name);

	for(count = 1; count <= 2; count++)
	{
		printLine();
	}
		
	printSentence(name);

	for(count = 1; count <= 2; count++)
	{
		printLine();
	}
		
	system("PAUSE");
	return 0;
}


Last edited on
Because of what was stated before, you need to remove the semicolo, ';', from that line. Also, instead of using cin.get(), which isn't actually doing anything other than waiting for you to input something, you should use cin.getline(name, 80) which will accept all characters and spaces up to 80 characters. This could improve how your program works.
Topic archived. No new replies allowed.