I have an assignment that asks to create a program to input students names & test scores. After which the user gets the option to calculate the average and print out which students were below, and/or print out the highest score and which students had that score.

Since the total # of students is unknown I'm using vectors in order to allow this. My problem at the moment is how to combine both char input and double input into a vector container and be able to generate the scores. I've already scrapped 3 different versions of my code since I hit a road block each time. I ideally want to create 2 classes (one for student names and the other for scores), but not sure how to bring those values from the class into the vector in the main program file.

Actually come to think of it, I can create a tmp1 for the name & tmp2 for the grade and add both to a container, but then I'm back at my original question on how to combine char with a integer (well double since scores may use decimals).

Any help would be greatly appreciated.

Thanks

I have an assignment that asks to create a program to input students names & test scores. After which the user gets the option to calculate the average and print out which students were below, and/or print out the highest score and which students had that score.

Since the total # of students is unknown I'm using vectors in order to allow this. My problem at the moment is how to combine both char input and double input into a vector container and be able to generate the scores. I've already scrapped 3 different versions of my code since I hit a road block each time. I ideally want to create 2 classes (one for student names and the other for scores), but not sure how to bring those values from the class into the vector in the main program file.

Actually come to think of it, I can create a tmp1 for the name & tmp2 for the grade and add both to a container, but then I'm back at my original question on how to combine char with a integer (well double since scores may use decimals).

Any help would be greatly appreciated.

Thanks

How about using a struct?

struct student
{
    string name;
    double score;
};

vector <student> students;

Or if a student can have more than one test score:

struct student
{
    string name;
    vector <double> scores;
    double average;
};

vector <student> students;

How about using a struct?

struct student
{
    string name;
    double score;
};

vector <student> students;

Or if a student can have more than one test score:

struct student
{
    string name;
    vector <double> scores;
    double average;
};

vector <student> students;

How would I put that in the class file so that I can call it from my main program file?

I do know structs are an alternative to classes, but I have to use at least 1 class in the program.

How would I put that in the class file so that I can call it from my main program file?

I do know structs are an alternative to classes, but I have to use at least 1 class in the program.

Well, it depends on how you want to organize things, but I suppose the easiest would be this. Just change the word "struct" to "class" and add the word "private" at the top if you want the data members to be private:

class student
{
    private:
        string name;
        double score;

        // private functions

    public:
        // constructors and other public functions
};

Classes are same as structs with the exception of default private and public.

Classes are same as structs with the exception of default private and public.

That's a good C++ definition, but that doesn't hold true for pure C.

It was a very interesting remark especially taking into account:
1. No classes in pure C
2. It's C++ forum.
;)

commented: You got me XP +3

It was a very interesting remark especially taking into account:
1. No classes in pure C
2. It's C++ forum.
;)

And this is why you are getting reputation, for noting my mistake of thinking he compared structs with classes =P

Here is what I have so far for my class source file;

#include <iostream>
#include <vector>
#include <string>
#include "students.h"

using namespace std;

void students::getData()
{
	int numStudents = 0;
	string lname;
	string fname;
	int grade = 0;
	int i = 0;
	int avg = 0;
	int choice;
do
{
	cout << " Welcome to Student Records "<<endl;
                cout << " Please make selection "<<endl;
 	cout << " 1 - Add students "<<endl;
	cout << " 2 - Students below average "<<endl;
	cout << " 3 - Students with highest score "<<endl;
	cout << " 4 - Quit program "<<endl;
	cin >> choice;

	if (choice==1)
	{
		cout << " How many students do you wish to input? : ";
	cin >> numStudents;

	vector<getData> students;

	do
	{
		cout << " Student " << i+1 <<" last name: ";
		cin >> lname;
		students.push_back(lname);

		cout << " Student " << i+1 <<" first name: ";
		cin >> fname;
		students.push_back(fname);

		cout << " Student " << i+1 <<" test score: ";
		cin >> grade;
		students.push_back(grade);
		i++;
		avg = grade / numStudents;

	}
	while (i < numStudents);
	}
	
	if (choice==2)
	{
		cout << " The average score is: "<< avg <<endl;
	}

	if (choice==3)
	{
	}

	if (choice==4)
	{
		cout << " Now Exiting Program "<<endl;
	}
}
while (choice !=4);
}

my main source file just activates the getdata and the rest is run from the class file.

What I ultimatly want it to do is create a single vector that containes both the students first & last name plus their test score. Then allow the user to pull up the list of students who were below the average and display it, and also display the highest score and those students that scored it.

What am I missing here??

my main source file just activates the getdata and the rest is run from the class file.

What I ultimatly want it to do is create a single vector that containes both the students first & last name plus their test score. Then allow the user to pull up the list of students who were below the average and display it, and also display the highest score and those students that scored it.

What am I missing here??

What I ultimatly want it to do is create a single vector that containes both the students first & last name plus their test score.

This is easy, once you have a class/struct establish with properties of
string first and last name and double test score.

Then just create a vector of the class/struct.

Then allow the user to pull up the list of students who were below the average and display it, and also display the highest score and those students that scored it.

This is easy, just ask the user to input the average he/she wanted
then call a function passing in the average.
Then the function will create a newly vector and then you would run through/iterate the vector of students and if student's score is below the average and add to that vector.

Return the vector and display it to the user.

Same situation for highest score.

Ok I went back to the drawing board a little bit and I believe I'm starting to see the light at the end of the tunnel. But now my problem is displaying the contents of the vector.

Here is my new code and at this point I assume it stores the data I input into the vector, but until I can have it output the contents I'm not positive.

#include <iostream>
#include <string>
#include <vector>
#include "student.h"

using namespace std;

int main()
{
	int numStudents = 0;
	int select;

	student mySTD;
	vector<student> std;
	
	cout << " Student scoring system! " << endl;
	cout << endl;
	cout << " Please make selection: " << endl;
	cout << " 1 - Add student " << endl;
	cout << " 2 - Get class average " << endl;
	cout << " 3 - Get high score " << endl;
	cin >> select;

	if (select==1)
	{
		mySTD.getData();
		std.push_back(mySTD);
		cout<<std
	}

	if (select==2)
	{
		cout << " Option 2 " << endl;
	}

	if (select==3)
	{
		cout << " Option 3 " << endl;
	}
}
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.