【发布时间】:2014-03-30 04:57:24
【问题描述】:
我需要有关 C 编程问题的帮助。我想知道是否有办法让用户在控制台输入一个单词,它会显示你编程的任何内容。这是我想做的一个例子
int Choice;
int Energy = 100;
printf ("Type 2817!\n");
scanf ("%s", &Choice);
if(Choice == 2817)
{
printf ("You have started the game\n\n\n\n\n\n\n\n\n\n\n");
}
else if(Choice == "Energy") //This isnt working in my compiler.
//Apparently the == sign is the underlined error
{
printf("%d", Energy);
}
到目前为止,我只能输入数字,但我希望能够输入单词并能够使用命令。所以基本上我希望能够输入“能量”,它会显示你有多少能量(printf(“%d”,能量)
请帮忙,
感谢您的阅读。
【问题讨论】:
-
在您的代码中应该是“
if(strstr(Choice,"2817") == 0”...strstr是在另一个字符串中查找特定字符串的最佳方式。 -
@MichaelDautermann 仅当“Choice”是字符串时;-)
-
呸...我没注意到“
Choice”是“int”。你是对的! -
选择应该是一个字符数组。 char 选择[100],或者符合要求的东西
-
单词是C中的字符串。了解
string.h中的函数