Hi everyone I need major help i took a class in c++ and was doing fine until the last challenge, I need help with it in every way, below is what is wanted but I cant understand anything please help me

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.

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

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

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

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

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

Recommended Answers

All 108 Replies

You appear to be lost and overwhelmed held captive in a class that probably has no relevance whatsoever to your degree major.

What you need is a strategy to tackle this seemingly overwhelming task.

If you look at this thing in it's entirety, yes.. it does look like some intense work. The key is to break it down as simple as possible, one step at a time.

Tip: Don't even worry about implementing error handling until you have at least a good working code. It's easy to add it in at the end once you have the main project complete.

I suspect you can perform step #1. But I'm sure you'll probably be asking what the heck you need to do for step 2.

Here is the main requirement for step #2:

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.

The requirement tells you everything you need to do, so there is no guesswork... all you have to do is translate the requirement into c++. Write a function that accepts arguments and required by your instructor, prompt the user to change stuff, and return a reference to the object passed into your function.

Student* StudentData(Student& S)
{
    cout << "Enter name: ";
    cin >> S.Name;

    cout << "Enter GPA: ";
    cin >> S.GPA;
    ....
    ....
    ....
    etc. etc.

    return &S;
}

So it's really not that difficult because I broke down each individual requirement into a task so small even a caveman could do it.

For the remainder of your assignment, feel free to perform each step to the best of your ability, then post your non-working code here for the rest of us to learn from.

why not comparing tow sturcture object

I don't know the way it needs to be is written and it's confusing to me since my brain is overwhelemed I had to write 3 programs in less then a week this is the last and for some reason I have a brain fart and can't put anything together I'm having trouble starting it. This is why I came here hopefully some can help.

#include <iostream>

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

int main () 
{
	Student*StudentData(Student& S)
	{
	  cout << "Enter name: ";
	  cin >> S.Name;
	
	  cout << "Enter GPA: ";
	  cin >> S.GPA;    
	}
	return 0;
}

how would I fill it in more

Why have you made no attempt at populating the 3rd attribute of your Student struct.?

I have worked a little on it but I feel lost and don't know what is next or how to write it I'm just lost I'll read some more and see what I can figure out but so far not much is working for me, the teacher isn't helping at all the only thing I have is the book but the teacher didn't take this out the book and is complicating things

I have spent around 6 hours looking at it and trying to understand it no luck no luck what so ever

well I've populated the first two attributes of the student struct for you.. let's see if you can at least make an attempt to populate the third attribute:

/* please, by all means.. just take a guess at how you would go about getting information from the user and storing into the 3rd (and last) attribute of the student struct: */

//Here is how we populated the first 2 attributes...

Student* StudentData(Student& S)
{
    cout << "Enter name: ";
    cin >> S.Name;

    cout << "Enter GPA: "
    cin >> S.GPA; 

     //At least take a guess at what would go here
}

when you make effort.. i'll make effort.

the code that you wrote how does it work as I cant seem to put the pieces together, im not sure if I have to write

cout << "please enter major" ;
cin >> S.Major;

by having the two that you put in there so would the one I wrote be the third

#include <iostream> struct Student{	char Name[30];	float GPA;	int Major;}; int main () {	Student*StudentData(Student& S)	{	  cout << "Enter name: ";	  cin >> S.Name; 	  cout << "Enter GPA: ";	  cin >> S.GPA;    	}	return 0;}#include <iostream>

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

int main () 
{
	Student*StudentData(Student& S)
	{
	  cout << "Enter name: ";
	  cin >> S.Name;
	
	  cout << "Enter GPA: ";
	  cin >> S.GPA;

                  cout << "Enter Major:";
                  cin >> S.Major;
    
	}
	return 0;
}
#include <iostream>
#include <iomanip>

struct Student
{
     char Name(30);
     float GPA;
     int Major;
};

int main()

{
    Student*StudentData(Student& S)

    {

       cout << "Enter Name: ";
       cint >> S.Name;

       cout << "Enter GPA: ";
       cin >> S.GPA;

       cout << "Enter Major: ";
       cint >> S.Major;
    }

return 0;

}

is this better and am I on the right track

How the code works should not be a mystery... but I will break it down for you.

What we are writing is called a "function".

The purpose of this function (as per your requirement) is to be able to accept an object of type 'student' and populate it's contents. We will then return a reference to the object (as per your requirement)

So, this is what we should have so far at step #2:

#include<iostream>
using namespace std;


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

//Function prototypes
Student* StudentData(Student& S);

int main()
{
     //Declare an object of type 'student'
     Student myStudent;

     //Pass this object into our function so it can be filled up with useful information
     StudentData(myStudent);

return 0;
}

//Function Definitions
Student* StudentData(Student& S)
{
     cout << "Enter name: ";
     cin >> S.Name;

     cout << "Enter GPA: ";
     cin >> S.GPA;

     cout << "please enter major" ;
     cin >> S.Major;

     return &S;
}

