我在原型代码中做错了什么?

时间:2016-06-19 19:23:57

标签: c++

我试图传递我的原型菜单的主要功能,但说我没有那个可变范围。我已经尝试过不同的方法,但它没有用。

#include <iostream>
#include <string>
using namespace std;
void menu(int opccion); 
void agregar();
void consultar();
void eliminar();

void menu(opccion){
    do{
        switch(opccion){
            case 1:
                cout << "menu 2" << endl;
                break;

            case 2:
                cout << "menu 3" << endl;
                break;

            case 3: 
                cout << "menu 24" << endl;
                break;

            default: 
                cout << "opccion invalida" << endl;
        }
    }while(opccion != 4);
}

int main(){
    int opccion;
    string Menu[] = {"agregar","consultar","eliminar"}; 
    for (int i = 0; i < 3; ++i){
        cout << i+1 << endl << Menu[i];
    }
    cout << "what would you do? " << endl;
    cin >> opccion;
    menu(opccion);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

更像这样的事情:

#include <iostream>
#include <string>
using namespace std;
void menu(int opccion);
void agregar();
void consultar();
void eliminar();

int menu(){
    static const string Menu[] = {"agregar","consultar","eliminar"};
    for (int i = 0; i < 3; ++i){
        cout << i+1 << " : " << Menu[i] << endl;
    }
    int opccion;
    do {
        cout << "what would you do? " << endl;
        cin >> opccion;
        switch(opccion){
            case 1:
                cout << "menu 2" << endl;
                break;

            case 2:
                cout << "menu 3" << endl;
                break;

            case 3:
                cout << "menu 24" << endl;
                break;

            default:
                cout << "opccion invalida" << endl;
        }
    } while (opccion <1 || opccion >= 4);
    return opccion;
}

int main(){
    int opccion = menu();
    return 0;
}