首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++编程问题

C++编程问题
EN

Stack Overflow用户
提问于 2009-11-25 09:31:03
回答 2查看 369关注 0票数 0

有没有人能告诉我我做错了什么?我已经尽了最大的努力使用这个程序,请有更多时间的人,以便他们可以花时间看看程序并提供帮助!感谢大家抽出时间来:)

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

发布于 2009-11-28 03:37:27

我不确定我是否理解了这个问题的要点,但是上面显示的代码中确实存在一个bug (在我看来)。

问问自己,在什么情况下会打印出错误消息?换句话说,"else“属于什么"if”?

票数 1
EN

Stack Overflow用户

发布于 2009-11-25 09:35:13

你需要#include <ctype>来降低你的负担。

函数getItemNumber和checkItemNumber不是由任何C标准定义的,您应该询问定义getItemNumber和checkItemNumber的代码所在的标准。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1794111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档