Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:10:56+00:00 2026-06-15T05:10:56+00:00

I am working on my final project for my c++ class. The instructions are

  • 0

I am working on my final project for my c++ class. The instructions are not clear but in either case, I have compiled my code but it won’t output anything. Could you please review my code and let me know what I am missing? Here is what she is asking for:


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.

Now 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];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:10:59+00:00Added an answer on June 15, 2026 at 5:10 am

    Review your syntax for calling functions:

    S2 = *studentData(S1);
    

    I believe functions are called without the ‘*’:

    S2 = studentData(S1);
    

    There may be more like this in your program.

    Edit1: Passing by reference, returning pointer to passed reference

    On further inspection of your program, the function studentData receives a Student variable passed by reference. This means that the original variable passed to this function can be modified directly. At the end of the function, you return a pointer to this variable. Kind of redundant.

    Similarly with changeData().

    Also, per requirement #2, the function StudentData needs to return a reference not a pointer. The declaration would look like:

    Student& StudentData(Student& any_student);
    

    Note the spelling of the function per requirement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on my final programming class project, and I am stuck right
I'm working on a project that requires GUI, but I have no experience with
I am working on final project for my C# class. My project is a
I'm working on my final project ,please help me ,i have to submit it
I am working on a final project for class and am having a little
I'm working on a final project for a PHP class and I'm having trouble
While working on my final project for my AS/400 Course, I encountered this problem
I'm working on a class project and I cannot seem to figure out what
I am working on a project where I need to have a buttonPanel in
I'm working on a project that will trace method calls from a class within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.