在 c++ 中使用 Enum,没有错误,但不会输入
Using Enum in c++, no errors but won't input
我尝试为一个任务创建一个代码,让用户输入两个字符,程序将从列表中确定用户的意思。在我的示例中,我的列表是视频游戏机,所以 xb 是 xbox,pl 是 playstation,依此类推。我已经走得很远,并使用了我的书和在线可用内容,但是此时每当我运行代码时,它都会编译、运行并立即关闭。没有错误,但也没有要求用户输入。有什么建议吗?
#include <iostream>
using namespace std;
enum gameconsoles { Xbox, Playstation, PSP, Super_Nintendo, NES, Sega, Gamecube, Nintendo64, Wii, Comodore64, Atari }; // Yes, I know some of this isn't proper.. Should be Atari 2600 and so on, I know my consoles. Just limited to 2 characters made my selection a little more narrow so I had to generalize.
gameconsoles listed;
gameconsoles readgameconsoles()
{
gameconsoles listed;
char char1, char2;
cout << "This program will determine a game console based off of" << endl;
cout << "the first two characters you input. The list is somewhat small, but" << endl;
cout << "demonstrates the operation of enumeration programming." << endl << endl;
cout << "Please input the first two characters of a game console: ";
cin >> char1 >> char2;
switch (char1)
{
case 'A':
case 'a':
listed = Atari;
break;
case 'C':
case 'c':
listed = Comodore64;
break;
case 'G':
case 'g':
listed = Gamecube;
break;
case'N':
case'n':
if (char2 == 'E' || char2 == 'e')
listed = NES;
else
listed = Nintendo64;
break;
case 'P':
case 'p':
if (char2 == 'L' || char2 == 'l')
listed = Playstation;
else
listed = PSP;
break;
case 'S':
case 's':
if (char2 == 'E' || char2 == 'e')
listed = Sega;
else
listed = Super_Nintendo;
break;
case 'W':
case 'w':
listed = Wii;
break;
case 'X':
case 'x':
listed = Xbox;
break;
default:
cout << "Illegal input. Try again" << endl;
}
return listed;
}
void printEnum(gameconsoles listed)
{
switch (listed)
{
case Atari:
cout << "The console you have specified is Atari";
break;
case Comodore64:
cout << "The console you have specified is the Comodore 64";
break;
case Gamecube:
cout << "The console you have specified is Gamecube";
break;
case NES:
cout << "The console you have specified is the NES" << endl;
cout << "or also known as the Nintendo Entertainment System";
break;
case Nintendo64:
cout << "The console you have specified is Nintendo 64";
break;
case Playstation:
cout << "The console you have specified is Playstation";
break;
case PSP:
cout << "The console you have specified is PSP" << endl;
cout << "or better known as the Playstation Portable";
break;
case Sega:
cout << "The console you have specified is Sega";
break;
case Super_Nintendo:
cout << "The console you have specified is Super Nintendo";
break;
case Wii:
cout << "The console you have specified is Wii";
break;
case Xbox:
cout << "The console you have specified is Xbox";
system("PAUSE");
}
}
你可以这样做:
#include <iostream>
using namespace std;
enum gameconsoles
{
Xbox,
Playstation,
PSP,
Super_Nintendo,
NES,
Sega,
Gamecube,
Nintendo64,
Wii,
Comodore64,
Atari
};
gameconsoles listed;
gameconsoles readgameconsoles(char char1, char char2)
{
switch (char1)
{
case 'A':
case 'a':
listed = Atari;
break;
case 'C':
case 'c':
listed = Comodore64;
break;
case 'G':
case 'g':
listed = Gamecube;
break;
case'N':
case'n':
if (char2 == 'E' || char2 == 'e')
listed = NES;
else
listed = Nintendo64;
break;
case 'P':
case 'p':
if (char2 == 'L' || char2 == 'l')
listed = Playstation;
else
listed = PSP;
break;
case 'S':
case 's':
if (char2 == 'E' || char2 == 'e')
listed = Sega;
else
listed = Super_Nintendo;
break;
case 'W':
case 'w':
listed = Wii;
break;
case 'X':
case 'x':
listed = Xbox;
break;
default:
cout << "Illegal input. Try again" << endl;
}
return listed;
}
void printEnum(gameconsoles listed)
{
switch (listed)
{
case Atari:
cout << "The console you have specified is Atari";
break;
case Comodore64:
cout << "The console you have specified is the Comodore 64";
break;
case Gamecube:
cout << "The console you have specified is Gamecube";
break;
case NES:
cout << "The console you have specified is the NES" << endl;
cout << "or also known as the Nintendo Entertainment System";
break;
case Nintendo64:
cout << "The console you have specified is Nintendo 64";
break;
case Playstation:
cout << "The console you have specified is Playstation";
break;
case PSP:
cout << "The console you have specified is PSP" << endl;
cout << "or better known as the Playstation Portable";
break;
case Sega:
cout << "The console you have specified is Sega";
break;
case Super_Nintendo:
cout << "The console you have specified is Super Nintendo";
break;
case Wii:
cout << "The console you have specified is Wii";
break;
case Xbox:
cout << "The console you have specified is Xbox";
system("PAUSE");
}
}
void main()
{
char char1, char2;
cout << "This program will determine a game console based off of" << endl;
cout << "the first two characters you input. The list is somewhat small, but" << endl;
cout << "demonstrates the operation of enumeration programming." << endl << endl;
cout << "Please input the first two characters of a game console: ";
cin >> char1 >> char2;
readgameconsoles(char1, char2);
printEnum(listed);
system("pause");
return;
}
我试过了,它对我有用。
只需将main
放在代码的末尾,然后调用相关函数:
int main()
{
readgameconsoles();
return 0;
}
这不是关于枚举的问题,而是关于开发环境或主函数的问题。
你应该
1) 制作并运行此文件:
#include <iostream>
using namespace std;
int main(int argc, char** argv){
cout << "Please input the first two characters of a game console: ";
cin >> char1 >> char2;
cout << "you typed " << char1 << " and " << char2 << endl;
}
2)如果您没有看到"请输入游戏机的前两个字符:"行,则非常非常奇怪。通过描述您的环境来询问它。如果看到一个,请键入两个字符,按 Enter,然后转到步骤 (3):
3)如果您看到"您键入..."行,没关系,你可以继续学习枚举-s。其他转到步骤 (4)
4) 如果没有,
- 询问有关配置开发环境的信息
- 或使用其中之一按任意键功能,如那些例子: http://www.cplusplus.com/forum/articles/7312/
相关文章:
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 我的代码中有错误,未声明的标识符
- 为什么不能通过在错误输入后设置 std::cin.clear() 来使用 std::cin?
- C++ LeetCode #377 的 DP 解决方案中,此代码是否有错误?
- 试图建立银行管理系统,但有错误
- 我试图创建临时对象的方式有错误吗
- 我的随机生成器是否不工作,或者我决定人/骨架是否击中对手的方式是否有错误
- 我在OpenCV中有错误的"approxPolyDP(ROI_Vertices, ROI_Poly, 1.0, true)"
- 这段代码在获得错误输入时不会阻止 stoi 运行
- 我在这个 C++ 代码上有错误吗?
- 是视觉C++优化器错误还是我的代码中有错误?
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 为什么一个简单的C++程序会有错误的结果?是 #define 引起的吗?
- 对此有错误
- C :数据文件有错误:预期的无限制ID
- 一个带有参数的程序,为什么此程序有错误的答案
- 仅使用boost.datetime在Windows上有错误
- 使用向量循环时,是否有错误
- Microsoft Visual Studio发现我所有的CUDA设备端函数都有错误
- 使用 cin 错误输入多个浮点数