预期'虽然'或预期'}'

时间:2013-12-27 23:41:44

标签: c++ function menu user-defined-functions

我的功能int getUserOption()出现问题。函数的上部大括号(函数头右下方)出现错误,表示expected a ';',下部大括号上有一个错误,表示expected a 'while'。我似乎找不到任何有类似问题的人,而且我知道如果你定义它们,函数不会有分号跟随它们。我做错了什么,如何通过我的下一个功能来修复/避免它?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int getUserOption();

int main()
{
int option;
cout << "hello world\n";
    do {
        option = getUserOption();
        switch(option) {
        case 0:
            break;
        case 1:
            cout << "You chose COPY\n";
            break;
        case 2:
            cout << "You chose FACTORIAL\n";
            break;
        case 3:
            cout << "You chose COUNT\n";
            break;
        case 4:
            cout << "You chose COUNT BYTES\n";
            break;

return 0;
    }

    int getUserOption()
    {
string userSelection;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
cout << "Enter an option as it appears before the colon: \n";
cout << "  QUIT: Exit the program\n";
cout << "  1: Copy the contents of a file\n";
cout << "  2: Calculate the factorial of an integer (between 1 and 12)\n";
cout << "  3: Count the words in a file\n";
cout << "  4: Count the bytes in a file\n";

getline(cin,userSelection);

    if(userSelection[0] == '1') {
        return 1;
    } else if (userSelection[0] == '2') {
        return 2;
    } else if (userSelection[0] == '3') {
        return 3;
    } else if (userSelection[0] == '4') {
        return 4;
    } else if (userSelection == "QUIT") {
        return 0;
    } else {
        return -1;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:3)

您需要关闭do...while循环和switch声明。

do {
    ...
    switch(option) {
        ...
    } // end of switch statement
}while(...); // end of do-while loop

return 0;