#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;
}
这是我的输出
Main menu options:
1. Select a device to program (contains a submenu)
2. Display current status of all devices
3. Exit
Enter your option => 1
Select a device:
1. PVR
2. Camera DVR
3. Oven
Enter your option => 1
Select the channel ==> 23
Enter start date and start time: 12:01:19 20:00:00
当我按下回车键时,我的程序只是进入了一个循环,我不知道为什么会这样。
PrgDevice
成员变量,它们的值将是不确定的,使用它们将导致未定义的行为。创建一个构造函数来初始化它们。 - Some programmer dude