Эта ошибка у меня уже второй раз, почему она возникает и как это исправить?
У меня эта ошибка уже второй раз, в прошлый раз я ее исправил с помощью этой строки:"test.push_back(Test{});". Я не очень понимаю, что значит эта строка, но он помог в прошлый раз. Вот сама ошибка и мой код(ошибка в case4 скорее всего из за етих строк:"cin >> test[choose].student[num_of_student].name;", "test[choose].student[num_of_student].mark = right;"):
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int num_of_test = 0;
int choose = 0;
int num_of_student = 0;
struct Information
{
int mark;
string name;
};
struct Question
{
string question;
string var_answers[3];
string answer;
//vector<int> mark;
//vector<string> name;
bool check_answer(string num)
{
if (answer == num)
return true;
return false;
}
};
class Test
{
public:
vector <Question> questions;
vector <Information> student;
};
int main()
{
setlocale(LC_ALL, "");
int password = 0;
int count_of_questions = 0;
vector <Test> test;
test.push_back(Test{});
while (true)
{
cout << "select operation which you want to do" << endl;
cout << "1 - create new test" << endl;
cout << "2 - edit test" << endl;
cout << "3 - delete test" << endl;
cout << "4 - pass the test" << endl;
cout << "5 - show all tests" << endl;
cout << "6 - show all students" << endl;
cout << "7 - log uot" << endl;
int select;
cin >> select;
switch (select)
{
case 1: {//створення тесту
cout << "Input password";
cin >> password;
if (password == 1)
{
cout << "How many questions do you want to ask?" << endl;
cin >> count_of_questions;
test[num_of_test].questions.resize(count_of_questions);//виділення пам'яті під вказану к-сть питань
for (size_t i = 0; i < count_of_questions; i++)
{
test.push_back(Test{});
cout << "Write a question:" << endl;
cin >> test[num_of_test].questions[i].question; //в якийст тест добавляється питанння
cout << "Enter 1-st variaty of answer:" << endl;//варыанти відповідей в цьому питаннні-----
cin >> test[num_of_test].questions[i].var_answers[0];
cout << "Enter 2-nd variaty of answer:" << endl;
cin >> test[num_of_test].questions[i].var_answers[1];
cout << "Enter 3-rd variaty of answer:" << endl;
cin >> test[num_of_test].questions[i].var_answers[2];//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cout << "Enter correct answer" << endl;
cin >> test[num_of_test].questions[i].answer;//правильна відповідь
}
num_of_test++;
}
else
cout << "Error";
break; }
case 2: { //редагування тесту
cout << "Input password";
cin >> password;
if (password == 1)
{
cout << "Enter num of test" << endl;
cin >> choose;
cout << "Enter num of question which you want to change" << endl;
int num_change;
cin >> num_change;
cout << "Write a new question:" << endl;
cin >> test[choose].questions.at(num_change).question;
cout << "Enter 1-st variaty of answer:" << endl;
cin >> test[choose].questions.at(num_change).var_answers[0];
cout << "Enter 2-nd variaty of answer:" << endl;
cin >> test[choose].questions.at(num_change).var_answers[1];
cout << "Enter 3-rd variaty of answer:" << endl;
cin >> test[choose].questions.at(num_change).var_answers[2];
cout << "Enter correct answer" << endl;
cin >> test[choose].questions.at(num_change).answer;
}
else
cout << "Error";
break; }
case 3: {//видалення тесту
cout << "Input password";
cin >> password;
if (password == 1)
{
vector <Test>::iterator i = test.begin();
cout << "Enter num of test to delete: " << endl;
int num;
cin >> num;
advance(i, num);
test.erase(i);
}
else
cout << "Error";
break; }
case 4: {//здача тесту
cout << "Enter num of test" << endl;
cin >> choose;
cout << "Enter your name" << endl;
cin >> test[choose].student[num_of_student].name;
int right = 0;
int wrong = 0;
for (int i = 0; i < count_of_questions; ++i)
{
cout << endl;
cout << test[choose].questions[i].question << endl;
cout << test[choose].questions[i].var_answers[0] << endl;
cout << test[choose].questions[i].var_answers[1] << endl;
cout << test[choose].questions[i].var_answers[2] << endl;
string ans;
cin >> ans;
if (test[choose].questions[i].check_answer(ans))
right++;
else
wrong++;
}
cout << "\nCorrect answers - " << right;
cout << "\nUncorrect answers - " << wrong << endl;
test[choose].student[num_of_student].mark = right;
break; }
case 5: { //показ всіх питань
cout << "Input password";
cin >> password;
if (password == 1)
{
for (size_t j = 0; j < test.size() - 1; j++)
{
cout << "________________________________________________" << endl;
for (size_t i = 0; i < count_of_questions; i++)
{
cout << "Question: - " << test[j].questions[i].question << endl;
cout << "1)" << test[j].questions[i].var_answers[0] << endl;
cout << "2)" << test[j].questions[i].var_answers[1] << endl;
cout << "3)" << test[j].questions[i].var_answers[2] << endl;
cout << "Correct answer - " << test[j].questions[i].answer << endl;
}
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n";
}
}
else
cout << "Error";
break; }
case 6: {
cout << "Which test results do you want to see?" << endl;
cin >> choose;
for (size_t i = 0; i < num_of_student; i++)
{
cout << "Name - " << test[choose].student[i].name << "\t" << test[choose].student[i].mark << endl;
}
break; }
default:// вихід з програми
throw;
}
}
}
Ответы (1 шт):
Автор решения: user411714
→ Ссылка
ошибка в том, что мой вектор выходил за свои границы, поэтому я просто выделил ему больше памяти перед началом его использования
case 4: {//здача тесту
cout << "Enter num of test" << endl;
cin >> choose;
test[choose].student.resize(40); //вот решение
cout << "Enter your name" << endl;
cin >> test[choose].student[num_of_student].name;
int right = 0;
int wrong = 0;
for (int i = 0; i < count_of_questions; ++i)
{
cout << endl;
cout << test[choose].questions[i].question << endl;
cout << test[choose].questions[i].var_answers[0] << endl;
cout << test[choose].questions[i].var_answers[1] << endl;
cout << test[choose].questions[i].var_answers[2] << endl;
string ans;
cin >> ans;
if (test[choose].questions[i].check_answer(ans))
right++;
else
wrong++;
}
cout << "\nCorrect answers - " << right;
cout << "\nUncorrect answers - " << wrong << endl;
test[choose].student[num_of_student].mark = right;
num_of_student++;
break; }