2009-11-25 73 views
0

有人能告訴我,我做錯了什麼我已經盡了全力用此程序,請與人在他們的手上多一點時間,讓他們可以花時間看節目和幫助!謝謝大家的時間:)C++編程的問題

#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; 
} 
+0

爲什麼我的代碼在我輸入正確的時候混亂了? – Rosemary 2009-11-25 01:32:48

+0

使用詳細模式,在這裏,我會給你一個手... – 2009-11-25 01:33:56

+0

因爲代碼應該縮進四個空格。 – 2009-11-25 01:34:04

回答

0

您需要#include <ctype> for tolower。

功能getItemNumber和checkItemNumber不被任何C標準定義的,你應該問從中得到了其中getItemNumber和checkItemNumber定義代碼中的一個。

+0

這聽起來像一個家庭作業「修改下面的代碼,以便它使用單獨的函數getItemNumber和checkItemNumber」。推測性地定義這些功能是這項任務的一部分。 – 2009-11-25 01:37:03

+0

OP包含cctype,它爲ctype.h C函數提供C++包裝器。看到這一點:http://www.cplusplus.com/reference/clibrary/cctype/ – s1n 2009-11-25 02:37:30

+0

他不包含它,是被入編問題後.. – codymanix 2009-11-25 02:38:21

1

不知道我得到了問題的要點,但有一定的錯誤(在我看來)的代碼,因爲它上面出現。

問問自己,在什麼條件下將錯誤消息得到打印出來?換句話說,「其他」屬於什麼「如果」?