Learning Classes

Hey, I'm having a bit of trouble with my program. The issue isn't with the class I don't think, but rather the if statement. It's telling me that I have an else without a matching if statement. I get this error almost every time I make an if statement with an else. Can someone tell me what I'm doing wrong?

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
#include<iostream>
using namespace std;

class CoolSayingsClass
{
  public:

  void Saying1(){
      cout << "This is the first saying";
  }

  void Saying2(){
      cout << "This is the second saying";
  }

    void Saying3(){
        cout << "this is the third saying";
    }

};

int main()
{
    int a;

    cout << "Enter a number between 1 and 3";
    cin >> a;

    if (a=1)
    {
    CoolSayingsClass SayingOneObject;
    SayingOneObject.Saying1();
    }
    else if(a=2){

    CoolSayingsClass SayingTwoObject;
    SayingTwoObject.Saying2();

    }
    else if(a=3);{

    CoolSayingsClass SayingThreeObject;
    SayingThreeObject.Saying3();

    }
    else{

    cout << "You were supposed to enter a number between 1 and 3. I'm now going to terminate!";

    }

    return 0;
}
Extra semicolon on line 40. That condition would result in a no-op. Leaving just: { /*stuff*/ } else { /*stuff*/ }. So the compiler points out that you have an else without a matching if.
Hey thanks that made the program run, but now there's a new issue. No matter what is put in for a value between 1, and 3, only saying1 is shown. it's like only the saying1 function is called no matter what.
Last edited on
@InterFiction

Checking out the program, I see you have assignment equals ( = ), instead of comparison equals ( == ).
I would only get Saying1, no matter what I inputted. To help learn more about Classes, myself, I added a Saying4, which actually is the last else. Also I made it so that to end the program, you must enter something other than 1 to 3. Thanks for helping me learn, too..

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
// Learning Classes.cpp : Defines the entry point for the console application.

#include<iostream>

using namespace std;

class CoolSayingsClass
{
public:
	void Saying1()
	{
		cout << "This is the first saying";
	}
	void Saying2()
	{
		cout << "This is the second saying";
	}
	void Saying3()
	{
		cout << "This is the third saying";
	}
	void Saying4()
	{
		cout << "\nYou were supposed to enter a number between 1 and 3.\nI'm now going to terminate!\n\n";
	}
};

int main()
{
	int a=1;

	CoolSayingsClass SayingObject;

	while (a > 0 && a < 4)
	{
		cout << "Enter a number between 1 and 3 : ";
		cin >> a;

		if (a==1)
		{
			SayingObject.Saying1();
		}
		else if(a==2)
		{
			SayingObject.Saying2();
		}
		else if(a==3)
		{
			SayingObject.Saying3();
		}
		else
		{
			SayingObject.Saying4();
		}
		cout << "\n\n";
	}
	return 0;
}
Last edited on
Hey you should check out

http://www.thenewboston.com/?p=2629&pOpen=tutorial

this guy has tutorials on everything, and a large list of tuts on c++...he's fun..explains a lot, and it's good learning, lol.
Topic archived. No new replies allowed.