参加球员输入

时间:2018-01-14 14:55:35

标签: c++

我需要有关如何允许玩家在此代码中输入数字的帮助:

    using namespace std;

int main()
{
    char name[50];
    menu:
    cout << "Select your choice..." << endl;
    cout << "1) Start Game" << endl;
    cout << "2) Help" << endl;
    cout << "3) Exit Game" << endl;
    cin.getline(number, 1);
    if(number = 1) {
        start_game();
    }
    else if(number = 2){
        help();
    }
    else if(number = 3){
        exit();
    }
    else {
        goto menu;
    }
    return 0;
}

它表示我的程序中的cin.getline(number,1)存在问题。如果有人知道如何解决它,请告诉我。感谢

2 个答案:

答案 0 :(得分:0)

这样做:

using namespace std;

int main()
{
    char name[50];
    while (true) // Use a loop instead of goto since it is more readable for other developers
    {
        cout << "Select your choice..." << endl;
        cout << "1) Start Game" << endl;
        cout << "2) Help" << endl;
        cout << "3) Exit Game" << endl;
        int number; // You didn't declare `number`
        cin >> number; // You used getline wrong
        if (number == 1) { // Use `==` for comparing
            start_game();
            break;
        }
        if (number == 2) {
            help();
            break;
        }
        if (number == 3) {
            exit();
            break;
        }
    }
    return 0;
}

我在评论中解释了一些修正。

答案 1 :(得分:0)

#include <iostream>
using namespace std; 
int main()
{
     char name[50];
     int number ; 
     menu: 
     cout << "Select your choice..." << endl;
     cout << "1) Start Game" << endl;
     cout << "2) Help" << endl; 
     cout << "3) Exit Game" << endl;
     cin >> number;
     if(number == 1) { 
            start_game(); 
     } 
     else if(number == 2){ 
            help(); 
     } 
     else if(number == 3){ 
            exit(); 
     } 
     else { 
           goto menu; 
     } 
return 0; 
}