repeating class based on user input

Hey all. So I want this program to ask the user if they have another student to input. If yes, then go through the class again, if not, exit the program. I don't know how to go about doing that... please help!! Thanks in advance
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
/*
write a program using classes to ask the user for the student's name, grade in 3 subjects, and print the data
in a nice table
*/

#include <iostream>
#include <string>
using namespace std;

//takes and prints info about the student
class Student
{
	string name;		//name of student
	int gradeEng = 0, gradeMath = 0, gradeSci = 0;		//grades in each subject
	int total;			//total of all grades		
public:
	//takes the student's name
	void takeData();
	//takes grades of the student and finds total
	int grades();
	Student()
	{
		takeData();
		grades();
	}

	//returns values to print
	string getName() { return name; }
	int getTotal() { return total; }
};

int main()
{
	Student student1;

	cout << "Name: " << student1.getName() << endl;
	cout << "Total grade = " << student1.getTotal() << endl;

	system("pause");
	return 0;
}

void Student::takeData()
{
	cout << "Enter the student's name: ";
	getline(cin, name);
}

int Student::grades()
{
	cout << "Enter each grade for the applicable subject:" << endl;
	cout << "English: ";
	cin >> gradeEng;
	cout << "Math: ";
	cin >> gradeMath;
	cout << "Science: ";
	cin >> gradeSci;
	cout << endl;
	total = gradeEng + gradeMath + gradeSci;
	return total;
}

closed account (iAk3T05o)
Put it in a loop. At the end of the loop ask if there's another student. If yes, the loop repeats, if no, end it.
when i use a while loop, if i enter yes, then it just continuously runs through without letting the user enter anything...
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
/*
write a program using classes to ask the user for the student's name, grade in 3 subjects, and print the data
in a nice table
*/

#include <iostream>
#include <string>
using namespace std;

//takes and prints info about the student
class Student
{
	string name;		//name of student
	int gradeEng = 0, gradeMath = 0, gradeSci = 0;		//grades in each subject
	int total;			//total of all grades	
	char yes;
public:
	//takes the student's name
	void takeData();
	//takes grades of the student and finds total
	int grades();
	Student()
	{
/* The new loop ***********************************/
		do
		{
			takeData();
			grades();
			cout << "Name: " << name << endl;
			cout << "Total = " << total << endl << endl;
			cout << "Do you want to enter another student? (Y/N)  ";
			cin >> yes;
		} while (yes == 'y' || yes == 'Y');	
/***********************************************/	
	}

	//returns values to print
	string getName() { return name; }
	int getTotal() { return total; }
};

void Student::takeData()
{
	cout << "Enter the student's name: ";
	getline(cin, name);
}

int Student::grades()
{
	cout << "Enter each grade for the applicable subject:" << endl;
	cout << "English: ";
	cin >> gradeEng;
	cout << "Math: ";
	cin >> gradeMath;
	cout << "Science: ";
	cin >> gradeSci;
	cout << endl;
	total = gradeEng + gradeMath + gradeSci;
	return total;
}

int main()
{
	Student student1;	

	//cout << "Name: " << student1.getName() << endl;
	//cout << "Total grade = " << student1.getTotal() << endl;

	system("pause");
	return 0;
}
Last edited on
closed account (iAk3T05o)
Does it do that when you enter yes or 'y'. If it's 'y', then try a for loop + bool + switch.
I can't write any code now so i hope someone will help.
Topic archived. No new replies allowed.