gpt4 book ai didi

c++ - C++中的枚举/非法switch语句错误

转载 作者:行者123 更新时间:2023-12-02 10:33:51 27 4
gpt4 key购买 nike

我需要一点帮助,该程序用于计算和输出用户的体重与他们在我们太阳系中行星上的体重的关系。

当我尝试在该程序中使用switch语句时,整个语句被标记为错误,特别是“非法大小写错误”和“case标签只能在switch中使用”错误。我尝试将自己的语句与课本中的其他语句进行比较但没有区别。请告诉我如何解决此问题。

#include <cctype>
#include <iostream>
#include <cmath>


using namespace std;

double weight;
double planet_weight;
string planet;
double weightonplanet = 0;

enum planet_names{Mercury,Venus,Earth,Moon,Mars,Jupiter,Saturn,Uranus,Neptune,Pluto};



int main()
{
cout << "********************************************************************" << endl;
cout << endl;
cout << "Enter the name of a planet from the following options:" << endl;
cout << "(Mercury,Venus,Earth,Moon, Mars,Jupiter,Saturn,Uranus,Neptune,Pluto)" << endl;
cout << endl;
cout << "********************************************************************" << endl;
cin >> planet;

cout << endl;
cout << endl;
cout << "Please enter your current weight (ex:205)" << endl;
cin >> weight;
cout << endl;

cout << "You have entered " << planet << "." << endl;
cout << "You currently weigh " << weight << "lbs." << endl;
cout << "Your weigh on " << planet << "is " << weightonplanet << " ." << endl;

while (true)
{
switch (string planet);
{
case Mercury:
weightonplanet = weight * 0.4155;
break;
case Venus:
weightonplanet = weight * 0.8975;
break;
case Earth:
weightonplanet = weight * 1.0;
break;
case Moon:
weightonplanet = weight * 0.166;
break;
case Mars:
weightonplanet = weight * 0.3507;
break;
case Jupiter:
weightonplanet = weight * 2.5374;
break;
case Saturn:
weightonplanet = weight * 1.0677;
break;
case Uranus:
weightonplanet = weight * 0.8947;
break;
case Neptune:
weightonplanet = weight * 1.1794;
break;
case Pluto :
weightonplanet = weight * 0.899;
break;
}
}

return weightonplanet;

最佳答案

问题1:假冒分号。

switch (string planet);
^
remove me!

问题2:无法开启 string
为什么在这里介绍: Why the switch statement cannot be applied on strings?



所以,我们能做些什么?
我们可以有一串 if- else-if语句比较字符串。听起来很丑,但对于简短列表,这可能是最快的方法。注意:如果走这条路线,您可以直接比较 std::string,因此不需要 strcmp
if (planet == "Mercury")
{
weightonplanet = weight * 0.4155;
}
if (planet == "Venus")
{
weightonplanet = weight * 0.8975;
}
...

我们还可以将行星名称映射到 enum,然后打开 enum。但是,如果要这样做,为什么不消除 enumswitch并将行星名称直接映射到行星的重力呢?

为了说明这一点,我将使用C++标准库中的 std::map

#include <iostream>
#include <string> // added to get std::string. Sometimes you get it free from
// iostream, but you can't count on this, so leaving string
// out could be leaving a boobytrap for someone in the future
// If you use it, include it.
#include <map> // added to get std::map

using namespace std; // often dangerous. Use with caution

int main()
{
// No need for these variables to be global. Enclose them in main's scope
double weight;
string planet;
// removed a couple variables because they're not needed anymore

// you can chain <<s into one big statement. No need to line them up all nice and purty
// I just find it easier to read. Easier to read is easier to debug and maintain,
// so it worth the time spent. Your mileage may vary.
cout << "********************************************************************" << endl
<< endl
<< "Enter the name of a planet from the following options:" << endl
<< "(Mercury,Venus,Earth,Moon, Mars,Jupiter,Saturn,Uranus,Neptune,Pluto)" << endl
<< endl
<< "********************************************************************" << endl;
cin >> planet;

cout << endl
<< endl
<< "Please enter your current weight (ex:205)" << endl;
cin >> weight;

// can't switch on a string so here I'm mapping planet names to gravity
map<string, double> weightmap =
{
{ "Mercury", 0.4155 },
{ "Venus", 0.8975 },
{ "Earth", 1.0 },
{ "Moon", 0.166 }, // not a planet.
{ "Mars", 0.3507 },
{ "Jupiter", 2.5374 },
{ "Saturn", 1.0677 },
{ "Uranus", 0.8947 },
{ "Neptune", 1.1794 },
{ "Pluto", 0.899 } // not a planet.
};

// so now here we just look up the planet name in the map, and get gravity
// one multiplication later, we're done!
cout << endl
<< "You have entered " << planet << "." << endl
<< "You currently weigh " << weight << "lbs." << endl
<< "Your weight on " << planet << "is " << weight * weightmap[planet] << " ." << endl;
}

这里有一些问题。 "Saturn""saturn"不同。您可能希望将用户的输入转换为小写或大写,而仅测试一种情况。

如果用户输入的名称不在 map中,则 map 将为该行星创建一个新条目并将其初始化为零。因此,就该程序而言,“Iscandar”具有零重力。如果您想在 map中不捕获行星,请使用 std::map::find 。如果找不到输入,请要求用户提供其他输入。或无情地 mock 他们并退出。您的来电。

关于 map为您创建条目的快速说明。这可能非常有用,例如稀疏项目的频率计数器,但是如果程序要长时间运行,请注意不要用错别字填满过多的内存。

最后,简要介绍 using namespace std;: Why is "using namespace std;" considered bad practice?

关于c++ - C++中的枚举/非法switch语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61181221/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com