Need help using strings in a switch

In the code below I'm trying to assign a different value (name of mineral) to the string in each case of the switch. However in it's current form the code just leaves the material name (in the last cout) blank. How do I fix that?


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const double c = 300000000;
int main()
{
double D = 2.419;
double R = 2.614;
double S = 1.7648;
double T = 1.6913;
double Q = 1.458;
char N;
double L,V,t,n;
string material;

cout<< "SELECT MINERAL: "<<endl<<endl;
cout<<"Diamond ( D ) "<<endl;
cout<<"Rutile ( R ) "<<endl;
cout<<"Sapphire ( S ) "<<endl;
cout<<"Tanzanite ( T ) "<<endl;
cout<<"Quartz ( Q )"<<endl<<endl;
cout<<"Enter uppercase initial of mineral you want to select:"<<endl;

cin>>N;


if ((N != 'D') && (N != 'R') && (N != 'S') && (N != 'T') && (N != 'Q'))
{cout<< "Please enter 'D' for Diamond, 'R' for Rutile, 'S' for Sapphire, 'T' for Tanzanite or 'Q' for quartz"<<endl;
cin>>N;}


switch (N)
{
case 'D':
n = D;
string material = "Diamond"
break;

case 'R':
n = R;
string material = "Rutile"
break;

case 'S':
n = S;
string material = "Sapphire"
break;

case 'T':
n = T;
string material = "Tanzanite"
break;

case 'Q':
n = Q;
string material = "Quartz"
break;
}


cout<<"Please enter distance travelled in meters:"<<endl;
cin>>L;

if (L<0)
{cout<<"Please enter non-negative distance!"<<endl;
cin>>L; }



V = n * c;
t = (L/V)*60;

cout<<"It would take "<<setprecision(6)<<t<<" seconds for a beam of light to travel "<<setprecision(6)<<L<<" meters through"<<material<<endl;

return 0;

}


In the above code I'm trying to assign a different value (name of mineral) to the string in each case. However in it's current form the code just leaves the material name (in the last cout) blank. How do I fix that?
Last edited on
Well first of all you are trying to create and assign a string inside the switch statement which is different from the string that is outside of the scope. Second of all, variables cannot be made inside switch case statements because they are simply labels unless they have scope, which can be done with curly brackets. Anyways here is the fixed version, also please use code tagsformat button <> when posting code because it's easier to read and just looks better

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

const double c = 300000000;
int main()
{
    double D = 2.419;
    double R = 2.614;
    double S = 1.7648;
    double T = 1.6913;
    double Q = 1.458;
    char N;
    double L,V,t,n;
    string material;
    
    cout<< "SELECT MINERAL: "<<endl<<endl;
    cout<<"Diamond ( D ) "<<endl;
    cout<<"Rutile ( R ) "<<endl;
    cout<<"Sapphire ( S ) "<<endl;
    cout<<"Tanzanite ( T ) "<<endl;
    cout<<"Quartz ( Q )"<<endl<<endl;
    cout<<"Enter uppercase initial of mineral you want to select:"<<endl;

    cin>>N;
    
    if ((N != 'D') && (N != 'R') && (N != 'S') && (N != 'T') && (N != 'Q'))
    {   cout<< "Please enter 'D' for Diamond, 'R' for Rutile, 'S' for Sapphire, 'T' for Tanzanite or 'Q' for quartz"<<endl;
        cin>>N;
    }
    
    switch (N)
    {
    case 'D':
        n = D;
        material = "Diamond";
        break;
    
    case 'R':
        n = R;
        material = "Rutile";
        break;
    
    case 'S':
        n = S;
        material = "Sapphire";
        break;
    
    case 'T':
        n = T;
        material = "Tanzanite";
        break;
    
    case 'Q':
        n = Q;
        material = "Quartz";
        break;
    } 
    
    cout<<"Please enter distance travelled in meters:"<<endl;
    cin>>L;
    
    if (L<0)
    {   cout<<"Please enter non-negative distance!"<<endl;
        cin>>L;
    }
    
    V = n * c;
    t = (L/V)*60;
    
    cout<<"It would take "<<setprecision(6)<<t<<" seconds for a beam of light to travel "<<setprecision(6)<<L<<" meters through"<<material<<endl;
    
    return 0;
}
.

Topic archived. No new replies allowed.