Can't figure out what's wrong with if/else

My program is incomplete but should be executable. It won't link an else to an if statement even though everything between them is within brackets. And when i replace the else with if, it runs lines of statements that are false.

Line 54 of the code


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
58
59
60
61
62
63
64
65
#include <iostream> 

using namespace std;
int main()
{
	 int iplayer = 1;
	 int ia = 3;
	 int iaa = 3;
	 int ialoop = 1;
	 int ib = 5;
	 int ic = 7;
	 int x;
	 int y;
	 int z;
	 int looplines = 1;
	 int removeloop = 1;
	 {	
		while (ia > 0)
		{cout << "|";
		ia = ia - 1;}
		cout << endl;
		while (ib > 0)
		{cout << "|";
		ib = ib - 1;}
		cout << endl;
		while (ic > 0)
		{cout << "|";
		ic = ic - 1;}
		
			while (looplines > 0)
			{
		cout << endl;
		cout << "Which row would you like to remove lines fromn? (1, 2, or 3)";												
		cin >> x;
			 if (x = 1);
				{
				while (ialoop = 1)
				{
				cout << "How many lines would you like to remove from row 1?";
				cout << endl;
				cin >> y;
					if (iaa >= y);
						{
						if (y > 0)
							{
							iaa = iaa - y;
							cout << "line A has " << iaa << " line(s) remaining";
							ialoop = ialoop - 1;
							}
						else (y < 0);		
						cout << "invalid number of lines1";
						cout << endl;
						}
					else (y > iaa);
						cout << "invalid number of lines2";
						cout << endl;
				}
			}
			}
			
			
			}
	 cin >> z;
return 0;
}
1) don't put semicolons on your if's or your else's
2) you can't give else a condition. else is the catchall.

Bad:
1
2
3
4
5
6
7
8
if(foo);  // bad
{
  cout << "yes";
}
else (bar);  // bad
{
  cout << "no";
}


Good:
1
2
3
4
5
6
7
8
if(foo) // no semicolon
{
  cout << "yes";
}
else  // no parenthetical condition, no semicolon
{
  cout << "no";
}
Last edited on
If you're going to do that, use else if.
no need for else if, just
1
2
3
4
5
else (y > iaa) // <---Dont Put SemiColons on If/else statments
{ //<---You Need Brackets if your doint 2 or more operations
     cout << "invalid number of lines2";
     cout << endl;
}

To reiterate, you don't need / can't have parenthetical statements with else -- only with if. So if you want them you need else+if -- but he doesn't need them in this case because his else conditions are the exact opposite of his if's.

1
2
3
4
5
else (y > iaa) // this makes no sense and might not even compile
else if(y > iaa) // this makes sense, but the if is unnecessary because the preceeding if will
  // ensure it's always true
----
else   // you can get by in this instance with just else.  You only need else/if if you have
  // another condition


Also I just noticed this:

while (ialoop = 1)

That is an infinite loop. You probably meant ialoop == 1. Note that = is not the same as ==
Thank you, but now i have another problem. In my "if x = 1 do this, x = 2, do that" statement, it is only doing the x = 1 section.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream> 

using namespace std;
int main()
{
	 int iplayer = 1;
	 int ia = 3;
	 int iaa = 3;
	 int ialoop = 1;
	 int ib = 5;
	 int ibb = 5;
	 int icc = 7;
	 int ibloop = 1;
	 int icloop = 1;
	 int ic = 7;
	 int x;
	 int y;
	 int z;
	 int looplines = 1;
	 int removeloop = 1;
	 while (iaa + ibb + icc > 0)
	 {	
		ia = iaa;
		while (ia > 0)
		{cout << "1";
		ia = ia - 1;}
		cout << endl;
		ib = ibb;
		while (ib > 0)
		{cout << "2";
		ib = ib - 1;}
		cout << endl;
		ic = icc;
		while (ic > 0)
		{cout << "3";
		ic = ic - 1;}
		
			{
		cout << endl;
		cout << "Which row would you like to remove lines fromn? (1, 2, or 3)";												
		cin >> x;
		cout << "X = " << x;
			 if (x = 1)
			 {
				 if (iaa > 0)
				 {
				cout << "How many lines would you like to remove from row 1?";
				cout << endl;
				cin >> y;
					if (iaa >= y)
						{
						if (y > 0)
							{
							iaa = iaa - y;
							cout << "line A has " << iaa << " line(s) remaining";
							x = x * 0;
							}
						else if (y <= 0)		
						cout << "invalid number of lines1";
						cout << endl;
						}
					else if (y > iaa)
					{
						cout << "invalid number of lines2";
						cout << endl;
					}
				}
				 else if (iaa <= 0)
				 { cout << "This row is out of lines";
				 cout << endl;}
			}
			 else if (x = 2)
				{
				{
				cout << "How many lines would you like to remove from row 2?";
				cout << endl;
				cin >> y;
					if (ibb >= y)
						{
						if (y > 0)
							{
							ibb = ibb - y;
							cout << "line B has " << ibb << " line(s) remaining";
							x = x * 0;
							}
						else if (y <= 0);		
						cout << "invalid number of lines1";
						cout << endl;
						}
					else if (y > ibb)
					{
						cout << "invalid number of lines2";
						cout << endl;
					}
				}
				}
			
			
			}
	 }
	 cin >> z;
return 0;
}


= is assignment
== is comparison

assignment of non zero values is always true; assignment of zero is always false.

you meant if( xx == 1 )
Thank you very much. I can finish my program now.
Topic archived. No new replies allowed.