The code below is part of a much larger program. I'll post my psuedo code/outline as well and hopefully you can see where I'm trying to get the code to. At the moment I just can't sort out all the nested loops to make it not glitch out when run. Thanks for reading;
Pseudo code:
If user chooses to only view student's grade then;
Check if student has been initialized yet (if it has then display grade and break from all loops
If student name not on record then let user choose to pass name to newStudent function(this function does work just not when implemented in this segment of code for some reason)
If user chooses to revise input then allow them to input that name and then check again if it's a existing student or not
keep looping until user gets desired outcome
If you're getting lost in a loop, I highly suggest using correct indenting. Also, don't worry about having lots of white-space in there. It makes it easy to read and debug:
if (repeat == 2)
{
int reply;
do
{
if (students[name].semesterHours > 0)
{
students[name].viewStudent(studentNum);
break;
}
else
{
cout << "Student name not on record,\n(1)Create new student?\n(2)Or try again?\n... ";
cin >> reply;
}
switch (reply)
{
case'1':
students[name].newStudent(studentNum);
reply = 5;
break;
case'2':
cout << "Enter Student's name: ";
cin.sync();
getline(cin,name);
break;
default:
cout << "error" << endl;
}
} while (reply != 5);
}
As for your problem I can already see it:
you refer to students[name]. You also have getline(cin,name);. This leads me to believe that name is a string and students is an array of some structure. You can't refer to an value with a string.
The index for an array must be some sort of index (int, char, short, long, etc.). It cannot be a string.
int reply = 0;
int num_students = 0; // This keeps track of how many students we have
while (reply != 5)
{
// Input name:
cout << "Enter Student's name: "; // This is going here as we need to set name before going into this whole bit.
getline(cin.name);
// Check if name exists
bool name_exists = false;
int i = 0; // Index representing the current student
while (i < num_students)
{
if (students[i].name == name) // Compare the students name
{
name_exists = true;
break;
}
i++;
}
// If the name exists, output some info
if (name_exists)
{
students[i].viewStudent(students[i].studentNum); // Output the student number (note we haven't actually set this)
break;
}
else // If the name doesn't exist, give us the opportunity to create it.
{
cout << "Student name not on record,\n(1)Create new student?\n(2)Or try again?\n... ";
cin >> reply;
switch(reply)
{
case 1: // You don't need the quotes here
students[num_students++].newStudent(name); // Create a new student with the name (We don't have a number to input)
reply = 5;
break;
case 2:
// Not really needed since the input is at the top of the loop now.
break;
default:
cout << "error" << endl;
}
}
}
Your pointers about indenting were really helpful, thank you :) but clearly I should have posted the rest of my code and explained more, I'm quite sorry.