midterm help.

Pages: 12
I'm taking C++ class at college currently online and the teacher does not teach, just gives us powerpoints and says good luck.

I've scraped by for now but I have no clue how to do my midterm and I need straight A's this semester by any means necessary.

Here is the assignment:

Create a program to store information about students and then print it to the screen.

Requirements:
1) The user will decide for how many students he wants to enter information
2) The program must accept the following information for each student:
- First name
- Last name
- Gender (accept both m/f and M/F)
- Age
- Height (inches)
3)For each student, print the data to the screen in tablular, left-justified format (hint: use iomanip functions)

Any help will be greatly appreciated.

I truly am clueless and this is all I have so far:

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
  #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double firstname, lastname, gendermf, age1, height1;

	cout << "What is your last name? ";
	cin >> lastname;
	cout << "What is your first name? ";
	cin >> firstname;
	cout << "What is your Gender? M or F? ";
	cin >> gendermf;
	cout << "What is your age? ";
	cin >> age1;
	cout << "What is your height? "; 
	cin >> height1; 



	

	cin.get(); cin.get();
	return 0;
}
lastname, name gender is not a double type, its a string.....
Exactly my point, he has taught nothing, I have zero idea what I'm doing.
- First name
- Last name
- Gender (accept both m/f and M/F)
- Age
- Height (inches)

First and last name are strings, gender is a char, age and height are numeric (could be either int or double depending on whether you need decimals or just whole numbers).

There's a bit about the various types in the tutorial:
http://www.cplusplus.com/doc/tutorial/variables/

Probably a good textbook would be useful too.
Last edited on
Thank you for your help.

Do you have any idea how I would code:

1) The user will decide for how many students he wants to enter information. e.g 3 student's data.
Getting the information from the user on that part is straightforward. The number of students is an integer. Output a prompt asking the user to number, then get the input as an int.

What you do next will be more interesting. To hold the information for a single student, most conveniently, put the various variables inside a struct.
http://www.cplusplus.com/doc/tutorial/structures/

To hold the information for several students, an array of those objects would be useful.
http://www.cplusplus.com/doc/tutorial/arrays/

A common approach is to make the array larger than needed, e.g. allow for say 20 or 50 students in the array. But only actually use the first three locations (depending on the number entered by the user).


Is any of this familiar to you at all? I'm getting the impression this is almost the very first time you tried to write anything of this nature. If so, there's a fair amount of learning to do as you go along.
You could create a class for a student and store a list of that class. Then iterate over the list to print them.
Chervil,

Thank you so much for the help. This is all very new to me as I switched majors this semester to IT and have been thrown straight into learning C++ in an online class by my advisor.

This is a start for you to work with.

Here is the header file for the student class.

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
#include <iostream>
#include <string>

using namespace std;

class Student {
	string fName;
	string lName;
	char gender;
	int age;
	int height;

public:
	Student() { //contructor, gets called when a new student is added to the list
		cout << "What is your last name?\n";
		cin.ignore();
		getline(cin, lName);
		cout << "What is your first name?\n";
		cin.ignore();
		getline(cin, fName);
		cout << "What is your Gender? M or F?\n";
		cin >> gender;
		cout << "What is your age?\n";
		cin >> age;
		cout << "What is your height?\n";
		cin >> height;
	}
};


Here is in the main cpp file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include <vector>
#include "student.h" //including the header file that contains the class

using namespace std;

int main()
{
	vector<Student> MyStudents; //vector that holds our students
	int count;

	cout << "How many students would you like to enter? ";
	cin >> count;

	for (int i = 1; i < count; i++) { //loop that executes the number equal to count(user input)
		MyStudents.push_back(Student()); //adds student to the vector
	}


	return 0;
}



That covers points 1 and 2. You can use a loop to iterate over the vector, you will need to modify the class with a method that creates the tabular format or have "get" methods that gets the information and create the tabular format in your main method. This is only a quick example I have pulled together so I have not fully tested it but you get the idea.
Last edited on
Thank you so much Switchy.
No problem, we have all been there. Post what you get done and I will check back tomorrow with what I have done on this.
Hmm - there's giving help. Then there's doing someone else's homework for them. I think switchy's efforts stray into the latter area.

