Help with switch statement

Having issue with switch statement and class functions.
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
  #include <iostream>
#define MAXSIZE 10 //set maximum size of stack to 10 integers
#include <conio.h> 

using namespace std;

//You probably will want to add an include or two here
class Stack{

private:
	int stack[MAXSIZE]; //integer array for your stack
	int tos; //array index to top of stack
	bool isEmpty(){ if (tos == -1) return true; return false; }
	bool isFull(){ if (tos == MAXSIZE - 1) return true; return false; }

public:
	Stack(){ tos = -1; } //constructor-initialize tos to -1
	//if tos == -1 then stack is empty
	void push(int i); //Add i to top of stack;
	int pop(); //Remove and return value from top of stack
	void show(); //output the stack

};
//your code for push, pop, show, and main goes here
void Stack::push(int i){
	stack[tos++] = i;
	if (isFull()==true){
		cout << "Stack Overflow Too Many Papers in the Stack"<<endl;
		return;
	}
	cout<<i<<"Has been placed into the stack.";
}
int Stack::pop(){
		int returnThis = stack[tos];
		tos--;
		if(isEmpty()==true){ 
			cout << "Stack Is Empty Cannot Pop From the Stack." << endl; 
		};
		cout<<"The Following Has Been Removed From The Stack.";
		return returnThis;
	}
void Stack::show(){
	cout<<"This is your current stack"<<endl;
	cout<<tos;
	if(isEmpty()==true){cout<<"Stack is empty"; };
	for (int count = 0; count <= tos; count++){
		cout << stack[count];}
	}
int main(){
	int endless = 0;
	char select;
	do{
		cout <<"Welcome to the Amazing Stack Program"<<endl<< "p= to push into the stack\n i= to pop the stack\n d=to display the stack\n q= to quit ";
		cin >> select;
		switch(select){
		case'p':  Stack.push(i);
			break;
		case'i':  Stack.pop();
			break;
		case 'd': Stack.show();
			break;
		case 'q':exit(1);
			break;
		default:"select a valid choice you dolt.";
	}
}while(endless!=0);
	system("pause");
}

When I try to run this the compiler gives an error that there should be a semicolon before the function calls in the switch statement. I've tried looking at guides, and other places, but I can't seem to figure out what the issue is.
You are trying to call class methods without a class object created.

In main you will want to create Stack mystack (or whatever name you decide on) then call mystack.push(i) etc.

Or you could make your methods static and use ::.

Either way I would read some class documentation carefully.
http://www.cplusplus.com/doc/tutorial/classes/
1. you did not declare a "Stack" object.
2. you did not have a variable named "i" or a value on "i"
3. you forgot your "cout <<" for your default option.
4. now you need to make your switch loop. (let us know if you have issues with that)

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
#include <iostream>
#define MAXSIZE 10 //set maximum size of stack to 10 integers

using namespace std;

//You probably will want to add an include or two here
class Stack{
    
private:
    int stack[MAXSIZE]; //integer array for your stack
    int tos; //array index to top of stack
    bool isEmpty(){ if (tos == -1) return true; return false; }
    bool isFull(){ if (tos == MAXSIZE - 1) return true; return false; }
    
public:
    Stack(){ tos = -1; } //constructor-initialize tos to -1
    //if tos == -1 then stack is empty
    void push(int i); //Add i to top of stack;
    int pop(); //Remove and return value from top of stack
    void show(); //output the stack
    
};
//your code for push, pop, show, and main goes here
void Stack::push(int i){
    stack[tos++] = i;
    if (isFull()==true){
        cout << "Stack Overflow Too Many Papers in the Stack"<<endl;
        return;
    }
    cout<<i<<"Has been placed into the stack.";
}
int Stack::pop(){
    int returnThis = stack[tos];
    tos--;
    if(isEmpty()==true){
        cout << "Stack Is Empty Cannot Pop From the Stack." << endl;
    };
    cout<<"The Following Has Been Removed From The Stack.";
    return returnThis;
}
void Stack::show(){
    cout<<"This is your current stack"<<endl;
    cout<<tos;
    if(isEmpty()==true){cout<<"Stack is empty"; };
    for (int count = 0; count <= tos; count++){
        cout << stack[count];}
}
int main(){
    
    Stack myStack;
    int i = 0;
    int endless = 0;
    char select;
    do{
        cout <<"Welcome to the Amazing Stack Program"<<endl<< "p= to push into the stack\n i= to pop the stack\n d=to display the stack\n q= to quit ";
        cin >> select;
        switch(select){
            case'p':  myStack.push(i);
                break;
            case'i':  myStack.pop();
                break;
            case 'd': myStack.show();
                break;
            case 'q':exit(1);
                break;
            default:cout << "select a valid choice you dolt." << endl;
        }
    }while(endless!=0);
   // system("pause"); // does not work on my computer
}
Thanks for the tips guys, I feel like an idiot for not seeing my problem trying now.

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
#include <iostream>
#define MAXSIZE 10 //set maximum size of stack to 10 integers
#include <conio.h> 

