Switch Class names

I am very new to programming and I need to have a way to switch between two different class names depending on a user input for a school project. Listed below was my initial thoughts but I am probably very wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main () {
    cout << "Class Practice"<< endl;
    int switcher;
    switcher = 0;
    cin >> switcher;
    string which;
    if (switcher = 1){
        which = 'current';
    }
    else if (switcher = 2){
        which = 'cool';
    }

    Tempature which;
    float intemp;
    char inscale;

    cout << "Input tempature: ";
    cin >> intemp;
    cout << "Input Scale: (F - Farhienheit, C - Celsius, K - Kalvin): ";
    cin >> inscale;

    which.temp = intemp;
    which.scale = inscale;


I want the switch to change the name assigned to class temperature depending on the user input. So if I inputted a 1 as the value of switcher it would make line 14 "Tempature current" and make lines 23 & 24 "current.temp = intemp;" "current.scale = inscale:". If I were to input a 0 as the value of switcher it would make line 14 "Tempature cool" and make lines 23 & 24 "cool.temp = intemp;" "cool.scale = inscale;".
Last edited on
What effect should this switch have?

Change this:
1
2
3
4
5
6
    if (switcher == 1){ // Note: = assign -> == compare
        which = 'current';
    }
    else if (switcher == 2){ // Note: = assign -> == compare
        which = 'cool';
    }
I want the switch to change the name assigned to class temperature depending on the user input. So if I inputted a 1 as the value of switcher it would make line 14 "Tempature current" and make lines 23 & 24 "current.temp = intemp;" "current.scale = inscale:". If I were to input a 0 as the value of switcher it would make line 14 "Tempature cool" and make lines 23 & 24 "cool.temp = intemp;" "cool.scale = inscale;".

I may be going about this completely wrong but that is my ultimate goal.
No, it is not possible to change a variable name at runtime.

How would it help if you access the variable as "current" or as "cool"?
A masochist would:
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
void currF() {
    Tempature which;
    float intemp;
    char inscale;

    cout << "Input tempature: ";
    cin >> intemp;
    cout << "Input Scale: (F - Farhienheit, C - Celsius, K - Kalvin): ";
    cin >> inscale;

    which.temp = intemp;
    which.scale = inscale;
}

void coolF() {
    Tempature which;
    float intemp;
    char inscale;

    cout << "Input tempature: ";
    cin >> intemp;
    cout << "Input Scale: (F - Farhienheit, C - Celsius, K - Kalvin): ";
    cin >> inscale;

    which.temp = intemp;
    which.scale = inscale;
}

int main () {
    cout << "Class Practice"<< endl;
    int switcher;
    switcher = 0;
    cin >> switcher;
    string which;
    if (1 == switcher){
        currF();
    }
    else if (2 == switcher){
        coolF();
    }
}

Now you could Search&Replace "which" with "current" in one function and with "cool" in the other, but why copy-paste-duplicate all that code? If you notice a typo, you must remember to fix it in every copy.
Topic archived. No new replies allowed.