Remember - our aim isn't to allow someone to get a good grade from another person's efforts. Rather the aim is to help people to learn how to do the task for themselves.

I'm sure your professor would recommend you read your book.

Instead of trying to get other people to do your homework, how about you take the time to learn it?

http://www.cplusplus.com/doc/tutorial/
Last edited on
Chervil and diemfdie, opinions are great aren't they? You say i'm doing some ones homework but I say I showed scottish91 how to implement what I suggested, leaving enough room their own work. I did not do scottish91's homework I have given scottish91 a starting point on which to work off. Giving links upon links won't help any one.
For completion here is me doing scottish91's homework. XD

Header file: student.h

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
#include <iostream>
#include <string>


using namespace std;


class Student {
	string fName;
	string lName;
	char gender;
	unsigned int age;
	unsigned int height;
	int limit = 150;

public:
	Student() { //contructor, gets called when a new student is added to the list
		cout << "What is your last name?\n";
		cin.ignore();
		getline(cin, lName);

		cout << "What is your first name?\n";
		cin.ignore();
		getline(cin, fName);

		cout << "What is your Gender? M or F?\n";
		cin >> gender;
		while ((toupper(gender) != 'M') && (toupper(gender) != 'F')) {
			cout << "Error! What is your Gender? M or F?\n";
			cin >> gender;
		}

		cout << "What is your age?\n";
		cin >> age;
		while (cin.fail() || age > limit) {
			cin.clear();
			cin.ignore();
			cout << "Error! Bad entry. What is your age?\n";
			cin >> age;
		}

		cout << "What is your height in inches?\n";
		cin >> height;
		while (cin.fail() || age > limit) {
			cin.clear();
			cin.ignore();
			cout << "Error! Bad entry. What is your height in inches?\n";
			cin >> height;
		}
	}

	string getFName() {
		return fName;
	}

	string getLName() {
		return lName;
	}

	char getGender() {
		return gender;
	}

	int getAge() {
		return age;
	}

	int getHeight() {
		return height;
	}
};


Main cpp file.
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
#include <iomanip>
#include <vector>
#include "student.h" //including the class created

using namespace std;

int main()
{
	vector<Student> MyStudents; //vector that holds our students
	int count;
	int width = 10;

	cout << "How many students would you like to enter? ";
	cin >> count;

	for (int i = 0; i < count; i++) { //loop that executes the number equal to count(user input)
		cout << "Student No: " << i + 1 << endl; 
		MyStudents.push_back(Student()); //adds student to the vector
	}

	for (int i = 0; i < count; i++) { 
		cout << "|";
		cout << setw(width) << left << MyStudents[i].getFName();
		cout << "|";
		cout << setw(width) << left << MyStudents[i].getLName();
		cout << "|";
		cout << setw(width) << left << MyStudents[i].getGender();
		cout << "|";
		cout << setw(width) << left << MyStudents[i].getAge();
		cout << "|";
		cout << setw(width) << left << MyStudents[i].getHeight();
		cout << "|\n";
	}

	system("pause");
	return 0;
}


@switchy

switchy wrote:
Chervil and diemfdie, opinions are great aren't they?

It was not merely opinion. I was expressing the forum policy. Since you're new here you probably hadn't realised.

http://www.cplusplus.com/forum/beginner/1/#msg6680
admin wrote:
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.

I suggest reading the sticky post at the top of this forum for guidance on these matters.
But it was of your opinion that I done the home work. In the quote you posted
It is OK to ask for hints, but not for entire solutions.


I gave a starting point not the full solution to begin with.

I decided post the full answer because you got snotty XD
Last edited on
Also since I am new here, the quote you pulled is from the guidelines for asking questions. I was answering the question not asking it. Since I am new here I probably hadn't realised. XD
Last edited on
Not sure if that's an attempt at humour, or the misplaced application of logic. I'm not here to enforce forum policy, I merely advise. Note though that others here are rather less tolerant than I am. Tread carefully.
Nice threat. Do you treat all new members with such disdain and hostility? Getting an elitist vibe from you.

Also It was a perfect application on logic XDDDDDDD
Pages: 12