Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

The following code will not output correctly. Could you please review it? Here is what I've been asked to do.

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:

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:

    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.

  1. Call the function GetStudents from main.

  2. 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.

Here is my code

#include<iostream>
#include<iomanip>

using namespace std;

//Global variable to determine size of name
const int Name_Size = 30;

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

//Function Prototypes
Student* studentData(Student &S1);
Student* changeData(Student &S2);
Student getStudent(Student array[], int size);
Student printStudents(Student array[], int size);

int main()
{
    Student S1; // structure instance called S1.
    Student S2; // structure instance called S2.

    Student student[2]; //array of 2 structures.

    S2 = *studentData(S1); //Calls the studentData function.

    changeData(S2); //Calls the changeData function.

    getStudent(&S1, 2); //Calls the getStudent function.

    printStudents(&S1, 2); //Calls the printStudents function.

    system("pause");
    return 0;
}

//Function Definitions

//studentData function
Student* studentData(Student &S1)
{
        cout << "Student 1" << endl;
        cout << "_________" << endl;
        cout << "Name: " << S1.Name << endl;
        cout << "GPA: " << S1.GPA << endl;
        cout << "Major: " << S1.Major << endl;
        cout << endl;

    if (S1.Major == 0)
    {
        throw "Bad Major!\n";
    }
    else 
        return &S1;
}

Student* changeData(Student &S2)    // Changes the data.
{
        cout << "Name: " << S2.Name << endl;
        cout << "GPA: " << S2.GPA << endl;
        cout << "Major: " << S2.Major << endl;

    if (S2.Major == 0)
    {
        throw "Bad Major!\n";
    }
    else 
        return &S2;     
}

Student getStudent(Student array[], int size) //Function and loop to receive the array and an int representing the number of elements(2). 
{
    for (int index = 0; index < size; index++)
    {
        cout << "Please enter students full name: ";  
        cin.getline(array[index].Name, 30);
        cout << "Please enter students GPA: ";
        cin >> array[index].GPA;
        cout << "Please enter students major: ";
        cin >> array[index].Major;
        cin.ignore(1);
    }
    if (array[1].Major == 0 || array[2].Major == 0)
    {
        throw "Bad Major!\n";
    }
    else
        return array[size];
}

Student printStudents(Student array[], int size)
{
    for (int index = 0; index < size; index++)
    {
        cout << array[index].Name << "  " << array[index].GPA << "  " << array[index].Major << endl;
        return array[size];
    }
}
share|improve this question
really? no one can help with this? – Cynthia Biondi Nov 28 '12 at 2:07
3  
Since your code isn't working it's off-topic for Code Review. You accepted an answer on your duplicate question‌​. Did it not solve the problem? – Blastfurnace Nov 28 '12 at 2:51
excuse me? off-topic for Code Review? What does that mean? So, what is this site for? I have a working code. It just won't output. What is a review? Isn't that to have people check it out and tell you oh yeah, that's right or no that's wrong. stachoverflow referred me to you guys and told me this is what you all do so they couldn't help me. Are these forums really just a joke and are just here to waste people's time? I thought newbies like me come here to seek help. – Cynthia Biondi Nov 28 '12 at 2:58
3  
Your code "works" but there's no output? So it's not correct? Do you need help debugging your program? As far as I can tell from other Code Review questions and the site FAQ this is off topic. I think you were sent here in error but, either way, good luck with your homework. – Blastfurnace Nov 28 '12 at 3:10

closed as off topic by Jeff Mercado, Corbin, Brian Reichle, codesparkle, Paul Nov 28 '12 at 11:16

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

You didn't initialize S1 before using it, thus the value in it was undefined, and you will get garbage code after running your code. you can initialize it when define it like this.

Student S1 = {"Cynthia Biondi", 1, 100.0f};
Student S2; // structure instance called S2.

Some advices:

  1. You flag this as C++, infact, it's not so much C++, you just use cin/cout/throw, everything else is C.
  2. Do not use system("pause") to stop your program, use getchar() instead.
  3. studentData, what do you waht to do with this function? it just print the student info and return the input parameter, you should better get a better name for it.
  4. changeData, this function didn't change anything, and you don't need a return value for it.
  5. getStudent and printStudent also do not need a return value, just use void for them.
share|improve this answer
Thank you. The instructions are not very clear. It is my understanding that I need to ask the user for this input. However, I will try your example and see what I get. Re the advices. – Cynthia Biondi Nov 28 '12 at 2:36
studentData instructions. 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. – Cynthia Biondi Nov 28 '12 at 2:43
changeData instructions: 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…) Back in main print the data stored in the structure S2 using cout. – Cynthia Biondi Nov 28 '12 at 2:45
Oh, got it, this is a homework, and the instruction was given by your teacher, right? – zdd Nov 28 '12 at 2:45
getStudent: 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. 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. Call the function GetStudents from main. – Cynthia Biondi Nov 28 '12 at 2:48
show 2 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.