missing ';' before .

with all the errors of missing ';' before '.'
trying to call functions (not sure if i'm doing it right...
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

#include <iostream>
#include <math.h>
using namespace std;

class counterType{
public:
	counterType();
	int counterPlus(int count);
	int counterMinus(int count);
	int counterSet(int count);
	int count;
	int countAssign;

};

counterType::counterType(){
	int count=0;
};

int counterType::counterPlus(int count){
	count=count+1;
	std::cout<<"You have added '1' to count"<<endl;
	return count;
};

int counterType::counterMinus(int count){
	count=count-1;
	std::cout<<"you have subtracted '1' to count"<<endl;
	return count;
};

int counterType::counterSet(int count){

std::cout<<"pleas assign a value to count:";
std::cin>>countAssign;
count=countAssign;
return count;
};


int main(){
	int opselect;
	int startover;
	do{
	std::cout<<"count is currently set to '0'; please choose from the following options:"<<endl
	<<"1. assign a value to count"<<endl
	<<"2. add '1' to count"<<endl
	<<"3. subtract '1' from count"<<endl;
	switch(opselect){
	case 1:{
		counterType.counterSet;


		   break; };

	case 2:{

		counterType.counterPlus;

		break;};

	case 3:{
		counterType.counterMinus;

		   break;};
	};
	std::cout<<"please enter '0' to start over"<<endl;
	std::cin>>startover;
	}while(startover==0);



};
Two problems here:

1) You never create an instance of your class. This is needed somewhere near the top of main with the rest of your variables:

 
counterType my_countertype;  // Creates a new counterType object called my_countertype 


2) Yeah, you're right. Your function calls aren't right. Partly due to problem 1, but also they're syntactically wrong. So we'll use the object we've just created and call one if it's functions:

 
my_countertype.counterPlus(1); // You'd pass in whatever number you like here. 


You probably want to change the name of your parameters too, so that they don't match the class variable:

1
2
3
4
int counterType::counterMinus (int n)
{
  // Code here
};


In fact, if it's only ever adding or subtracting 1 from count, you don't need parameters at all. You could, however, use parameters to your advantage in your counterSet function. Have a go, if you don't see what I mean then post back and I'll show you.

One final thing, you can remove the int keyword from your constructor. Just having count = 0 will initialise the count variable in that class to 0. It would also be good practice to make the variables in your class private.

EDIT: Also, your while loop logic doesn't match up. When the loop starts over, count won't = 0, unless you reset it.
Last edited on
It seems that, in your switch statement, you are tyring to use a class to call member functions.
Classes can't do that; you need an object of the class.
1
2
3
4
5
//...
case 1:
counterType objectName; //this creates an instance of the counterType class
objectName.counterSet; //the object just created is used to call the member function
//the rest of the code is similar... 


You need a cin >>opselect; as well.

And the switch cases shouldn't have any curly braces, just the action to be taken and a break;
i'm a little confused as what to pass. i want to have the user select an option, and to display the current value of count.

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

class counterType{
public:
	counterType();
	int counterPlus(int count);
	int counterMinus(int count);
	int counterSet(int count);
	int count;
	int countAssign;

};

counterType::counterType(){
	int count=0;
};

int counterType::counterPlus(int count){
	count=count+1;
	std::cout<<"You have added '1' to count"<<endl;
	
};

int counterType::counterMinus(int count){
	count=count-1;
	std::cout<<"you have subtracted '1' to count"<<endl;
	
	
};

int counterType::counterSet(int count){

std::cout<<"pleas assign a value to count:";
std::cin>>countAssign;
count=countAssign;

	
};


int main(){
	counterType mycount;
	int opselect;
	int startover;
	do{
	std::cout<<"please choose from the following options:"<<endl
	<<"1. assign a value to count"<<endl
	<<"2. add '1' to count"<<endl
	<<"3. subtract '1' from count"<<endl;
	cin>>opselect;
	switch(opselect){
	case 1:{
		mycount.counterSet(1);


		   break; };

	case 2:{

		mycount.counterPlus(1);

		break;};

	case 3:{
		mycount.counterMinus(-1);

		   break;};
	};
	std::cout<<"count is currently:"<<count;
	std::cout<<"please enter '0' to start over"<<endl;
	std::cin>>startover;
	}while(startover==0);



};
Ok, no problem.

If your only ever incrementing or decrementing by one, then there's no need to pass a value into it. It's always going to do the same thing. So you can change the function to look like this:

1
2
3
4
5
int counterType::counterMinus (void)
{
   count--;
   return count;
}


You could leave the void out, but putting it in explicitly denotes that we don't want parameters in this function.

If you want to display the current counter, you could use the return value from those functions that return count:

1
2
3
4
5
6
7
8
// Somewhere up top
int current_count;

// Function call in loop
current_count = myCount.counterPlus();

// Print it out after switch
std::cout << "count is currently: << current_count; 
Last edited on
Ninja'd
Last edited on
okay i got it working, however in the final part(line 74) it return a random number of count.

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

class counterType{
public:
	counterType();
	int counterPlus(int count);
	int counterMinus(int count);
	int counterSet(int count);
	int count;
	int countAssign;

};
counterType mycount;

counterType::counterType(){
	int count=0;
};

int counterType::counterPlus(int count){
	mycount.count=mycount.count+1;
	std::cout<<"You have added '1' to count"<<endl;
	return mycount.count;
	
};

