Hello!
So for my assignment I had to declare an array of 25 student objects and input data into them including another array of 5 test scores.
All that is done and works flawlessly(At least that's what I think), but the last portion of the assignment is for me to design a stringstream and print function to print out all of the data in a tabular format. I have never used stringstream before, but my main problem is formatting and finding a way for the print and display(stringstream) functions to take in and output the correct data.
Here's my code so far:
MAIN:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "Student.h"
const int MAX = 25;
//void printData(Student userData[]);
void getData(Student userData[]);
bool newStudent(Student userData[], int index);
int main(int argc, char *argv[])
{
Student userData[MAX];
getData(userData);
void printData();
system("PAUSE");
return EXIT_SUCCESS;
}
void getData(Student userData[])
{
bool done = false;
int index = Student::getCount();
while (index < MAX && !done)
{
while(newStudent(userData, index))
{
Student::addCount();
index++;
}
done = true;
}
}
bool newStudent(Student userData[], int index)
{
int sX;
int sS;
string fN;
string lN;
cout << "Students currently entered: " << index << endl;
cout << "Enter student ID (ctrl-z to quit): ";
cin >> sX;
if(cin.eof() || cin.fail())
{
system("cls");
return false;
}
cin.ignore();
cout << "Enter student's first name: ";
getline(cin, fN);
cout << "Enter student's last name: ";
getline(cin, lN);
userData[index].setId(sX);
userData[index].setFirst(fN);
userData[index].setLast(lN);
cout << "Please enter 5 test scores (seperate with spaces): ";
for(int i = 0; i < 5; i++)
{
cin >> sS;
userData[index].setScores(sS,i);
}
system("cls");
return true;
}
HEADER FILE:
using namespace std;
#ifndef STUDENT_CLASS
#define STUDENT_CLASS
class Student
{
private:
int studentId;
string firstName;
string lastName;
int scores[5];
double average;
char grade;
static int count;
double calcAverage();
public:
Student();
void setId(int sX);
void setFirst(string fN);
void setLast(string lN);
void setAverage(double sA);
void setGrade(char sG);
void setScores(int sS, int i);
int getId() const;
string getFirst() const;
string getLast() const;
int getScores() const;
char getGrade() const;
double getAverage() const;
static int getCount();
static void addCount() { count++; };
string displayData();
};
#endif
IMPLEMENTATION FILE:
#include<iostream>
#include<iomanip>
#include<string>
#include<sstream>
using namespace std;
#include "Student.h"
int Student :: count = 0;
//Default Constructor
Student::Student()
{
int sX = 0;
string fN = "";
string lN = "";
for(int i = 0; i < 5; i++)
scores[i] = 0;
double sA = 0.0;
char sG = ' ';
}
//================================Set Mutator Function declarations=============================================
void Student::setId(int sX)
{
studentId = sX;
}
void Student::setFirst(string fN)
{
firstName = fN;
}
void Student::setLast(string lN)
{
lastName = lN;
}
void Student::setAverage(double sA)
{
average = sA;
}
void Student::setScores(int sS, int i)
{
scores[i] = sS;
}
//================================Get Accessor Function declarations=============================================
int Student::getId() const
{
return studentId;
}
string Student::getFirst() const
{
return firstName;
}
string Student::getLast() const
{
return lastName;
}
double Student::getAverage() const
{
return average;
}
char Student::getGrade() const
{
if(average >= 90.0)
grade == 'A';
return grade;
if(average >= 80.0)
grade == 'B';
return grade;
if(average >= 70.0)
grade == 'C';
return grade;
if(average >= 60.0)
grade == 'D';
return grade;
if(average < 60.0)
grade == 'F';
return grade;
}
int Student::getCount()
{
return count;
}
double Student::calcAverage()
{
double avg;
avg = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4]) / 5.0;
}
Okay so as you can see I have not even set up a string stream function as of yet. I do have a print method prototype up in main, but I'm unsure how to implement it along with the stringstream.
I'm guessing the function would look something like this (according to what I've read):
string Student :: displayData(void)
{
calcAverage();
stringstream strstr;
strstr << left << setw(3) << studentId
<< right << setw(3) << lastName << ", "
<< left << setw(10) << firstName
<< right << setw(10) << scores[0]
<< right << setw(10) << scores[1]
<< right << setw(10) << scores[2]
<< right << setw(10) << scores[3]
<< right << setw(10) << scores[4]
<< right << setw(3) << average
<< right << setw(3) << getGrade();
return strstr.str();
}
Obviously the setw values are just placeholders. Some guidance would be extremely helpful!
Thank you!