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;
}
}
|