Я создаю программу, которая будет иметь некоторые функции для работы с моей структурой. Я еще не добавил функции, но у меня проблемы с пользовательским интерфейсом. Когда я ввожу «привет» вместо int (для choice & choice2), я ожидаю получить исключение, но вместо этого получаю бесконечное значение l oop. Если «привет» - это массив символов и символы передаются в соответствующие коды ASCII, я не понимаю, почему l oop бесконечно. Мне нужно исключение, чтобы я мог его поймать.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define MARK_AMOUNT 4
struct student{
string name;
string group;
int mark[MARK_AMOUNT];
};
istream& operator >> (istream& in, student& student) {
in >> student.name >> student.group;
for (int i = 0; i < MARK_AMOUNT; i++) in >> student.mark[i];
return in;
}
ostream& operator << (ostream& out, const student& student) {
out << student.name << "\t" << student.group << "\t";
for (int i = 0; i < MARK_AMOUNT; i++) out << student.mark[i] << " ";
return out;
}
int main(){
vector <student> list;
int choice, choice2;
ifstream myfile;
bool input = false, input2 = false;
while(!input){
cout << "Input:\n1 to work with files\n2 to work in console\n";
cin >> choice;
if(choice == 1 || choice == 2) input = true;
else cout << "Wrong number.\n";
}
while(!input2){
cout << "Input function number[1-3]: ";
cin >> choice2;
if(choice2 >= 1 && choice2 <= 3) input = true;
else cout << "Wrong number.\n";
}
switch (choice) {
case 1:
{
string fileName;
cout << "Input name of file: ";
cin >> fileName;
ifstream myfile(fileName);
student temp;
while(myfile >> temp){
list.push_back(temp);
}
}
break;
case 2:
break;
default:
break;
}
switch (choice2) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
switch (choice) {
case 1:
{
myfile.close();
break;
}
case 2:
break;
default:
break;
}
return 0;
}
Вывод:
Input:
1 to work with files
2 to work in console
hello
Wrong number.
Input:
1 to work with files
2 to work in console
Wrong number.
Input:
1 to work with files
2 to work in console
Wrong number.
...
...