Why doesn't the output my string?

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
#include <iostream.h>

using namespace std;
double order;
string choice1;
double options(string option1, string option2, string option3)
{
	cout << "1." << option1 << endl;
	cout << "2." << option2 << endl;
	cout << "3." << option3 << endl;
	cin >> order;
	return order;
}

int main()
{
	using namespace std;
	int order;
	cout << "Wawa Order" << endl;
	
	cout << "Hoagie Size? " << endl;;
	options("Shorti", "Junior", "King");
	switch(order)
	{
		case 1:
		{
			choice1="Shorti";
		}
		case 2:
		{
			choice1="Junior";
		}
		case 3:
		{
			choice1="King";
		}
	}
	
	
	cout << "Final Order:" << endl;
	cout << choice1 << endl;
	
	return 0;
} 



The bold code I have does not output anything. I cannot figure out why its not working. Please help!
int order on line 18 will hide the global double order; declared at line 4

Notice that <iostream.h> is very old, the current header is <iostream> without .h
Are you reading your compiler warnings? It should warn that you're using order without initializing it. Line 22 should be int order = options("Shorti", "Junior", "King"); and line 18 should be removed (obviously).

That won't fix the program though, because it will always output "King". Read about the switch statement here: http://www.cplusplus.com/doc/tutorial/control/
closed account (z05DSL3A)
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
#include <iostream>
#include <string>

using namespace std;


int options(string option1, string option2, string option3)
{
    cout << "1." << option1 << endl;
    cout << "2." << option2 << endl;
    cout << "3." << option3 << endl;
    int order;
    cin >> order;
    return order;
}

int main()
{
    cout << "Wawa Order" << endl;

    cout << "Hoagie Size? " << endl;;

    string choice1;
    int order = options("Shorti", "Junior", "King");
    switch(order)
    {
    case 1:
        {
            choice1="Shorti";
            break;
        }
    case 2:
        {
            choice1="Junior";
            break;
        }
    case 3:
        {
            choice1="King";
            break;
        }
    default:
        {
            choice1="Error";
        }
    }

    cout << "Final Order:" << endl;
    cout << choice1 << endl;

    return 0;
}
Last edited on
Ok thanks for the advice, but that doesnt really help with my problem!
Order is a double, that's why.
If you assign an int to a double, it becomes inaccurate.
for example, if you do
 
double d = 1;


d won't hold 1, but most likely something like 0.9999999997 or 1.00000003 or something like that. And 1 is not the same as 0.99999999. Solution: Either make order an int, or cast it to an int
1
2
//either
int order;

1
2
//or
switch((int)order)


In general, avoid using floating point variables (like doubles or floats) unless you REALLY need floating point numbers. In this case you don't, so just go with an int.

EDIT: The way it is now it should actually work. Cases do not have {}'s though, it's just
1
2
3
4
5
6
case 1:
statement;
statement;
statement;
case 2:
...
Last edited on
closed account (z05DSL3A)
Order is a double, that's why.

Line 18 of the OP code is an int that hide the global double but it is never initialized so will have a junk value when it goes to the switch. it is not likely to be any of the switch cases and there is no default so the switch gets by-passed.
thank you filipe
hanst99 wrote:
Order is a double, that's why.

Actually, as Bazzy said, the global order was being clouded by the local one, which was an int, but wasn't being initialized.

Cases do not have {}'s though

In that case, those curly braces wouldn't be required, but if you declare something inside a case you need to make it a block (use curly braces).
Last edited on
Topic archived. No new replies allowed.