2013-10-01 47 views
0

我遇到錯誤,他們說,a,q,h等沒有聲明變量,但我不知道如何解決這個問題。這是我的代碼的一個例子。菜單嵌套循環(基本的c + +)

我也遇到了一個錯誤,它無法識別while循環內的userinp,但我不確定如何將while循環外部的變量存儲從while循環轉移到while循環內部,而不聲明全局變量,我想避免。

最後一個問題:我希望能夠在每個菜單項後立即返回主菜單,但我不確定要使用什麼。

任何幫助,非常感謝!

//Runs a program with a menu that the user can navigate through different options with via text input 

#include <iostream> 
#include <iomanip> 
#include <algorithm> 
#include <ctype.h> 
#include <string> 
using namespace std; 

int main() 
{ 
    int userinp; 
    cout<<"Here is the menu:" << endl; 
    cout<<"Help(H)  addIntegers(A)  subDoubles(D)   Quit(Q)"; 

    cin >> userinp; 
    userinp = tolower(userinp); 
    while userinp != q //loop while no q is input 
    { 
     if userinp == h 
     { 
      cout <<"This is the help menu. Upon returning to the main menu, input A or a to add 2 intergers." << endl; 
      cout <<"Input D or d to subtract 2 doubles. Input Q or q to quit."; 
     } 
     if userinp == a 
     { 
      int a, b, result; 
      cout <<"Enter two integers:"; 
      cin << a << b; 
      result = a + b; 
      cout << "The sum of " << a << " + " << b << " = " << result; 
     } 
    } 
} 

回答

1

試試這個:

while(userinp != 'q') { 

這:

if(userinp == 'h') 

這:

if(userinp == 'a') 

等,你可能想聲明userinp作爲char而不是 一個int

如果要比較文字字符,則必須使用單引號。如果你想比較一個文字字符串,你需要雙引號。唯一比較文字數字而不使用引號。

您的輸入也存在錯誤。您的>>應該是<<。使用>>cin並使用<<cout

+1

另外'CIN << A << B;'應該是' cin >> a >> b;'。您包含無關的頭文件'iomanip','algorithm'和'string'。 'ctype.h'應該是'cctype'來堅持C++特定的頭文件。 – jodag

+0

我已經使用了上述更改並添加了一些代碼,但是我已經從調試器/ builde中得到了這個結果:http://pastebin.com/RNtECZVJ – Sean

+0

@Sean修復您的鏈接。或者更好的是,編輯新的東西到原始問題中,或者如果它不同,請發佈一個新問題。 – nhgrif

0

問題代碼: ù期望userinp是一個charachter但u必須聲明爲整數 2)條件是否應該是括號內