can't sort out logic progression of loop

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
			}


edit: the errors occur around the switch statement I believe
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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.

I think this is what you are intending to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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.

1
2
3
Student s[105]= {0}; 
		std::map<string,Student> students;
		std::string name;

That's why the array looked weird to you. I fixed it though, I think it was actually just the quotes in the case messing it up, thanks :D
Topic archived. No new replies allowed.