using namespace std;

//You probably will want to add an include or two here
class Stack{
	
private:
	int stack[MAXSIZE]; //integer array for your stack
	int tos; //array index to top of stack
	bool isEmpty(){ if (tos == -1) return true; return false; }
	bool isFull(){ if (tos == MAXSIZE - 1) return true; return false; }

public:
	Stack(){ tos = -1; } //constructor-initialize tos to -1
	//if tos == -1 then stack is empty
	void push(int i); //Add i to top of stack;
	int pop(); //Remove and return value from top of stack
	void show(); //output the stack

 }coolStack;
//your code for push, pop, show, and main goes here
void Stack::push(int i){
	stack[tos++] = i;
	if (isFull()==true){
		cout << "Stack Overflow Too Many Papers in the Stack"<<endl;
		return;
	}
	cout<<i<<"Has been placed into the stack.";
}
int Stack::pop(){
		int returnThis = stack[tos];
		tos--;
		if(isEmpty()==true){ 
			cout << "Stack Is Empty Cannot Pop From the Stack." << endl; 
		};
		cout<<"The Following Has Been Removed From The Stack.";
		return returnThis;
	}
void Stack::show(){
	cout<<"This is your current stack"<<endl;
	cout<<tos;
	if(isEmpty()==true){cout<<"Stack is empty"; };
	for (int count = 0; count <= tos; count++){
		cout << stack[count];}
	}
int main(){
	int i;
	int endless = 0;
	char select;
	do{
		cout <<"Welcome to the Amazing Stack Program"<<endl<< "p= to push into the stack\n i= to pop the stack\n d=to display the stack\n q= to quit ";
		cin >> select;
		switch(select){
		case'p':  coolStack.push(i);
			break;
		case'i':  coolStack.pop();
			break;
		case 'd': coolStack.show();
			break;
		case 'q':exit(1);
			break;
		default:"select a valid choice";
	}
}while(endless!=0);
	system("pause");
}


Thanks for the help with the switch statement guys I'll fix my .variable issue myself you guys are awesome, I'll post the completed code when I fix the last issue.
Last edited on
Almost Final 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#define MAXSIZE 10 //set maximum size of stack to 10 integers
#include <conio.h> 

using namespace std;

//You probably will want to add an include or two here
class Stack{
	
private:
	int stack[MAXSIZE]; //integer array for your stack
	int tos; //array index to top of stack
	bool isEmpty(){ if (tos == -1) return true; return false; }
	bool isFull(){ if (tos == MAXSIZE - 1) return true; return false; }

public:
	Stack(){ tos = -1; } //constructor-initialize tos to -1
	//if tos == -1 then stack is empty
	void push(int i); //Add i to top of stack;
	int pop(); //Remove and return value from top of stack
	void show(); //output the stack

 }coolStack;
//your code for push, pop, show, and main goes here
void Stack::push(int i){
	
	stack[tos++] = i;
	if (isFull()==true){
		cout << "Stack Overflow Too Many Papers in the Stack"<<endl;
		return;
	}
	cout<<i<<" Has been placed into the stack."<<endl;
	system("pause");
}
int Stack::pop(){
		int returnThis = stack[tos];
		tos--;
		if(isEmpty()==true){ 
			cout << "Stack Is Empty Cannot Pop From the Stack." << endl; 
		};
		cout<<"The Following Has Been Removed From The Stack.";
		return returnThis;
		system("pause");
	}
