我遇到錯誤,他們說,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;
}
}
}
另外'CIN << A << B;'應該是' cin >> a >> b;'。您包含無關的頭文件'iomanip','algorithm'和'string'。 'ctype.h'應該是'cctype'來堅持C++特定的頭文件。 – jodag
我已經使用了上述更改並添加了一些代碼,但是我已經從調試器/ builde中得到了這個結果:http://pastebin.com/RNtECZVJ – Sean
@Sean修復您的鏈接。或者更好的是,編輯新的東西到原始問題中,或者如果它不同,請發佈一個新問題。 – nhgrif