Using the same declaration value more than once

I am trying to make a menu program and was wondering if there is a way to declare something more than once without using a different word.

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

using namespace std;
double grams, ounces, inches, feet, meters; //units to convert
int choice; //menu choice
int main()
{
   cout << "Welcome to Measurement Converter" << endl; 
   cout << "1 - Convert grams to/from Ounces" << endl;
   cout << "2 - Convert ounces to/from Gallons" << endl;
   cout << "3 - Convert inches to/from Feet" << endl;
   cout << "4 - Convert feet to/from Meters" << endl;
   cout << "5 - Exit" << endl;
   cout << "What type of conversion would like to do?";
   cin >> choice;
   
   switch(choice)
   {
     case '1':
        cout << "Conver grams to/from ounces" << endl;
           cout << "Convert grams to/from Ounces"<< endl;
           cout << "1 - Convert grams to ounces" << endl;
           cout << "2 - Convert ounces to grams" << endl;
           cout << "3 - Cancel operation" << endl;
           int choices;
           if (choice==1)
           {
               cout << "insert grams";
               cin >> grams;
               ounces = grams/28.35;
               
           }
           
     }
   return 0;
}


I dont really know how to explain it but im trying to use int choice to make choose a program from a simple list.
Last edited on
In this case, couldn't you just reuse 'choice' again for each choice?

Also, on line 26 you need to use == for equality comparison as = is assignment.
Yeah, i kinda figured about the double equal sign. Oh really, that simple? hahaha thank you.
Well, it doesn't seem to read what I want it to read. When I put 1, it just ends the program. What is an easy and efficient way to use switch commands for menu inside of a menu? Also, can some explain me how the switch command works exactly with an example. Much would be appreciated.
Last edited on
A better structured solution would be
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
int main(){
    main_menu();
}

void main_main(){
    int choice;
    //...
    switch (choice){
        case '1':
            menu1();
            break;
        case '2':
            menu2();
            break;
        //...
    }
    //...
}

void menu1(){
    int choice;
    //...
    switch (choice){
        //...
    }
}

//etc. 
Topic archived. No new replies allowed.