Help with Switch Statements

I'm working on combining two programs I've already created. Problem is, I need to use switch statements in it. I understand what they do, but I'm confused on where to add them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <file1.h> 
#include <file2.h> 

void mainmenu() 
{ 
string ans; 
cout << "1\tGenerate a Namecard\n2\tCalculate the Volume of a Cone\n3\tExit\n"; 
cin >> ans; 

case 1: case P: case p: 
printNameCard(); 
case 2: case C: case c: 
PrintConeVolume(); 
Else if (ans == "0, E, or e")
main3 ();
return 0; 
} 

I'm fairly certain I'm close to finishing this, if not for my incompetence with switch statements. Any help would be greatly appreciated.
Here is a simple switch statement I made... if your combining 2x switch statements make sure they use the same "test" variable type.. for example mine uses char..

switch example: http://www.cplusplus.com/doc/tutorial/control/

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

int main()
{
	char test = 'x';
	
	cout << "Please Enter a single char: 'a'" << endl;
	cin >> test;

	switch (test)
	{
	case 'a':
		cout << "case a: " << endl;
		break;
	case 'A':
		cout << "case A: " << endl;
		break;
	default :
		cout << "this is the default case statement" << endl;
		break;
	}



	system("PAUSE");
	return 0;
}
Okay, but I'm still not entirely sure how to fit that into my current program.
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
#include <iostream>
#include <string>
#include <file1.h> 
#include <file2.h> 
using namespace std;
void mainmenu() 
{ 
  string ans; 
       char test = 'x'
       cout << "1\tGenerate a Namecard\n2\tCalculate the Volume of a Cone\n3\tExit\n"; 
       cin >> test; 
       
       switch (test)
	{
	case '1':
		cout << "case a: " << endl;
		break;
	case '2':
		cout << "case A: " << endl;
		break;
	default :
		cout << "this is the default case statement" << endl;
		break;
	}

return 0; 
} 

Is it supposed to look something like this?
switch() works the same way as if/else if statements. You cannot use a string as an argument with a switch(), though. The else/if else and switch() do exactly the same thing in this example:

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
int item;

cout << "Select a menu item: ";
cin >> item;

// else / if else
if(item == 1)
	cout << "You chose 1";
else if(item == 2)
	cout << "You chose 2";
// ...
else
	cout << "That was not an option";

// switch()
switch(item)
{
case 1:
	cout << "You chose 1";
	break;
case 2:
	cout << "You chose 2";
	break;
// ...
default:
	cout << "That was not an option";
}


If you want to use a string as an argument, you can only use the if/else if method. If you change it to an int or char, you can use either one.
this might help you..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream.h> 

void main() 
{ 
string ans; 
cout << "1\tGenerate a Namecard\n2\tCalculate the Volume of a Cone\n3\tExit\n"; 
cin >> ans; 
switch(ans)
{
case 1: case P: case p: 
            printNameCard(); 
case 2: case C: case c: 
            PrintConeVolume(); 
case 3: exit(1);
default: cout<<"Wrong Choice!;
}
getch();
}  
@amannv2

Some problems with your code:

- It is int main() not void main(). That is in the C++ standard.

- switch only works with int or char, not std::string. So it would work if the variable ans was a char and the P's and C's were single quoted. Without the quotes, the compiler is looking for variables named P for example, but it doesn't exist.

- You are missing the break statements after each case, so execution falls through - not what you want - it might execute both functions.

- If using std::string, you must #include <string>

- It is #include <iostream> not <iostream.h>

