Need Help & Review on my final project

This is my final project and below are my final code and the instructions.
I am not sure if I covered everything correctly. The instructions aren't clear as to what should fully display. Could you please review and advice? Thank you!

Instructions:
This program will include error trapping with try and catch.

Put a throw in each function which gets user input and throw a string "Bad Major" if a Major of 0 is entered. The input functions are specified in 2, 4 and 7 below.

Create a global structure as follows:

1
2
3
4
5
6
struct Student 
{ 
       char Name[30]; 
       float GPA; 
       int Major; 
};


1. In main create 2 instances of that structure. Call them S1 and S2.

2. Create and call a function named StudentData:
S2 = StudentData( S1 ); //this is the call to the function
The function receives as a parameter a reference to the structure (prototyping will handle this) and will return a reference to the structure. Use couts and cins for getting data from the user. For testing purposes, change the data in S1 so that the GPA is 3.5 and the Major is 2. Since you are to use cins for getting data from the user, you are the user and just enter these values. After the call to the function both S1 and S2 will contain the same data.

3. In main print the data stored in the structures S1 and S2 using cout.

4. Call a function named ChangeData with a pointer to S2 as the argument:
ChangeData( &S2 ); //this is the call to the function
Change the data in S2 so that the GPA is 3.0 and the Major is 1. (Using these values for testing)

5. Back in main print the data stored in the structure S2 using cout.

6. Now create an array of 2 structures in main. Call the array Students.

7. Create a function, GetStudents, which will receive the array and an int representing the number of elements(2). In the function, loop through the data and get all three fields from the user using cin, cin.getline and cout statements. Organize like this:
1
2
3
4
5
6
7
8
9
10
 for (...........) 
                  { 
                         cout prompt to user 
                         cin.getline for name 
                         cout prompt to user 
                         cin for GPA
                         cout promp to user
                         cin for Major 
                          cin.ignore(1); 
                      }


The problem is that a cin for a numeric value will leave the ENTER key in the keyboard buffer and that is OK with cin and other numbers but not with strings, thus we must remove it on our own. cin.ignore should handle this for us.

8. Call the function GetStudents from main.

9. Create a function, PrintStudents, which will receive the same arguments as GetStudents. It will print out the array of students on 2 lines, 1 line per student.

[u]My Code:[/u]
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
#include<iostream>
#include<iomanip>
#include<string>
#include<istream>

using namespace std;

const int size = 30;

//Global Structure
struct Student
{
	char Name[30];
	float GPA;
	int Major;
};

// Function Prototype
Student studentData(Student&);
Student changeData(Student&);
Student getStudent(Student array[], int size);
Student printStudent(Student getStudent[], int size);

int main()
{
	Student s1;		//s1 is a Student structure
	Student s2;		//s2 is a Student structure

	s2 = studentData(s1);
	s2 = changeData(s2);

	Student getStudent[2], size;

	Student printStudent();

	cout << "Student 1 Name: " << s1.Name << endl;
	cout << "Student 1 GPA: " << s1.GPA << endl;
	cout << "Student 1 Major: " << s1.Major << endl;
	cout << "_______________________________" << endl;
	cout << "Student 2 Name: " << s2.Name << endl;
	cout << "Student 2 GPA: " << s2.GPA << endl;
	cout << "Student 2 Major: " << s2.Major << endl;

	system("Pause");
	return 0;
}

Student studentData(Student& s1)
{
	cout << "Please enter the students name: ";
	cin.getline(s1.Name, 30);
	cout << "Please enter the students GPA: ";
	cin >> s1.GPA;
	cout << "Please enter the students Major: ";
	cin >> s1.Major;
		try
		{
			s1.Major == 0;
			cout << "Bad Major";
		}
		catch (string exceptionString)
		{
			cout << exceptionString;
		}
	return s1;
}

Student changeData(Student& s2)
{
	s2.GPA = 3;
	s2.Major = 1;
		try
		{
			s2.Major == 0;
			cout << "Bad Major";
		}
		catch (string exceptionString)
		{
			cout << exceptionString;
		}
	return s2;
}

Student getStudent(Student getStudent[], int size)
{
	for (int index = 0; index > size; index ++)
	{
		cout << "Please enter the students name: ";
		cin.getline(getStudent[index].Name, 30);
		cout << "Please enter the students GPA: ";
		cin >> getStudent[index].GPA;
		cout << "Please enter the students Major: ";
		cin >> getStudent[index].Major;
			try
			{
			getStudent[index].Major == 0;
			cout << "Bad Major";
			}
			catch (string exceptionString)
			{
				cout << exceptionString;
			}
	
	return getStudent[index];
	}
}

Student printStudent(Student getStudent[], int size)
{
	for (int index = 0; index > size; index ++)
	{
		cout << "Name: " << getStudent[index].Name << "GPA: " << getStudent[index].GPA << "Major: " << getStudent[index].Major << endl;
	}
}
Your try and catch blocks are nonsensical. Get rid of them.

If you want to validate input and ask the user to re-enter a valid input, do that. There isn't any need for exceptions here.

In line 85, and 110, replace > with <

Could you please review and advice?


This much less helpful to anyone reviewing your code than something like: "I expect x to happen when I'm doing z and y happens instead."
1) Your studentData() function returns an object. It should return a reference.

2) Your try/catch blocks are nonsensical. A try/catch block means " execute what's in the try, and if an exception is thrown execute what's in the catch".
So they shouldn't be inside your functions, but outside. Like this:
1
2
3
4
5
6
7
8
try
{
    s2 = studentData(s1);
}
catch( /* what you want to catch */)
{
    // handle the error
}


3) You are asked to throw "Bad Major" if the major is 0, but you are only displaying it. Throwing is done that way: throw "Bad Major";

4) "Bad Major" is not a cpp string, it's a const char*. So catching strings won't work.

5) Intructions 3 tells you to print the contents of S1 and S2 after calling studentData().
You don't do it.

6) Your changeData() function receives a reference and returns an object. According to instruction 4 it should receive a pointer and return nothing (void).

7) You didn't do instruction 5.

8) Instruction 6 tells you to name the array "Students". You named the array "getStudent".

9) Your getStudent() function should be named getStudents(). It should use cin.ignore() (instruction 7).

10) The getStudents() function is not called from main() (instruction 8).

11) Same remarks as cire.
Have you tried to run this code ? I think you need to work on it some more.

Does not look like you followed instructions here:
This program will include error trapping with try and catch.

or
9. Create a function, PrintStudents, which will receive the same arguments as GetStudents. It will print out the array of students on 2 lines, 1 line per student.


I pretty much stop reading the rest of the instructions, maybe you should try it again and check off each one that is complete.

Output from when I ran it.

1
2
3
4
5
6
7
8
9
10
Please enter the students name: smith
Please enter the students GPA: 3
Please enter the students Major: Math
Bad MajorBad MajorStudent 1 Name: smith
Student 1 GPA: 3
Student 1 Major: 4932288
_______________________________
Student 2 Name: smith
Student 2 GPA: 3
Student 2 Major: 1
Last edited on
Thank you everyone! I am new at this so I was a bit confused on the instructions. I will follow your advise and get back to you if I encounter any troubles.
Topic archived. No new replies allowed.