在while循环中出现我的开关菜单问题

时间:2018-03-15 00:25:29

标签: c++

所以我正在使用while循环中的开关。而且表现不正常。当我选择' l'并加载文件,让我再次选择,然后当我尝试按下' p'要打印它,它只是循环选择提示。我很确定这是因为选择!=' q',但不知道如何修复它。

感谢您的帮助。

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

using namespace std;
//create a struct called Weather
struct Weather {
    int month;
    int date;
    int high;
    int avg;
    int low;
    double precip;
    string event;
};
//function prototypes
int loadData(ifstream &file, Weather days[1000]);
void printData(Weather days[1000], int count);

int main() {    
    // declare variables
    Weather days[1000];
    ifstream inFile;
    string checker;
    char choice = '0';
    int month = 0, count;
    string path;

    cout << "Welcome to the weather analyzer!" << endl;


    while (choice != 'q') {
        cout << "Would you like to (l)oad data, (p)rint data, (s)earch data, (o)rder the data, or (q)uit? ";
        cin >> choice;
        cout << endl;

        switch (choice) {
        case 'l':
            // promt user for file path
            cout << "Please enter the file path: ";
            cin >> path;
            // open the file
            inFile.open(path);
            // checks to see if file successfully opened and terminates if not
            if (!inFile) {
                cout << "Bad Path";
                getchar();
                getchar();
                return 0;
            }
            loadData(inFile, days);
            count = loadData(inFile, days);
            break;
        case 'p':
            printData(days, count);
            break;
        case 's':

        case 'o':

        case 'q':
            cout << "Good bye!";
            break;
        default:
            cout << "Invalid option";
        }

    }

    // Close file.
    inFile.close();

    // Pause and exit.
    getchar();
    getchar();
    return 0;
}

//loading function
int loadData(ifstream &inFile, Weather days[1000]) {
    string checker;
    int month = 0;
    int i; //i varaiable keeps track of how many lines there are for the print function
    for (i = 0; !inFile.eof(); i++) {
        inFile >> days[i].date; // gets date and checks if it is 2017 with if loop
        if (days[i].date == 2017) {
            getline(inFile, checker);
            getline(inFile, checker);
            inFile >> days[i].date; //gets correct date value
            month++;//increments month counter
        }
        days[i].month = month;//gets and stores data from file into days
        inFile >> days[i].high
            >> days[i].avg
            >> days[i].low
            >> days[i].precip;
        getline(inFile, days[i].event);

    }
    return i; //returns amount of days 
}
// printing function
void printData(Weather days[1000], int count) {
    for (int i = 0; i < count; i++) {
        cout << days[i].month << " "
            << days[i].date << " "
            << days[i].high << " "
            << days[i].avg << " "
            << days[i].low << " "
            << days[i].precip << " "
            << days[i].event << " ";
        cout << endl;
    }
}

1 个答案:

答案 0 :(得分:0)

cin读取用户输入后,您可能想要刷新cin缓冲区:

cin.clear();
cin.ignore(INT_MAX);