我想刷新一些簡單的C++程序,這一個有一些錯誤,我不明白爲什麼。我會寫的線以上的評論中的錯誤switch語句和其他erros
int main(void)
{
bool x = true;
char userInput;
double weight;
string planetName;
while (x == true)
{
//endl; has the error "expected a ;"
cout << "Please enter your weight:" endl;
cin >> weight;
cout << "Please enter a planet name (The moon and pluto count)" endl;
cin >> planetName;
for (int i = 0; i < planetName[i] != '\0'; i++)
{
planetName[i] = tolower(planetName[i]);
}
//planetName has the error "expression must have integral or enum types"
switch (planetName)
{
//mercury has the error "too many characters in character constant"
case 'mercury' :
cout << "Your weight is :" << weight * .4155;
case 'venus' :
cout << "Your weight is :" << weight * .8975;
case 'earth' :
cout << "Your weight is :" << weight * 1;
case 'mars' :
cout << "Your weight is :" << weight * .3507;
case 'jupiter' :
cout << "Your weight is :" << weight * 2.5374;
case 'saturn' :
cout << "Your weight is :" << weight * 1.0677;
case 'uranus' :
cout << "Your weight is :" << weight * .8947;
case 'neptune' :
cout << "Your weight is :" << weight * 1.1794;
case 'pluto' :
cout << "Your weight is :" << weight * .0899;
case 'moon' :
cout << "Your weight is :" << weight * .116;
}
cout << "Try again?" << endl;
cout << "Press Y to see your weight on a different planet,or N to exit." << endl;
cin >> userInput;
// both == signs have "operand types are incompatable"
if (userInput == "y" || userInput == "Y")
{
break;
}
else if (userInput == "n" || userInput == "N")
{
x = false;
}
}
PressEnterToContinue();
return 0;
}
如果我做的事情在不到聰明的辦法,或者你認爲的更智能的方式做任何的這個,請隨時這樣說的。我真的不喜歡使用while循環,但有時候對於像這樣的短程序,我無法快速想到其他任何東西。
爲什麼標記爲「C」? –
change'to「。'只能包含一個字符 – SliceSort
如果你想以更現代的C++方式來做到這一點,你可以使用'std :: map'來映射行星名稱與引力比 –