我有一个程序会询问学生姓名和他/她的 10 个班级。该代码是为了防止重复条目,但每次我运行程序时它都说每个条目都已经存在。我已经经历了一千次这件事,但我一生都无法弄清楚。对我的问题的任何见解将不胜感激。
#include <iostream>
using namespace std;
struct student
{
string name;
string classes[10];
};
int main(int argc, char *argv[])
{
string test_name;
student entry;
cout << "Enter the name of the student you wish to insert (string) \n";
cin >> entry.name;
for(int i = 0; i < 9; i++)
{
cout << "Enter class number " << i + 1 << " For " << entry.name << endl;
cin >> test_name;
for(int j = 0; j < 9; j++)
if(test_name == entry.classes[j])
{
cout << "Class already exists for " << entry.name << ". Please try again.\n";
i -= 1;
}
else
{
entry.classes[i] = test_name;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}