So at this point, we have a good working code that compiles and completes the requirements for step #2.. it isn't much, but it is something to build on.

Actually, this will satisfy the requirement for step #1 & #2:

#include<iostream>
using namespace std;


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

//Function prototypes
Student* StudentData(Student& S);

int main()
{
     //Declare two objects of type 'Student'
     Student* S1, S2;

     //Pass this object into our function so it can be filled up with useful information
     S1 = StudentData(S2);

return 0;
}

//Function Definitions
Student* StudentData(Student& S)
{
     cout << "Enter name: ";
     cin >> S.Name;

     cout << "Enter GPA: ";
     cin >> S.GPA;

     cout << "please enter major" ;
     cin >> S.Major;

     return &S;
}

Now we are at step #3. I know you are probably not an expert at c++, but sir, can you tell me in english how you would go about accomplishing the 3rd step of the task at hand...

wouldnt that satisfy #3 aswell as we have cout in there, and am I on the right track

and do I have to use <iomanip>

to get #3 would I have to use and if statement to bring the cout up

You do not have to use anything other than the <iostream> library to send output to the dos console.

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

We've wrote one function so far.

I want you to try writing another function.. one that will take a reference to a 'Student' type object and display it's contents. The function will look similar to the one we have just written; however, it will only contain cout << commands.

This will be your display function outline:

void display(Student& S)
{
     //Suprise me with your hidden c++ talents
     //Display all atributes of the 'student' object here...
}
void display(Student& S)
{
        cout << Student[].S1 << endl;
        cout << Student[].S2 << endl;
       

}

Take another look at how data is formatted inside of our StudentData() function and try again....

(and why are you using array subscripts..??!??!)

Like I said before, the display() function is going to look very similar to the StudentData() function (but since we are displaying only, it will only use cout statements)... so don't over complicate this shizzle.

do i have to use if and count or just cout, is what i wrote even close

  • What you wrote, is not close.
  • Please.. take another look at the StudentData() function.
  • It will be almost identical to our display function BUT WITHOUT THE CIN'S.
  • Take a look at my code. Your code should look like my code.
  • Once you are comfortable with how a normal c++ program should look, feel free to attempt to write a "display()" function.
  • The function will accept a reference to a 'Student' type object.
  • The function will 'cout' all attributes of the Student object.
  • As soon as you can do this, we can move on to step #4.
  • But for now, we are stuck at step #3.

Now amaze us with your coding ability.

#include<iostream>

using namespace std;

 

 

struct Student

{

     char Name[30];

     float GPA;

     int Major;

};

 

//Function prototypes

Student* StudentData(Student& S);

 

int main()

{

     //Declare two objects of type 'Student'

     Student* S1, S2;

 

     //Pass this object into our function so it can be filled up with useful information

     S1 = StudentData(S2);

 

return 0;

}

 

//Function Definitions

Student* StudentData(Student& S)

{

     cout << "Enter name: ";

     cin >> S.Name;

 

     cout << "Enter GPA: ";

     cin >> S.GPA;

 

     cout << "please enter major" ;

     cin >> S.Major;

 

     return &S;

}




void display(Student& S)

{

        cout << S.Name << endl;

        cout << S.GPA << endl;

	      cout << S.Major << endl;

 

 

}

i am trying to find something in the book to remember how to write it and many things that I see and codes are using an for and index

but number 3 wants s1 and s2 but in the code those are written student

i think the reason why im having so much trouble is because its due today and i am rushing to remember but cant

i think the reason why im having so much trouble is because its due today and i am rushing to remember but cant

this isn't my fault.

Thank you. So this is how our program looks 3 steps into your project. It isn't much, but it is a working good program that compiles with no errors and gives us expected repeatable performance:

#include<iostream>
using namespace std;


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

//Function prototypes
Student* StudentData(Student* S);
void display(Student* S);

int main()
{
     //Declare two objects of type 'Student'
     Student* S1 = new Student;
     Student* S2 = new Student;

     //Pass this object into our function so it can be filled up with useful information
     S1 = StudentData(S2);
     //Display new and exciting information to the DOS console
     display(S1);
     display(S2);


return 0;
}


//Function Definitions
Student* StudentData(Student* S)
{
     cout << "Enter name: ";
     cin >> S->Name;

     cout << "Enter GPA: ";
     cin >> S->GPA;

     cout << "please enter major" ;
     cin >> S->Major;

     return S;
}

void display(Student* S)
{
    cout << S->Name  << endl;
    cout << S->GPA   << endl;
    cout << S->Major << endl;
}

I tried to avoid having everything pointered out... but I think that's the way we are going to have to go with this.

Ok, so we are at step #4:


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…)

This part I will make so easy for you....

JUST COPY AND PASTE THE STUDENTDATA() FUNCTION AND CHANGE THE NAME TO CHANGEDATA().

It's the same exact function, just a different name. Does the same exact thing.

Once you are done with this, we can move onto step #5.

But for now, we are stuck at step #4.

no I apretiate you helping this means a lot, no one else is willing not even the teacher helps so thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.