void Stack::show(){
	cout<<"This is your current stack "<<endl;
	if(isEmpty()==true){cout<<endl<<"Stack is empty "; };
	for (int count = 0; count <= tos; count++){
		cout << stack[count]<<", ";};
		cout<<endl;
	system("pause");
	}
int main(){
	int i;
	int endless = 1;
	char select;
	do{
		cout <<"Welcome to the Amazing Stack Program"<<endl;
                cout<< "p= to push into the stack"<<endl;
                cout<<i= to pop the stack<<endl; 
                cout<<d=to display the stack<<endl; 
                cout<<q= to quit"<<endl;
		cin >> select;
		switch(select){
		case'p': cout<<"Enter Number For Stack ";
			cin>>i;
			coolStack.push(i);
			break;
		case'i':  coolStack.pop();
			break;
		case 'd': coolStack.show();
			break;
		case 'q':exit(1);
			break;
		default:cout<<"Select a valid choice ";
	}
}while(endless!=0);
	system("pause");
} 


Actually my stack overflow error isn't working does anyone see an issue? I want this to be perfect.
Last edited on
Line 60, 61, 62 has missing double quotes "
Actually my stack overflow error isn't working does anyone see an issue?

You're testing too late. You need to check there's room before adding (trying to add) the new element.

And you pop() method has the same problem. And more! (inc. you say "The Following Has Been Removed From The Stack." but not what!)

Andy

Meanwhile...

Why are you posting broken code? Lines 60-62 are clearly wrong!

Edit: Ah ok, I assume it was for readablity reasons.

Instead of using multiple cout <<s you can do the following:

58
59
60
61
62
        cout <<"Welcome to the Amazing Stack Program\n"
               "p= to push into the stack\n"
               "i= to pop the stack\n"
               "d=to display the stack\n"
               "q= to quit"<<endl;


During preprocessing all adjacent string literals are concatenated, so this ends up looking more or less like your earlier code by the time the compilation phase begins.

I want this to be perfect.

Well...

1. as Bdanielz has already commented, you should not use system() to pause.

See this thread from 2008...

Console Closing Down
http://www.cplusplus.com/forum/beginner/1988/

and

Why system() is evil
http://www.cplusplus.com/forum/articles/11153/

based on "Console Closing Down" article:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using namespace std;

