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?
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
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
constdouble 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;
}