int counterType::counterMinus(int count){
	mycount.count=mycount.count+1;
	std::cout<<"you have subtracted '1' to count"<<endl;
	return mycount.count;
	
	
};

int counterType::counterSet(int count){

std::cout<<"pleas assign a value to count:";
std::cin>>countAssign;
mycount.count=mycount.countAssign;
return mycount.count;
	
};


int main(){
	counterType mycount;
	int opselect;
	int startover;
	do{
	std::cout<<"please choose from the following options:"<<endl
	<<"1. assign a value to count"<<endl
	<<"2. add '1' to count"<<endl
	<<"3. subtract '1' from count"<<endl;
	cin>>opselect;
	switch(opselect){
	case 1:{
		mycount.counterSet(0);


		   break; };

	case 2:{

		mycount.counterPlus(1);

		break;};

	case 3:{
		mycount.counterMinus(-1);

		   break;};
	};
	std::cout<<"count is currently:"<<mycount.count<<endl;
	std::cout<<"please enter '0' to start over"<<endl;
	std::cin>>startover;
	}while(startover==0);

	

};
The counterSet function isn't applying anything the count variable of the class.

It should be like this:

1
2
3
4
void counterType::CounterSet (int n)
{
   count = n;
}


Then, if you want to set it in your loop:

1
2
3
4
5
6
7
// Top of main
int set_value;

// Loop
cout << "Please enter a value to set the count to: ";
cin >> set_value;
mycount.counterSet(set_value);


In the interest of some important OOP principles, I'd strongly recommend making the variables in your class private and making some kind of Get() function to retrieve the current value.

Also, those other functions have gone crazy. My example above is all you need.

EDIT: You're also using namespace std, so there's no need to prefix all of your cin and cout commands with std::.
Last edited on
i cant seem to get it to break out of the loop option to go to the end of count is currently.
also did i get it right?

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

class counterType{
public:
	counterType();
	void counterPlus(int count);
	void counterMinus(int count);
	void counterSet(int count);
	int count;
	int set_value;

};
counterType mycount;

counterType::counterType(){
	int count=0;
};

int plus=1;
int minus=-1;
void counterType::counterPlus(int plus){
	mycount.count=mycount.count+plus;
	std::cout<<"You have added '1' to count"<<endl;
	
	
};

void counterType::counterMinus(int minus){
	mycount.count=mycount.count+minus;
	std::cout<<"you have subtracted '1' to count"<<endl;
	
	
	
};
int set_value;

void counterType::counterSet(int set_value){


cout << "Please enter a value to set the count to: ";
cin >> set_value;
mycount.counterSet(set_value);

	
};


int main(){
	counterType mycount;
	int opselect;
	int startover;
	do{
	std::cout<<"please choose from the following options:"<<endl
	<<"1. assign a value to count"<<endl
	<<"2. add '1' to count"<<endl
	<<"3. subtract '1' from count"<<endl;
	cin>>opselect;
	switch(opselect){
	case 1:{
		mycount.counterSet(set_value);


		   break; };

	case 2:{

		mycount.counterPlus(1);

		break;};

	case 3:{
		mycount.counterMinus(-1);

		   break;};
	};
	std::cout<<"count is currently:"<<mycount.count<<endl;
	std::cout<<"please enter '0' to start over"<<endl;
	std::cin>>startover;
	}while(startover==0);

	

};
bump
i'm rrreeeaaalllyyy confused....

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



class counterType{
public:
	counterType();
	int counterPlus(void);
	int counterMinus(void);
	int counterSet(int n);
	int count;
	int set_value;

};
counterType mycount;
int counterType::counterSet (int n)
{
   count = n;
   return count;
}
counterType::counterType(){
	int count=0;
};

int plus=1;
int minus=-1;
int counterType::counterPlus(void){
	count++;
	std::cout<<"You have added '1' to count"<<endl;
	return mycount.count;
	
	
};

int counterType::counterMinus (void)
{
   count--;
   return count;
}
int set_value;
int current_count=mycount.count;

//int counterType::counterSet(int set_value){
//
//cout << "Please enter a value to set the count to: ";
//cin >> set_value;
//mycount.counterSet(set_value);
//return mycount.count;
//
//	
//};


int main(){
	counterType mycount;
	int opselect;
	int startover;
	do{
	std::cout<<"please choose from the following options:"<<endl
	<<"1. assign a value to count"<<endl
	<<"2. add '1' to count"<<endl
	<<"3. subtract '1' from count"<<endl;
	cin>>opselect;
	switch(opselect){
	case 1:{
		mycount.counterSet(set_value);


		   break; };

	case 2:{

		mycount.counterPlus();
		current_count=mycount.counterPlus();
		break;};

	case 3:{
		mycount.counterMinus();
		current_count=mycount.counterMinus();
		   break;};
	};
	std::cout<<"count is currently:"<<current_count<<endl;
	std::cout<<"please enter '0' to start over"<<endl;
	std::cin>>startover;
	}while(startover==0);

	

};
After a class definition, you need a semicolon:
1
2
class A {
}; // <-- here 


After function definitions, including members, you do not:
1
2
function A::f() {
} // <-- here 


I find it distracting that they are all of the place like that. The "unnecessary" ones are empy statements, as if you had lines like this:
1
2
3
4
f(); // statement
; // <-- empty statement
; // <-- empty statement
; // <-- empty statement 
Topic archived. No new replies allowed.