| lyla (9) | |||||||
|
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. 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 functionThe 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 functionChange 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:
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]
| |||||||
|
|
|||||||
| cire (2357) | ||
|
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 <
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." | ||
|
|
||
| toum (205) | |||
|
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:
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. | |||
|
|
|||
| SamuelAdams (321) | |||||
|
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:
or
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.
| |||||
|
Last edited on
|
|||||
| lyla (9) | |
| 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. | |
|
|
|