- It is std::cout, not cout. Note that using namespace std is bad, but you don't have that either, so lines 5,6,7,15 would cause errors.
Alright, I think it's looking better, but I'm still getting some errors, like "no such directory" for the two files.
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
#include <iostream>
#include <string>
#include <file1.cpp> 
#include <file2.cpp> 
using namespace std;
void mainmenu() 
{
string char; 
  cout << "1\tGenerate a Namecard\n2\tCalculate the Volume of a Cone\n3\tExit\n"; 
cin >> ans; 
switch(char)
{
case 1: case 'P': case 'p': 
            printNameCard();
            break; 
case 2: case 'C': case 'c': 
            PrintConeVolume();
            break;
case 0: case 'E': case 'e': exit(0);
            break;
default: cout<<"Place Holder";
}
}
	}

return 0; 
} 

Would it be easier to try and directly include their source files in this?
char is a key word, you cannot use it as a variable. I am not sure what you are trying to do, but see if it works better like this:
1
2
3
4
5
6
7
8
char ans;
cout << "1\tGenerate a Namecard\n2\tCalculate the Volume of a Cone\n3\tExit\n";
cin >> ans;

switch(ans)
{
//...
}


You also cannot have a mixture of int and char, ans can only be one or the other.
1
2
3
4
5
6
case 1:

// or

case 'P':
case 'p':
I decided to do an overhaul of the code, and this is what I came up with:
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>
#include <iomanip>
#include <sstream>
#include <string>
 
using namespace std;

const string UNIVERSITY = "placeholder"; //Define a line of University Name
const string LOCATION = "placeholder "; //Define a line of location
const string NAME = "placeholder"; //Define a line of first and last name
const string DEPARTMENT = "placeholder"; //Define a line of department

int main(int argc, char **argv)
{
    int choice;

    cout << fixed << showpoint << setprecision(2) << endl;
    cout << "This program can calculate volume of a cone, "
         << "or print a name card." << endl;
    cout << "Main Menu: " <<endl;
    cout << "1: Print a name card." << endl;
    cout << "2: Calculate the volume of a cone." << endl;
    cout << "0: Exit the Program" << endl;
    cin >> choice;
    cout << endl;

    while (choice != 0)
    {
        switch (choice)
        {
        case 1: 
                stringstream stream;
    string s;
 
    for(int i=0; i<60; i++)
        stream<<"*";
 
    stream<<"\n";
    stream<<"*"<<UNIVERSITY<<" "<<LOCATION<<"*\n*\n*\n";
    stream<<NAME<<"*\n*\n";
    stream<<DEPARTMENT<<"*\n*\n";
 
    for(int i=0; i<60; i++)
        stream<<"*";
 
    stream<<"\n";
    s = stream.str();
    cout<<s;
    system ("PAUSE");
            break;

        case 2:
float r, h, v;
float pi = 3.14159;
r = 5.8;
h = 6.3;

cout << "What is the Radius?";
cin >> r;

cout << "What is the Height?";
cin >>  h;

 v = (1.0/3.0) * pi * (r*r) * h;
 
 	cout << "The volume of the cone is\n\n " << v << "\n";

system ("PAUSE");
            break;
        default:
            cout << "Invalid choice!" << endl;
        }

        cout << "Main Menu: " <<endl;
        cout << "1: Print a name card" << endl;
        cout << "2: Calculate the volume of a cone." << endl;
        cout << "0: Exit the program." << endl;
        cin >> choice;
        cout << endl;
    }

    return 0;
 }

There only seem to be minor errors throughout, but I don't quite understand what the compiler's saying.

In function `int main(int, char**)':
56 jump to case label
37 crosses initialization of `std::string s'
36 crosses initialization of `std::stringstream stream'
74 jump to case label
58 crosses initialization of `float pi'
37 crosses initialization of `std::string s'
36 crosses initialization of `std::stringstream stream'
56 [Warning] destructor needed for `s'
56 [Warning] where case label appears here
56 [Warning] (enclose actions of previous case statements requiring destructors in their own scope.)
74 [Warning] destructor needed for `s'
74 [Warning] where case label appears here

These are all the errors it has. As usual, any help would be greatly appreciated.
Alright, it works great now!
Thanks!
Topic archived. No new replies allowed.