为什么我的代码在运行时仍处于循环中

时间:2019-12-02 06:31:43

标签: c++

JTextField textField = (JTextField) evt.getSource();
textField.setBorder( /* whatever border you need */ );

这是我的输出

#include <iostream>
#include <string>
#include <ctime>

using namespace std;

class PVR {
private:
    int channel;
public:
    PVR() {
        cout << "Select the channel ==> ";
        cin >> channel;
        cout << endl;
    }
};

class DVR  {
private:
    string position;
public:
    DVR() {
        cout << "Select the position ==> ";
        cin >> position;
        cout << endl;
    }
};

class Oven {
private:
    string food;
public:
    Oven() {
        cout << "What do you want to bake? ==> ";
        cin >> food;
        cout << endl;
    }
};


class PrgDevice {
private:
    tm startTime;
    tm stopTime;
    int choice;
    int choice1;
    char c;
public:
    void dateTime() {
        cout << "Enter start date and start time: ";
        cin >> startTime.tm_mday >> startTime.tm_mon >> startTime.tm_year >> startTime.tm_hour >> startTime.tm_min >> startTime.tm_sec;
        cout << "Enter stop date and stop time: ";
        cin >> stopTime.tm_mday >> stopTime.tm_mon >> stopTime.tm_year >> stopTime.tm_hour >> stopTime.tm_min >> stopTime.tm_sec;
    }

    void mainMenu() {
        while (choice != 3) {
            cout << "Main menu options: \n";
            cout << "      1. Select a device to program (contains a submenu)" << endl;
            cout << "      2. Display current status of all devices" << endl;
            cout << "      3. Exit" << endl;
            cout << "Enter your option => ";
            cin >> choice;

            if (choice == 1) {
                subMenu();
            }
            else if (choice == 2) {
                cout << choice;
            }
            else {
            }
        }
    }
    void subMenu() {
        do {
            cout << "Select a device:" << endl;
            cout << "         1. PVR" << endl;
            cout << "         2. Camera DVR" << endl;
            cout << "         3. Oven" << endl;
            cout << "Enter your option => ";
            cin >> choice1;

            if (choice1 == 1) {
                PVR n1;
            }
            else if (choice1 == 2) {
                DVR n2;
            }
            else {
                Oven n3;
            }
            dateTime();
            cout << endl;
            cout << "Another device to program Y/N ? => ";

            cin >> c;
        }
        while(c == 'Y' || c == 'y');
    }
};


int main() {
    PrgDevice obj1;
    obj1.mainMenu();

    system("pause");
    return 0;
}

当我按Enter键时,我的程序进入了一个循环,我不知道为什么这么做

2 个答案:

答案 0 :(得分:4)

问题是您的程序希望用户输入时间和日期作为空格分隔的值,而您将它们输入为冒号分隔的值。

那是

cin >> startTime.tm_mday >> startTime.tm_mon >> ...

会将12读入startTime.tm_mday,但是输入中的:startTime.tm_mon类型不匹配。这样将停止所有输入读取,将:留在输入缓冲区中,供您进行下一个输入操作。

下一个输入操作用于stopTime,由于输入缓冲区中的:,该操作将立即失败。因此,您回到了subMenu函数中,冒号将被读入c中。并且':'不等于'Y''y',因此循环结束,subMenu函数返回到mainMenu,在此您再次打印菜单。

答案 1 :(得分:3)

对我来说,这似乎并不是一个无限循环。尝试输入开始时间为:

12 1 19  20 0 0

cin不会神奇地处理输入中的':'字符,因此您可能需要阅读整行,并解析出':'字符。