Hello, I'm new to this thread but have referenced to it before many times. I have finally joined thanks to all the great responses I have read in the past.
Okay, I recieved an assignment which is as follow...
"Declare a dynamic array of Person to store the information of each student. Implement a simple user interface that allows the enduser of your program to: (1) type in the total number of students at runtime from the keyboard; and (2) provide the specific value of every data member for each student. Then implement the counting sort algorithm to sort all the students in increasing order of age. You can assume a personʼs age is an integer and within a small interval, say, 15--90. For comparison purpose, print out the unsorted array before calling the sorting procedure and the sorted one after.
Note: Person is implemented as a struct and consists of: firstname, lastname, age,
birthdate, and salary.
birthdate is of type date which is implemented as a struct and consists of:
day, month, and year.
-----------------------------------------------------------------------------------------
My code this is what I have done so far
/******************************************************************************
Person Header File
*******************************************************************************/
#ifndef _PERSON_H
#define _PERSON_H
#include<iostream>
#include<string>
using namespace std;
struct Date {
unsigned short day; //1-31
unsigned short month; //1-12
int year;
};
struct Person {
string fname; //first name
string lname; //last name
unsigned short age; //person's age
Date birthDay; //mm/dd/year
double salary; //person's salary
};
Person init_Person(string& fname, string& lname,
unsigned short& age, Date& bdate, double& salary);
#endif
/******************************************************************************
Person Implementation File
*******************************************************************************/
#include "Person.h"
//Description: implementation of init_Person. Initialize a Person object
Person init_Person(string& fname, string& lname,
unsigned short& age, Date& bdate, double& salary)
{
Person someone; //someone is object of Person
someone.fname = fname;
someone.lname = lname;
someone.age = age;
someone.birthDay.day = bdate.day;
someone.birthDay.month = bdate.month;
someone.birthDay.year = bdate.year;
someone.salary = salary;
return someone;
}//end-of-init_Person(...)
/******************************************************************************
Person Test-Main File
*******************************************************************************/
#include "Person.h"
int main()
{
string yourName; //Stores the name of the user
int stuNum; //Stores the # of students
cout << "Please enter your name: ";
getline(cin,yourName);
cout << "Welcome, " << yourName << "!" << endl
<< "Please enter the number of students: ";
cin >> stuNum;
cout << endl;
Person *darrPer;
darrPer = new Person[stuNum]; //Dynamic array of type person
int i; //Index of the dynamic array
for(i = 0; i < stuNum; i++)
{
string firNam, lasNam; //User entered data
unsigned short ageOld;
Date burDay;
double salMon;
cout << "Enter the first name: ";
cin >> firNam;
cout << "Enter the last name: ";
cin >> lasNam;
cout << "Enter the age: ";
cin >> ageOld;
cout << "Enter the birth day: ";
cin >> burDay.day;
cout << "Enter the birth month: ";
cin >> burDay.month;
cout << "Enter the birth year: ";
cin >> burDay.year;
cout << "Enter the salary: ";
cin >> salMon;
darrPer[i] = init_Person(firNam,lasNam,ageOld,burDay,salMon);
cout << "\n" << endl;
}
//**************************THIS IS WHERE THE PROBLEM OCCURS*******************//
for(i = 0; i < stuNum; i++)
cout << darrPer[i] << endl;
int k = 90; //Assumed age range max at 90 in description of the problem.
int j;
Person *tempPer, *sortPerAge;
tempPer = new Person[stuNum];
for(i = 0; i < k; i++)
tempPer[i] = 0;
for(j = 1; j < stuNum; j++)
tempPer[darrPer.age[j]] = tempPer[darrPer.age[j]] + 1;
for(i = 1; i < k; i++)
tempPer[i] = tempPer[i] + tempPer[i-1];
for(j = stuNum; j > 0; j--)
{
sortPerAge[tempPer[darrPer.age[j]]] = darrPer.age[j];
tempPer[darrPer.age[j]] = tempPer[darrPer.age[j]] - 1;
}
delete [] sortPerAge;
delete [] tempPer;
delete [] darrPer;
return 0;
}//end-of-main()
Okay the error(s) I get from the compiler:
1)test-MainPerson.cpp no match for 'operator<<' in 'std::cout << *((+(((unsigned int)i) * 32u)) + darrPer)
2)Dev-Cpp\include\c++\3.4.2\bits\ostream.tcc:63 candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
3):\Dev-Cpp\include\c++\3.4.2\bits\ostream.tcc:63 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
.
.
.
.
.
The errors continue. Way too many.
Any insight will be greatly appreciated.