void waitForUser(){
	cout << "Press ENTER to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

int main(){
	cout << "Hello world!\n";
	waitForUser();
	return 0;
}

// main() is one case where you can omit the return, even though it's
// a function which returns an int, but I'm one of the people who
// still does it as it's consistent with all other functions. 


2. conio.h is not a standard header. And you don't even appear to need it. So lose it!

3. C++ programmers do not use #define for consts. We use consts

const int maxSize = 10;

(I follow the convention that all caps is used for #defines -- used for other things than defining consts!)

4. It is better form to initialize class members using the constructor initializer list.

5. operator== returns true or false, so you can simplify some of your code.

6. if() tests a boolean condition, and isFull() and isEmpty() already return true or false. So the explicit test looks a bit wordy to me. (This is more a convention thing, but I can tell you the standard library writers don't use ==true or ==false either!)

7. I follow the convention that all variables are initialized.

(This is point 19 of Sutter and Alexandrescu's C++ Coding Standards book: "Always Initialize Variables")

8. C++ programs should not really use exit() to end a program.

(Calling exit() disrupts the destruction of locally scoped objects:
http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main
return statement vs exit() in main() )

You have already defined endless to control your while loop but you don't use it.

Also, it looks like endless should be a bool rather than int.

9. the (evil) system("pause"); call on line 42 is after a return statement so is pointless.

10 why so many pauses, anyway? You only really need the one at the end to stop the console closing (which you would have been skipping as you called exit().)

11 you have a superfluous ; on line 47

And in a few other places.

12 I think it's better to keep a condition evaluation and what it triggers on separate lines. This is to do with the way debugger step though code and handle break points. e.g.

1
2
3
if(isEmpty()){ // no == true
    cout<<endl<<"Stack is empty ";
} // no ; 


rather than

if(isEmpty()==true){cout<<endl<<"Stack is empty "; };

13 globals are evil.

coolStack is only used in main(), so that's where it should de defined.

14 you could make the methods of your class which don't alter its state const.

15 This

48
49
50
for (int count = 0; count <= tos; count++){
		cout << stack[count]<<", ";};
		cout<<endl;


would read better as

48
49
50
51
for (int count = 0; count <= tos; count++){
		cout << stack[count]<<", ";
	} // lost ;
	cout<<endl;


(at first glance I thought the cout<<endl; was inside the loop.)

Etc.

16 your indenting could (should) be tidied it up to make it easier to spot scopes

17 why tos rather than topIndex?

a variable or function name should make it clear what it does!

Self-documenting
http://en.wikipedia.org/wiki/Self-documenting

18 and finally, a blank line between each function would make your code a bit more readable.

(There are few more minor points, such as preferring ++x to x++ as a rule (only use post increment when actually required), but they're not so important.)
Last edited on

Also on line 27, because you initially set tos to -1 you need to be incrementing tos before writing the stack value, and not the way you have it currently. try using stack[++tos] = i;
Oops! -- the issue Softrix spotted should really have been the thing I said first. It's even worse than testing late.

Andy
Last edited on
Man, you guys thorough. Really great tips, I'll try to apply them more as I learn. The broken code was just for better readability on this end, I left the actual code the same as before. I couldn't edit some of the parts to the advice, strictness of requirements, and such.

Anyway here it is complete.

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
#include <iostream>
#define MAXSIZE 10 //set maximum size of stack to 10 integers


using namespace std;

//You probably will want to add an include or two here
class Stack{
	
private:
	int stack[MAXSIZE]; //integer array for your stack
	int tos; //array index to top of stack
	bool isEmpty(){ if (tos == -1) return true; return false; }
	bool isFull(){ if (tos == MAXSIZE - 1) return true; return false; }

public:
	Stack(){ tos = -1; } //constructor-initialize tos to -1
	//if tos == -1 then stack is empty
	void push(int i); //Add i to top of stack;
	int pop(); //Remove and return value from top of stack
	void show(); //output the stack

 };
//your code for push, pop, show, and main goes here
void Stack::push(int i){
	if (isFull()){
		cout << "Stack Overflow Too Many Papers in the Stack"<<endl;
		return;
	}
	stack[++tos] = i;
	
	cout<<i<<" Has been placed into the stack."<<endl;
	system("pause");
}

int Stack::pop(){
		if(isEmpty()){cout << "Stack Is Empty Cannot Pop From the Stack. " << endl;
						return(0);
						};
		
		int returnThis = stack[tos];
		tos--;
		
		cout<<returnThis<<" Has been removed from the stack "<<endl;
		return returnThis;
	}

void Stack::show(){

	cout<<"This is your current stack "<<endl;
	
	if(isEmpty()){cout<<endl<<"Stack is empty "; };
	
	for (int count = 0; count <= tos; count++){
		cout << stack[count]<<", ";
				};
		cout<<endl;
	}

int main(){
	int z;
	Stack coolStack;
	int endless = 1;
	char select;
	do{
		cout <<"Welcome to the Amazing Stack Program"<<endl<< "p= to push into the stack\n i= to pop the stack\n d=to display the stack\n q= to quit"<<endl;
		cin >> select;
		switch(select){
		case'p': cout<<"Enter Number For Stack ";
			cin>>z;
			coolStack.push(z);
			break;
		case'i':  coolStack.pop();
			break;
		case 'd': coolStack.show();
			break;
		case 'q':return 0;
			break;
		default:cout<<"Select a valid choice "<<endl;
	}

}while(endless!=0);
	return(0);
}
Given the way your while loop is coded, I expected to see:

77
78
		case 'q':endless = 0;
			break;


If you are going to return out of the loop you should lose your "endless" variable and use while(true).

Andy

PS Thought of another comment on the code. Always try to define variables in as tight a scope as possible.
Last edited on
Topic archived. No new replies allowed.