有没有人能告诉我我做错了什么?我已经尽了最大的努力使用这个程序,请有更多时间的人,以便他们可以花时间看看程序并提供帮助!感谢大家抽出时间来:)
#include <iostream>
#include <string>
#include <cctype>
using std::cout;
using std::cin;
using std::endl;
//function prototypes
void getItemNumber ();
void checkItemNumber (char *, int);
int main()
{
//declare variables
string Item = "";
getItemNumber();
checkItemNumber(item);
//call function to get input
//void getItemNumber ();
//void checkItemNumber (item);
cout << "Enter your 5-digit item #: ";
cin >> item;
while (item.length() != 5)
{
cout << "Invalid item #. Please enter a 5-digit item # ";
getline(cin, item);
}
if (item.length() == 5)
{
if ('B' == toupper(item[2]))
cout << "Your color is blue" << endl;
else if ('G' == toupper(item[2]))
cout << "Your color is green" << endl;
else if ('R' == toupper(item[2]))
cout << "Your color is red" << endl;
else if ('W' == toupper(item[2]))
cout << "Your color is white" << endl;
}
else
cout<< "Invalid name no matching color...";
// if code is not from any of the above.
system("pause");
return 0;
}发布于 2009-11-28 03:37:27
我不确定我是否理解了这个问题的要点,但是上面显示的代码中确实存在一个bug (在我看来)。
问问自己,在什么情况下会打印出错误消息?换句话说,"else“属于什么"if”?
发布于 2009-11-25 09:35:13
你需要#include <ctype>来降低你的负担。
函数getItemNumber和checkItemNumber不是由任何C标准定义的,您应该询问定义getItemNumber和checkItemNumber的代码所在的标准。
https://stackoverflow.com/questions/1794111
复制相似问题