Switch statements

Hello all. I am having some trouble with switch statements. I have been trying to put my original switch inside a switch. Like this:

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#ifdef WIN32
#include <windows.h>
#endif
#include "console.h"
#include "clearscreen.h"

namespace con = JadedHoboConsole;

int main()
{
    using std::cout;
    using std::endl;
    using std::cin;
    
    char choice;
    
    int option;
    
    cout << "\t\t\t\t\tMenu";
    cout << "\n1. Currency Converter";
    cout << "\n\n2. Message 2";
    cout << "\n\n3. Message 3";
    cout << "\n\nPlease choose one of the options above.";
    cin >> option;
    
        
    switch (option)
    {      
           case 1: 
                 do
                 {
                  int currency;
                  float eurosdollars, dollarseuros, dollarsyen, yendollars, dollarsUKpounds;
                  float UKpoundsdollars, dollarkronor, kronordollar, dollarrubles, rublesdollar;


                  cout << con::fg_white <<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
                  << con::fg_cyan <<"1. Dollars to Euros\t\t\t2. Euros to Dollars\n"
                  << con::fg_green <<"\n3. Dollars to Yen\t\t\t4. Yen to Dollars\n"
	              << con::fg_red <<"\n5. Dollars to UK Pounds\t\t\t6. UK Pounds to Dollars\n"
	              << con::fg_white <<"\n7. Dollars to Sweden Kronor\t\t8. Sweden Kronor to Dollars\n"
	              << con::fg_magenta <<"\n9. Dollars to Russian Rubles\t\t10. Russian Rubles to Dollars\n"
                  << con::fg_white <<"\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
                  cout << con::fg_yellow <<"\nPlease enter the number of the currency you want to convert: ";
                  cin >> currency;

                  if (currency == 1)
                     {
                      cout << con::fg_blue <<"\nPlease enter the amount of United States Dollars you would like to convert to European Euros: ";
                      cin >> eurosdollars;
                      cout << con::fg_blue <<"\nYou have entered " << eurosdollars << " Dollars which is equal to " << eurosdollars*0.678518116 << " Euros." << endl;
                     }
                  else if (currency == 2)
                     {
                      cout << con::fg_blue <<"\nPlease enter the amount of European Euros you would like convert to United States Dollars: ";
                      cin >> dollarseuros;
                      cout << con::fg_blue <<"\nYou have entered " << dollarseuros << " Euros which is equal to " << dollarseuros*1.4738 << " Dollars." << endl;
                      }
                  else if (currency == 3)
                      {
                       cout << con::fg_green <<"\nPlease enter the amount of United States Dollars you would like to convert to Japanese Yen: ";
                       cin >> dollarsyen;
                       cout << con::fg_green <<"\nYou have entered " << dollarsyen << " Dollars which is equal to " << dollarsyen*95.71255 << " Yen." << endl;
                      }
                  else if (currency == 4)
                      {
                       cout << con::fg_green <<"\nPlease enter the amount of Japanese Yen you would like to convert to United States Dollars: ";
                       cin >> yendollars;
                       cout << con::fg_green <<"\nYou have entered " << yendollars << " Yen which is equal to " << yendollars*0.0105652 << " Dollars." << endl;
                      }
                  else if (currency == 5)
                       {
                        cout << con::fg_red<<"\nPlease enter the amount of United States Dollars you would like to convert to United Kingdom Pounds: ";
                        cin >> dollarsUKpounds;
                        cout << con::fg_red<<"\nYou have entered " << dollarsUKpounds << " Dollars which is equal to " << dollarsUKpounds*0.598787 << " United Kingdom Pounds." << endl;
                        }  
                  else if (currency == 6)
                       {
                        cout << con::fg_red<<"\nPlease enter the United Kingdom Pounds you would like to covert to United States Dollars: ";
                        cin >> UKpoundsdollars;
                        cout << con::fg_red<<"\nYou have entered " << UKpoundsdollars << " United Kingdom Pounds which is equal to " << UKpoundsdollars*1.67004 << " Dollars." << endl;
                       }
                  else if (currency == 7)
                       {
                        cout << con::fg_white<<"\nPlease enter the amount of United States Dollars you would like to convert to Sweden Kronor: ";
                        cin >> dollarkronor;
                        cout << con::fg_white<<"\nYou have entered " << dollarkronor << " Dollars which is equal to " << dollarkronor*7.19434 << " Sweden Kronor." << endl;
                       }
                  else if (currency == 8)
                       {
                        cout << con::fg_white <<"\nPlease enter the amount of Sweden Kronor you would like to convert to United States Dollars: ";
                        cin >> kronordollar;
                        cout << con::fg_white <<"\nYou have entered " << kronordollar << " Kronor which is equal to " << kronordollar*0.138998 << " United States Dollars." << endl;
                       }
                  else if (currency == 9)
                       {
                        cout << con::fg_magenta <<"\nPlease enter the amount of United States Dollars you would like to convert to Russian Rubles: ";
                        cin >> dollarrubles;
                        cout << con::fg_magenta <<"\nYou have entered " << dollarrubles << " Dollars which is equal to " << dollarrubles*31.5650 << " Russian Rubles." << endl;
                       }
                  else if (currency == 10)
                       {
                        cout << con::fg_magenta <<"\nPlease enter the amount of Russian Rubles you would like to convert to United States Dollars: ";
                        cin >> rublesdollar;
                        cout << con::fg_magenta <<"\nYou have entered " << rublesdollar << " Rubles which is equal to " << rublesdollar*0.0316807 << " United States Dollar." << endl;
                       }       
        
                       cout << "\nDo you want to convert again? (Y/N) ";
                       cin >> choice;

                       } while(choice != 'n' && choice != 'N');

                       cout << con::clr <<"\n\n\n\n\t\t\t\tGoodbye!\n";

                               int seconds = 1;
                        #ifdef WIN32
                               Sleep(seconds*2000);
                        #else
                               sleep(seconds);
                        #endif


                        return 0;
                        }
           case 2:
                cout << "Hello";
                return 0;
                break;
           case 3:
                cout << "o hai";
                return 0;
                break;         
}

                
           
    
    


I'm getting an error saying that case 2 and 3 are not in a switch statement. How can I fix this guys?
The bracket in line 125 corresponds to the bracket for the entire switch statement. Delete this bracket, and then add a break; there instead and you should be ok.

EDIT: Technically you don't need to add the break; because you're ending main() right there anyway... You might consider putting the return 0; outside of the switch statement though, because it is consistent with every case. If you do this, then the break;s are necessary.

EDIT2: Oh! you also need to add another bracket (to make up for the one you deleted) to end the switch statement. You should do this at line 134, and end main() at 135.
Last edited on
I get an error saying that I jumped to case label and that it crosses initialization of 'int seconds'. That happens for both of the cases.
Add braces so that the scope of 'seconds' is limited:
1
2
3
4
5
6
7
8
{
    int seconds = 1;
     #ifdef WIN32
        Sleep(seconds*2000);
    #else
        sleep(seconds);
    #endif
}// here 'seconds' gets destructed so it won't give you any troubles 
Last edited on
Topic archived. No new replies allowed.