for loops and break statements

Write your question here.
for this project im having a hard time on which mistakes im doing for the loops and break statements too for this project.
#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
using namespace std;

int main()
{

vector<string> names;
vector<double> grades;
int numStudents;
int sum = 0.0
cout << "Welcome to the Student roster" << endl;
double average = 0.0;
double grade = 0.0;
string fullName;
string firstName;
string lastName;
char selection;

cout << "Welcome to the student roster! " ;
cout << "How many students are in your class?: " ;
cin >> numStudents;
//Read in Student Info
for (int i = 0; i < numStudents; ++i){
cout << "Please enter student (First Last Grade) info: ";
cin >> firstName >> lastName >> grade;
fullName = firstName + " " + lastName;
names.push_back(fullName);
grades.push_back(grade);
}

bool loop=true
//Print Menu and Thanks
cout << "Thank you for entering your students information! " << endl;
cout << "Please choose one of the following options:" << endl
<< "a: add a Student" << endl
<< "r: remove a Student" << endl
<< "p: print the class summary" << endl
<< "m: print menu" << endl
<< "q: quit program" << endl;

//Read in Initial Selection
while(selection!='q')
{

char = "selection " {} ;
cout << "Selection: " ;
cin >> selection;
switch(selection)
{

//Handle Add Student
case 'a':
{

cout << "Please enter student (First Last Grade) info: ";
cin >> firstName >> lastName >> grade;
fullName = firstName + " " + lastName;
names.push_back(fullName);
grades.push_back(grade);
break;
}
//Handle Remove Student
case'r':
{

//Ask the user what the students name is
cout << "\nPlease enter student (First Last ) info for remove: ";
cin >> firstName >> lastName;
fullName = firstName + " " + lastName;
for (int i = 0; i < names.size(); i++) {
if(names.at(i) == fullName) {
names.erase(names.begin()+i);
grades.erase(grades.begin()+i);
break;
}
}
//Print class summary
case 'p':
{
cout << "\nClass Summary "<< endl;
cout << "------------------------------ " << endl;
cout << "Name " << "\t\t" << "Games\n " ;
cout << "-------- ------------- -------- " << endl;
cout << left << setw(20) << "Name";
cout << right << setw(10) << fixed << "Grade" << endl;
cout << left << setw(20) << "--------------";
cout << right << setw(13) << "--------" << endl;
for (int i = 0; i < names.size(); i++)
{

double average=0.0;
double grades= 0.0;
cout << left << setw(20) << names.at(i);
cout << right << setw(10) << fixed << setprecision(2) << grades.at(i) << endl;
cout << names.at(i) << setw(20) << grades.at(i) << endl;
sum += grades.at(i);
}
average = average/names.size();
cout << "Number of students\n: " << endl;
cout << "------------------- "<< endl;
cout << names.size() << endl;

cout << "Average Grade: " << endl;
cout << "-------------- " << endl;
cout << average << endl;

break;
}
// display print menu
case 'm'
{
cout << "Please choose one of the following options:\n " ;
cout << "a: add a Student\n " ;
cout << "r: remove a Student\n " ;
cout << "p: print the class summary\n " ;
cout << "m: print menu\n " ;
cout << "q: quit program\n " << endl;

break;
}
case 'q':
{
loop = false;
break;
}
default:
cout << "Not a valid selection\n";
}
}
return 0;
}

Try to compile the code. Look at the errors produced by the compiler. Start with the first one, fix it, recompile and repeat.

If it still doesn't work as expected, or you have trouble fixing some of the compilation errors, then we can discuss it, but right now there are just too many obvious syntactical errors that I would just be repeating what the compiler is saying if I tried to help you.
Last edited on
Also, when posting code, please use code tags so that the code is readable.


[code]
code goes here
[/code]


As formatted:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
using namespace std;

int main() {
	vector<string> names;
	vector<double> grades;
	int numStudents;
	int sum = 0.0
		cout << "Welcome to the Student roster" << endl;
	double average = 0.0;
	double grade = 0.0;
	string fullName;
	string firstName;
	string lastName;
	char selection;

	cout << "Welcome to the student roster! ";
	cout << "How many students are in your class?: ";
	cin >> numStudents;
	//Read in Student Info
	for (int i = 0; i < numStudents; ++i) {
		cout << "Please enter student (First Last Grade) info: ";
		cin >> firstName >> lastName >> grade;
		fullName = firstName + " " + lastName;
		names.push_back(fullName);
		grades.push_back(grade);
	}

	bool loop = true
	//Print Menu and Thanks
		cout << "Thank you for entering your students information! " << endl;
	cout << "Please choose one of the following options:" << endl
		<< "a: add a Student" << endl
		<< "r: remove a Student" << endl
		<< "p: print the class summary" << endl
		<< "m: print menu" << endl
		<< "q: quit program" << endl;

		//Read in Initial Selection
	while (selection != 'q') {

		char = "selection " {};
		cout << "Selection: ";
		cin >> selection;
		switch (selection) {

		//Handle Add Student
			case 'a':
				{
					cout << "Please enter student (First Last Grade) info: ";
					cin >> firstName >> lastName >> grade;
					fullName = firstName + " " + lastName;
					names.push_back(fullName);
					grades.push_back(grade);
					break;
				}
				//Handle Remove Student
			case'r':
				{
				//Ask the user what the students name is
					cout << "\nPlease enter student (First Last ) info for remove: ";
					cin >> firstName >> lastName;
					fullName = firstName + " " + lastName;
					for (int i = 0; i < names.size(); i++) {
						if (names.at(i) == fullName) {
							names.erase(names.begin() + i);
							grades.erase(grades.begin() + i);
							break;
						}
					}
					//Print class summary
			case 'p':
				{
					cout << "\nClass Summary " << endl;
					cout << "------------------------------ " << endl;
					cout << "Name " << "\t\t" << "Games\n ";
					cout << "-------- ------------- -------- " << endl;
					cout << left << setw(20) << "Name";
					cout << right << setw(10) << fixed << "Grade" << endl;
					cout << left << setw(20) << "--------------";
					cout << right << setw(13) << "--------" << endl;
					for (int i = 0; i < names.size(); i++) {

						double average = 0.0;
						double grades = 0.0;
						cout << left << setw(20) << names.at(i);
						cout << right << setw(10) << fixed << setprecision(2) << grades.at(i) << endl;
						cout << names.at(i) << setw(20) << grades.at(i) << endl;
						sum += grades.at(i);
					}
					average = average / names.size();
					cout << "Number of students\n: " << endl;
					cout << "------------------- " << endl;
					cout << names.size() << endl;

					cout << "Average Grade: " << endl;
					cout << "-------------- " << endl;
					cout << average << endl;

					break;
				}
				// display print menu
				case 'm'
				{
					cout << "Please choose one of the following options:\n ";
					cout << "a: add a Student\n ";
					cout << "r: remove a Student\n ";
					cout << "p: print the class summary\n ";
					cout << "m: print menu\n ";
					cout << "q: quit program\n " << endl;

					break;
				}
				case 'q':
					{
						loop = false;
						break;
					}
				default:
					cout << "Not a valid selection\n";
				}
		}
		return 0;
	}
]


For a starter, have a look at the end of L11 and L32. What's missing?
As a first refactor, possibly something like:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
using namespace std;

void menu() {
	cout << "Please choose one of the following options:\n"
		<< "a: add a Student\n"
		<< "r: remove a Student\n"
		<< "p: print the class summary\n"
		<< "m: print menu\n"
		<< "q: quit program\n";
}

int main() {
	vector<string> names;
	vector<double> grades;
	int numStudents {};
	double grade {};
	string firstName;
	string lastName;
	char selection {};

	cout << "Welcome to the student roster! ";
	cout << "How many students are in your class?: ";
	cin >> numStudents;

	for (int i {}; i < numStudents; ++i) {
		cout << "Please enter student (First Last Grade) info: ";
		cin >> firstName >> lastName >> grade;
		names.push_back(firstName + " " + lastName);
		grades.push_back(grade);
	}

	cout << "Thank you for entering your students information!\n";
	menu();

	while (selection != 'q') {
		cout << "Selection: ";
		cin >> selection;

		switch (selection) {
			case 'a':
				{
					cout << "Please enter student (First Last Grade) info: ";
					cin >> firstName >> lastName >> grade;
					names.push_back(firstName + " " + lastName);
					grades.push_back(grade);
				}
				break;

			case'r':
				{
					cout << "\nPlease enter student (First Last ) info for remove: ";
					cin >> firstName >> lastName;

					const auto fullName { firstName + " " + lastName };

					for (size_t i {}; i < names.size(); ++i)
						if (names[i] == fullName) {
							names.erase(names.begin() + i);
							grades.erase(grades.begin() + i);
							break;
						}
				}
				std::cout << "Name not found\n";
				break;

			case 'p':
				{
					double sum {};

					cout << "\nClass Summary\n";
					cout << "------------------------------\n";
					cout << left << setw(20) << "Name";
					cout << right << setw(10) << fixed << "Grade\n";
					cout << left << setw(20) << "--------------";
					cout << right << setw(13) << "--------\n";

					for (size_t i {}; i < names.size(); ++i) {
						cout << left << setw(20) << names[i];
						cout << right << setw(10) << fixed << setprecision(2) << grades[i] << '\n';
						//cout << names[i] << setw(20) << grades[i] << '\n';
						sum += grades[i];
					}

					cout << "Number of students:\n";
					cout << "-------------------\n";
					cout << names.size() << '\n';

					cout << "Average Grade:\n";
					cout << "--------------\n";
					cout << sum / names.size() << '\n';
				}
				break;

			case 'm':
				menu();
				break;

			case 'q':
				//loop = false;
				break;

			default:
				cout << "Not a valid selection\n";
				break;
		}
	}
}

thank you, so sorry i also meant to do the brackects when i was posting this, because that is not the original style work to how i did it to be very honest. But i did see the errors which that i had forgetten my semicolons. I am a new programmer and i also overthink things alot. and thank you again.
Lmac wrote:
I am a new programmer

Spending some time poking around Learn C++ wouldn't be a bad idea. It is a free C++ tutorial website.

https://www.learncpp.com/

Something else to read, about using code tags:
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

Topic archived. No new replies allowed.