unresolved external symbol problems? - c++

So first time posting and real beginner so hope doing this right but really need help with this program.
Thanks guys for all the help, I have made some changes and those errors have gone away, but now I am getting these:
Error 5 error LNK2019: unresolved external symbol "void __cdecl searchID(struct StudentRecord *,int)" (?searchID##YAXPAUStudentRecord##H#Z) referenced in function _main
Error 6 error LNK2019: unresolved external symbol "void __cdecl sortRecordsByName(struct StudentRecord *,int)" (?sortRecordsByName##YAXPAUStudentRecord##H#Z) referenced in function _main
Error 7 error LNK2019: unresolved external symbol "void __cdecl getInformation(struct StudentRecord * const,int)" (?getInformation##YAXQAUStudentRecord##H#Z) referenced in function _main
Error 8 error LNK1120: 3 unresolved externals
Here is the revised code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int MAX_SIZE = 20;
struct Answers
{
char answer1;
char answer2;
char answer3;
char answer4;
char answer5;
};
struct StudentRecord
{
int ID;
char student_name[MAX_SIZE];
Answers answer;
double score;
double average;
char letter_grade;
};
void getInformation(StudentRecord student[], int&);
void enterKey(char[]);
void calculateAvgAndLetter(StudentRecord *student, int, char[]);
void sortRecordsByName(StudentRecord * student, int);
void displayResults(StudentRecord * student, int);
bool displayReport(StudentRecord * student, int);
void searchID(StudentRecord * student, int);
int main()
{
int search;
int ID = 0;
bool check = false;
char repeat = 'y';
const int MAX_STUDENTS = 10;
int number_of_students = 0;
char key[5];
do {
cout << "How many students: ";
cin >> number_of_students;
StudentRecord student[MAX_STUDENTS];
enterKey(key);
getInformation(student,number_of_students);
calculateAvgAndLetter(student, number_of_students, key);
sortRecordsByName(student, number_of_students);
displayResults(student, number_of_students);
cout << "Would you like to search for a student (y for yes and n for no)?: ";
cin >> search;
while (search != 'n' && search != 'y')
{
cout << "Wrong ID" << endl;
cout << "Would you like to search for another student (y for yes and n for no)?: ";
cin >> search;
}
if (search == 'y')
{
searchID(student, number_of_students);
check = true;
}
else
check = false;
while (check)
{
cout << "Would you like to search for another student (y for yes and n for no)?: ";
cin >> search;
while (search != 'n' && search != 'y')
{
cout << "Wrong ID" << endl;
cout << "Would you like to search for another student (y for yes and n for no)?: ";
cin >> search;
}
if (search == 'y')
{
searchID(student, number_of_students);
check = true;
}
else
check = false;
}
cout << "Would you like to process another group of students(y for yes and n for no)?: " << endl;
cin >> repeat;
} while (repeat == 'y');
return 0;
}
void enterKey(char key[5])
{
for (int i = 0; i < 6; i++) {
cout << "Please enter the answer to question " << i + 1 << ": ";
cin >> key[i]; }
}
void getInformation(StudentRecord *student[],int number_of_students)
{
int max_students;
char again;
int i = 0;
cout << "Would you like to enter student information? " << endl;
cin >> again;
if (again == 'Y' || again == 'y')
{
do
{
cout << "Please enter student " << i + 1 << " information:" << endl;
cout << "Please Enter Student Name Last name first with no spaces: " << endl;
cin >> student[i]->student_name;
cout << "Please Enter Student Id Number: " << endl;
cin >> student[i]->ID;
cout << "Please enter the student's answer to question 1: ";
cin >> student[i]->answer.answer1;
cout << "Please enter the student's answer to question 2: ";
cin >> student[i]->answer.answer2;
cout << "Please enter the student's answer to question 3: ";
cin >> student[i]->answer.answer3;
cout << "Please enter the student's answer to question 4: ";
cin >> student[i]->answer.answer4;
cout << "Please enter the student's answer to question 5: ";
cin >> student[i]->answer.answer5;
i++;
if (i < number_of_students) {
cout << "Would you like to enter student information? " << endl;
cin >> again;
}
else again='n';
}
while(again == 'y' || again == 'Y');
cout<<"Reports:"<<endl;
}
}
void sortByID(StudentRecord student[], int number_of_students)
{
bool swap = true;
int j = 0;
int temp;
while (swap)
{
swap = false;
j++;
for (int i = 0; i < number_of_students - j; i++)
{
if (student[i].ID > student[i + 1].ID)
{
temp = student[i].ID;
student[i].ID = student[i + 1].ID;
student[i + 1].ID = temp;
swap = true;
}
}
}
}
int LinearSearch(StudentRecord student[], int number_of_students, int ID, int first, int last)
{
int i = 0;
int position;
for (int i = 0; i < number_of_students; i++)
{
if (ID == student[i].ID) {
position = i; }
}
return position;
}
void SortRecordsByName(StudentRecord student[], int number_of_students)
{
bool swap = true;
int j = 0;
StudentRecord temp;
while (swap)
{
swap = false;
j++;
for (int i = 0; i < number_of_students - j; i++)
{
if (student[i].student_name > student[i + 1].student_name)
{
strcpy(temp.student_name,student[i].student_name);
strcpy(student[i].student_name,student[i + 1].student_name);
strcpy(student[i + 1].student_name,temp.student_name);
swap = true;
}
}
}
}
void calculateAvgAndLetter(StudentRecord student[], int number_of_students, char key[5])
{
int points1;
int points2;
int points3;
int points4;
int points5;
int i = 0;
int j = 0;
for (int i = 0; i < number_of_students; i++)
{
if (student[i].answer.answer1 == key[0])
points1 = 10;
else
points1 = 0;
if (student[i].answer.answer2 == key[1])
points2 = 10;
else
points2 = 0;
if (student[i].answer.answer3 == key[2])
points3 = 10;
else
points3=0;
if (student[i].answer.answer4 == key[3])
points4 = 10;
else
points4 = 0;
if (student[i].answer.answer5 == key[4])
points5 = 10;
else
points5 = 0;
student[i].score= points1 + points2 + points3 + points4 + points5;
student[i].average = student[i].score * 2 ;
if((student[i].average >= 90) && (student[i].average <= 100))
student[i].letter_grade='A';
else if ((student[i].average >= 80) &&(student[i].average <= 89))
student[i].letter_grade='B';
else if ((student[i].average >= 70) && (student[i].average <= 79))
student[i].letter_grade='C';
else if ((student[i].average >= 60) && (student[i].average <= 69))
student[i].letter_grade='D';
else if ((student[i].average >= 0) && (student[i].average <= 59))
student[i].letter_grade='F'; }
}
void displayResults(StudentRecord student[], int number_of_students)
{
cout << fixed<< setprecision(2);
cout << "Student ID" << setw(10) << "StudentName" << setw(10) << "Answers" << setw(10) <<"Total Pts" << setw(10) << "Average" << setw(12) << "Letter Grade"<<endl;
cout << setfill('-');
cout << setw(50) << "-" << endl;
cout << setfill(' ');
for (int i = 0; i < number_of_students; i++)
cout << setw(3) << student[i].ID << setw(15) << student[i].student_name << setw(10) << student[i].answer.answer1 << student[i].answer.answer2 << student[i].answer.answer3 << student[i].answer.answer4
<< student[i].answer.answer5 << setw(9) << student[i].score << setw(10) << student[i].average << setw(13) << student[i].letter_grade << endl;
cout <<"\n\n\nStudents admitted to graduate program: \n" << endl;
cout << fixed<< setprecision(2);
cout << "Student ID" << setw(10) << "StudentName" << setw(10) <<"Total Pts" << setw(10) << "Average" << setw(12) << "Letter Grade"<<endl;
cout << setfill('-');
cout << setw(40) << "-" << endl;
cout << setfill(' ');
for (int i = 0; i < number_of_students; i++)
{
if (student[i].letter_grade == 'A' || student[i].letter_grade == 'B') {
cout << setw(2) << student[i].ID << setw(12) << student[i].student_name << setw(10) << student[i].score
<< setw(10) << student[i].average << setw(10) << student[i].letter_grade << endl; }
}
cout <<"\n\n\nStudents with Conditional Admission to Graduate Program: \n" << endl;
cout << fixed<< setprecision(2);
cout << "Student ID" << setw(10) << "StudentName" << setw(10) <<"Total Pts" << setw(10) << "Average" << setw(12) << "Letter Grade"<<endl;
cout << setfill('-');
cout << setw(40) << "-" << endl;
cout << setfill(' ');
for (int i = 0; i < number_of_students; i++)
{
if (student[i].letter_grade == 'C') {
cout << setw(2) << student[i].ID << setw(10) << student[i].student_name << setw(10) << student[i].score
<< setw(10) << student[i].average << setw(10) << student[i].letter_grade << endl; }
}
cout <<"\n\n\nStudents Not Allowed Admission: \n" << endl;
cout << fixed<< setprecision(2);
cout << "Student ID" << setw(10) << "StudentName" << setw(10) <<"Total Pts" << setw(10) << "Average" << setw(12) << "Letter Grade"<<endl;
cout << setfill('-');
cout << setw(40) << "-" << endl;
cout << setfill(' ');
for (int i = 0; i < number_of_students; i++)
{
if (student[i].letter_grade == 'D' || student[i].letter_grade == 'F') {
cout << setw(2) << student[i].ID << setw(10) << student[i].student_name << setw(10) << student[i].score
<< setw(10) << student[i].average << setw(10) << student[i].letter_grade << endl; }
}
}
void searchIDandDisplay(StudentRecord student[], int number_of_students, int ID)
{
bool check = true;
string acceptence;
cout << "\n\n\nEnter the ID of the student: ";
cin >> ID;
while (check)
{
for(int i = 0; i < number_of_students; i++)
{
if (ID == student[i].ID)
{
check = false;
}
}
if (check == true)
{
cout << "No student with this ID" << endl;
cin >> ID;
}
}
int i = LinearSearch(student, number_of_students, ID, student[0].ID, student[number_of_students].ID);
if (student[i].letter_grade == 'A' || student[i].letter_grade == 'B' || student[i].letter_grade == 'C') {
acceptence = "Accepted"; }
else
acceptence = "Denied";
cout << fixed << setprecision(2);
cout << "Student ID" << setw(10)
<< "StudentName"
<< setw(10) << setw(10)
<<"Total Pts" << setw(10)
<< "Average" << setw(10)
<< "Letter Grade"
<< "Status" << endl;
cout << setfill('-');
cout << setw(48) << "-" << endl;
cout << setfill(' ');
cout << setw(2) << student[i].ID
<< setw(10) << student[i].student_name
<< setw(10) << student[i].score
<< setw(10) << student[i].average
<< setw(10) << student[i].letter_grade
<< setw(10) << acceptence <<endl;
}
Like I said a real beginner so any help will be greatly appreciated, and hope I posted right, sorry if anything wrong. And again thanks guys for any help!

void getInformation(StudentRecord *student[], int& num_std);
This says getInformation takes an array of pointers to StudentRecord. You want just
void getInformation(StudentRecord student[], int& num_std);
which takes an array of StudentRecords.

void getInformation(StudentRecord *student[], int& num_std);
In this declaration, student is taken as a pointer to pointer to StudentRecord. Change it to:
void getInformation(StudentRecord *student, int& num_std);
or
void getInformation(StudentRecord student[], int& num_std);

The problem is this line
getInformation(student,number_of_students);
The type of student here is a StudentRecord[] but the parameter of getInforamtion is typed to StudentRecord[]*. In order to make the types line up you need to change the type of the parameter to StudentRecord* or pass the address of student.
Given that you are passing the size along with the set of values I would favor changing the signature of getInformation to
void getInformation(StudentRecord *student, int& num_std);

Some comments
int search;
int ID = 0;
bool check = false;
char repeat = 'y';
const int MAX_STUDENTS = 5;
int number_of_students = 0;
char key[5];
do {
cout << "How many students: ";
cin >> number_of_students;
you should check if (number_of_students < MAX_STUDENTS) before continuing
your array key has dimension 5 but in your for loop you go from 0..5 which is six, better still give the dimension as an argument to the function enterKey(char *key, int maxsize) { ... }
void enterKey(char key[5]) -> (char* key, int maxsize)
{
for (int i = 0; i < 6; i++) -> for (int i=0; i<maxsize; ++i)
{
cout << "Please enter the answer to question " << i + 1 << ": ";
cin >> key[i];
}
}
the formal argument specifies an array of StudentRecord pointers
void getInformation(StudentRecord *student[],int number_of_students)
but you do not supply that in this way:
StudentRecord student[MAX_STUDENTS];
enterKey(key);
getInformation(student,number_of_students);
if you write as above then student is an array of StudentRecords
instead you should change the prototype to look like this (remember array decays to ptr):
void getInformation(StudentRecord *student,int number_of_students)
another tip
your struct Answers has 5 answer characters answer1,answer2,.. it would be more convenient to have it as an array, then you could shorten down the way you input the answers

Your prototypes don't need names for their arguments. They just need the data types.
void getInformation(StudentRecord*, int&);
void enterKey(char[]);
void calculateAvgAndLetter(StudentRecord*, int, char[]);
void sortRecordsByName(StudentRecord*, int);
void displayResults(StudentRecord*, int);
bool displayReport(StudentRecord*, int);
void searchID(StudentRecord*, int);
Your prototype for getInformation:
void getInformation(StudentRecord *student[], int& num_std);
has its second parameter typed as int&, whereas your definition:
void getInformation(StudentRecord *student[],int number_of_students)
is missing the &. In this case, you could either remove the & from the prototype or add it to the definition and your current implementation should work the same.
And as others have said, you're overstating the first parameter. Try Removing either the [] or the * (arrays are always passed by reference anyway).

Related

Error: expected primary-expression before ']' token. Need assitance with function definition and calling

This is my first semester of computer science, and I need help with my first project. So far, it's still a mess, and I am mainly doing test cases to ensure basic things like my functions work.
The goal of the project is to ask the user how many robots they want to make, name them, then they can use the robot's name (their unique identifier) to move the robots along an X-Y axis, which is really just the program adding or subtracting to a .Xvalue or .Yvalue.
My MenuFunction works, but I am having trouble with the MoveFunction. Basically, I want the MoveFunction to ask the user which robot they want to use, go through the RobotArray, and print the Robot's name once found.
Again, this is just a test case so I can better understand the coding. Right now, I am getting two errors:
main.cpp:42:33: error: expected primary-expression before ‘]’ token
42 | string MoveFunction(RobotArray[], robotName, NumberOfRobots);
main.cpp:62:16: error: no match for call to ‘(std::string {aka std::__cxx11::basic_string}) ()’
62 | MoveFunction();
I don't know what to do for the first error, but I think the latter is due to my not having any objects in the function call, but I wouldn't know what to put in there anyway.
My complete code is below:
#include <iostream>
using namespace std;
struct userRobot
{
string name;
int Xvalue = 0;
int Yvalue = 0;
};
void MenuFunction()
{
cout << "Welcome to MultiRobo Guider." << endl;
cout << "Please select:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
int main()
{
int NumberOfRobots;
string robotName;
cout << "Enter the number of robots" << endl;
cin >> NumberOfRobots;
cout << endl << "Enter their name(s)" << endl;
userRobot RobotArray[NumberOfRobots];
for (int i = 0; i < NumberOfRobots; i++)
{
cin >> robotName;
RobotArray[i].name = robotName;
}
cout << endl;
for (int j = 0; j < NumberOfRobots; j++)
{
cout << RobotArray[j].name << "'s position is ";
cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
}
string MoveFunction(RobotArray[], robotName, NumberOfRobots);
{
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++)
{
if (robotName == RobotArray[k].name)
{
cout << RobotArray[k].name;
}
}
}
MenuFunction();
char input;
cin >> input;
if (input == 'm')
{
cout << "Which robot would you like to move?";
MoveFunction();
}
else if (input == 'd')
{
cout << "distance";
}
else if (input == 'q')
{
cout << "quit";
}
}
First off, userRobot RobotArray[NumberOfRobots]; is a variable-length array, which is a non-standard extension supported by only a few compilers. To create an array whose size is not known until runtime, you should use new[] instead, or better std::vector.
That said, your main issue is that you are trying to define your MoveFunction() function inside of your main() function, which is not allowed. But, even if it were, you are not declaring it correctly. It has an erroneous ; on it. And RobotArray, robotName, and NumberOfRobots are not types, but variables. Like a variable, a function parameter always starts with a type. There are no untyped parameters/variables in C++.
You need to either:
move MoveFunction() outside of main(), and fix its declaration to use proper types, eg:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct userRobot
{
string name;
int Xvalue = 0;
int Yvalue = 0;
};
void MenuFunction()
{
cout << "Welcome to MultiRobo Guider." << endl;
cout << "Please select:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
void MoveFunction(userRobot RobotArray[], int NumberOfRobots)
{
cout << "Which robot would you like to move?";
string robotName;
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++)
{
if (robotName == RobotArray[k].name)
{
cout << RobotArray[k].name << endl;
return;
}
}
cout "Robot not found" << endl;
}
int main()
{
cout << "Enter the number of robots" << endl;
int NumberOfRobots;
cin >> NumberOfRobots;
vector<userRobot> RobotArray(NumberOfRobots);
cout << endl << "Enter their name(s)" << endl;
for (int i = 0; i < NumberOfRobots; i++)
{
cin >> RobotArray[i].name;
}
cout << endl;
for (int j = 0; j < NumberOfRobots; j++)
{
cout << RobotArray[j].name << "'s position is ";
cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
}
MenuFunction();
char input;
cin >> input;
if (input == 'm')
{
MoveFunction(RobotArray.data(), NumberOfRobots);
}
else if (input == 'd')
{
cout << "distance";
}
else if (input == 'q')
{
cout << "quit";
}
}
or change MoveFunction() into a lambda, eg:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct userRobot
{
string name;
int Xvalue = 0;
int Yvalue = 0;
};
void MenuFunction()
{
cout << "Welcome to MultiRobo Guider." << endl;
cout << "Please select:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
int main()
{
cout << "Enter the number of robots" << endl;
int NumberOfRobots;
cin >> NumberOfRobots;
vector<userRobot> RobotArray(NumberOfRobots);
cout << endl << "Enter their name(s)" << endl;
for (int i = 0; i < NumberOfRobots; i++)
{
cin >> RobotArray[i].name;
}
cout << endl;
for (int j = 0; j < NumberOfRobots; j++)
{
cout << RobotArray[j].name << "'s position is ";
cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
}
auto MoveFunction = [&]{
cout << "Which robot would you like to move?";
string robotName;
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++)
{
if (robotName == RobotArray[k].name)
{
cout << RobotArray[k].name << endl;
return;
}
}
cout "Robot not found" << endl;
};
MenuFunction();
char input;
cin >> input;
if (input == 'm')
{
MoveFunction();
}
else if (input == 'd')
{
cout << "distance";
}
else if (input == 'q')
{
cout << "quit";
}
}
You have a semi-colon at the end of MoveFunction() definition, In C++ when you define a function the name of the function does not end with a semi-colon.

Keep receiving this error main.cpp:9:91: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘const std::vector’

Sorry in advance if this is lengthy. the problem is within line 9. (where the 2nd cout is.) apparently, but i'm new to this so I can't identify exactly what the issue is.
#include <iostream>
#include <vector>
using namespace std;
void outputRoster(const vector<int> &jersey, const vector<int> &ratings) {
cout << "ROSTER" << endl;
for (int i =1; i < jersey.size(); ++i) {
cout << "Player " << i << " -- Jersey number: " << jersey.at(i-1) << ", Rating: " << ratings << endl;
}
cout << endl;
}
void addPlayer(vector<int> &jersey, vector<int> &ratings) {
int num;
cout << "Enter another player's jersey number: ";
cin >> num;
jersey.push_back(num);
cout << "Enter another player's ratings: ";
cin >> num;
cout << endl;
ratings.push_back(num);
}
void removePlayer(vector<int> &jersey, vector<int> &ratings) {
int num;
cout << "Enter a jersey number: ";
cin >> num;
cout << endl;
for (int i = 0; i < jersey.size(); ++i){
if (jersey.at(i)==num){
jersey.erase(jersey.begin()+i);
ratings.erase(ratings.begin()+i);
break;
}
}
}
void updatePlayerRating(const vector<int> &jersey, vector<int> &ratings){
int num;
cout << "Enter a jersey number: " << endl;
cin >> num;
for (int i = 0; i < jersey.size(); ++i){
if(jersey.at(i) == num){
cout << "Enter a new rating for player: ";
cin >> num;
cout << endl;
ratings.at(i) = num;
}
}
}
void outputPlayersAboveRating(const vector<int> &jersey, const vector<int> &ratings) {
int num;
cout << "Enter a rating: ";
cin >> num;
cout << endl;
cout << "ABOVE " << num << endl;
for (int i = 0; i < ratings.size(); ++i){
if (ratings.at(i) > num) {
cout << "Player " << i+1 << " -- Jersey number: " << jersey.at(i) << ", Rating: " << ratings.at(i);
}
}
cout << endl;
}
int main() {
vector<int> jersey;
vector<int> ratings;
for (int i = 0; i < 5; ++i) {
int num;
cout << "Enter player " << i+1 << "'s jersey number:";
cin >> num;
jersey.push_back(num);
cout << "Enter player " << i+1 << "'s ratings:";
cin >> num;
ratings.push_back(num);
cout << endl;
cout << endl;
}
outputRoster(jersey, ratings);
char inp;
while(true) {
cout << "MENU" << endl;
cout << "a - Add player" << endl;
cout << "d - Remove player" << endl;
cout << "u - Update player rating" << endl;
cout << "r - Output players above a rating" << endl;
cout << "o - Output roster" << endl;
cout << "q - Quit" << endl;
cout << "Choose an option: ";
cin >> inp;
cout << endl;
if (inp == 'a') {
addPlayer(jersey, ratings);
}
else if (inp == 'd') {
removePlayer(jersey, ratings);
}
else if (inp == 'u') {
updatePlayerRating(jersey, ratings);
}
else if (inp == 'r') {
outputPlayersAboveRating(jersey, ratings);
}
else if (inp == 'o') {
outputRoster(jersey, ratings);
}
else if (inp == 'q') {
return 0;
}
}
return 0;
}
Any and all help is appreciated also if possible, could you explain how to avoid an error like this in the future.
Thanks in advance.
You cannot print ratings directly in
cout ... << ratings ... because std::vector doesn't have an operator overload for printing. Rather, you have to print out an element inside that vector, so change it to cout ... << ratings[i] ..., which I'm assuming is your desired effect.
This is exactly what the compiler error is telling you. std::vector doesn't have an overload (no operator<< match).

How to only print out highest/smallest array values in c++

Good day, I'm having difficulty on the last two parts of my program where it's supposed to only output players who got maximum/minimum scores, I need help on how to do it because I'm really confused. If it's also alright to provide some explanations I'd really appreciate it.
I tried this approach:
#include <iostream>
using namespace std;
int main() {
double lrgst, lrgst2, lrgst3;
int numbers[5];
lrgst = lrgst2 = lrgst3;
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
for (int i = 0; i < 5; i++) {
if (numbers[i] > lrgst) {
lrgst3 = lrgst2;
lrgst2 = lrgst;
lrgst = numbers[i];
} else if (numbers[i] > lrgst2) {
lrgst3 = lrgst2;
lrgst2 = numbers[i];
} else if (numbers[i] > lrgst3) {
lrgst3 = numbers[i];
}
}
cout << "largest are: " << lrgst << " " << lrgst2 << " " << lrgst3;
}
this is my actual code:
#include <iostream>
using namespace std;
struct playerdata {
char name[50];
int age, score1, score2;
double average;
};
int main() {
int choice, i = 1, j = 1, z = 1, backtomain2;
char backtomain;
playerdata p1[10];
do {
for (int a = 0; a < 47; a++) {
cout << "=";
}
cout << "\n";
for (int b = 0; b < 22; b++) {
cout << " ";
if (b == 21) {
cout << "MENU \n";
}
}
for (int c = 0; c < 47; c++) {
ocut << "=";
}
cout << " "
"\n1. Add record\n"
"2. View players records\n"
"3. Compute for the average\n"
"4. Show the player(s) who gets the max average.\n"
"5. Show the player(s) who gets the min average.\n"
"6. Exit\n"
"Enter your choice:";
cin >> choice;
if (choice == 1) {
cout << "Add player data" << endl;
do {
cout << "Enter player " << i << " nickname:";
cin >> p1[i].name;
cout << "Enter player " << i << " age:";
cin >> p1[i].age;
cout << "Enter player " << i << " score 1:";
cin >> p1[i].score1;
cout << "Enter player " << i << " score 2:";
cin >> p1[i].score2;
cout << "Enter again? (Y/N)";
cin >> backtomain;
i++;
}
while (backtomain != 'N' && backtomain != 'n' && i < 7);
if (choice == 2) {
cout << "Player records" << endl;
cout << "Player nickname "
<< "Player age "
<< " player score 1"
<< "
player score 2\n ";
for (z = 1; z <= i - 1; z++) {
cout << p1[z].name << " " << p1[z].age << "" << p1[z].score1 << ""
<< p1[z].score2 << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 3) {
cout << "Computing for average...\n";
for (int d = 1; d <= i - 1; d++) {
p1[d].average = (p1[d].score1 + p1[d].score2) / 2.0;
cout << "\n" << p1[d].average << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 4) {
cout << "Player(s) who got the max average:\n";
cout << "\nPress 1 to go back to main menu";
cin >> backtomain;
}
if (choice == 5) {
cout << "player(s) who got the min average: \n";
cout << "Press 1 to go back to main menu";
cin >> backtomain;
}
}
while (choice != 6);
}
You can simply sort the array of players for that
int n = sizeof(p1)/ sizeof(p1[0]);
sort(p1, p1+n, compPlayer);
//larget at pl[0]
//smallest at pl[9]
where
bool compPlayer(playerdata p1, playerdata p2) {
return (p1.score1+p1.score2) > (p2.score1+p2.score2);
//use score incase average has not been calculated for all players yet
}

vector compiler error for different variable type

The goal of this assignment for my class is to ask the user for how many students are in the class.
Using the Vector library create a vector of Strings to hold the students names.
Create a vector of type double to hold a students grade average.
I have a couple two compiler errors that are preventing my code from running. Line 73 & 83. The following errors I am given:
main.cpp: In function ‘void add_student()’: main.cpp:73:11: error: request for member ‘push_back’ in ‘grade’, which is of non-class type ‘double’
73 | grade.push_back(grade);
| ^~~~~~~~~
main.cpp: In function ‘void remove_student()’: main.cpp:83:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::__cxx11::basic_string<char> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
83 | for (int i = 0; i < students.size(); i++)
| ~~^~~~~~~~~~~~~~~~~**
Any help would be much appreciated. Below is my entire code
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
vector<string> students;
vector<double> grade;
void add_student();
void remove_student();
void menu();
void print_summary();
int main()
{
int numStudent;
char menuSelection;
cout << "Welcome to the Student roaster!" << endl;
cout << "How many students are in your class?:" << endl;
cin >> numStudent;
for (int i = 0; i < numStudent; i++)
{
add_student();
}
cout << "Thank you for entering class information!" << endl;
//calls menu for the user
menu();
while (1)
{
cout << "selection:" << endl;
cin >> menuSelection;
if (menuSelection == 'a')
{
add_student();
}
else if (menuSelection == 'r')
{
remove_student();
}
else if (menuSelection == 'p')
{
print_summary();
}
else if (menuSelection == 'm')
{
menu();
}
else if (menuSelection == 'q')
break;
else
cout << "Not a valid selection" << endl;
}
return 0;
}
void add_student()
{
string firstName, lastName;
double grade;
//ask for student info
cout << "Please enter student (Fisrt Last Grade) info: " << endl;
cin >> firstName >> lastName >> grade;
firstName += " ";
firstName += lastName;
//inserts new student
students.push_back(firstName);
grade.push_back(grade);
}
void remove_student()
{
string first, last;
cout << "Enter the student (First Last) to remove :\n";
cin >> first >> last;
first += " ";
first += last;
int loc = 0;
for (int i = 0; i < students.size(); i++)
{
if (students[i] == first)
{ // finding the location to erase
loc = i;
break;
}
}
students.erase(students.begin() + loc); //removing using erase function
grade.erase(grade.begin() + loc);
}
void menu()
{
cout << "Please choose one of the following options:\n";
cout << "a: add a student\n";
cout << "r: remove a student\n";
cout << "p: print the class summary\n";
cout << "m: print menu\n";
cout << "q: quit program\n";
}
void print_summary()
{
cout << "class summary" << endl;
cout << "--------------------------------" << endl;
cout << "Name" << setw(20) << "Grade" << endl;
cout << "-------" << setw(20) << "--------" << endl;
int n = students.size();
double total = 0;
//cycles through each student
for (int i = 0; i < n; i++)
{
int temp = students[i].size();
int loc = 20 - temp;
cout << students[i] << setw(loc) << grade[i] << endl;
total += grade[i];
}
cout << "Number of students: " << endl;
cout << "------------------" <<endl;
cout << n << " " << endl;
total = (total) / n;
cout << "Average Grade: " << endl;
cout << "--------------" << endl;
//limits the deciaml places to 2
cout << fixed << setprecision(2) << total << " " << endl;
}
The name of the local variable was the same as the name of the global variable.
So you should rename local variable.
62) double grade_;
66) cin >> firstName >> lastName >> grade_;
73) grade.push_back(grade_);

Array of Structures - Functions not pulling through data

I am studying BscCS so I am familiar with programming concepts, however I always seem to get stuck on structs and arrays of structs.
I am required to convert parallel arrays into an array of structs. I have done this, but for some reason the information is not getting sent to the functions properly and the program keeps crashing.
Could you help identify where I am going wrong of if there is something I am missing? I don't need exact code for answers, just some guidance. Here is the code:
#define SIZE 3
struct Employee {
string firstName[SIZE];
string lastName[SIZE];
int id[SIZE];
int hoursWorked[SIZE];
int payRate[SIZE];
int stat[SIZE];
};
//Functions
int menu();
void printReport(Employee & employees);
void search(Employee & employees);
void calculatePay(Employee & employees);
void orderByLastName(Employee & employees);
void orderByid(Employee & employees);
void printActive(Employee & employees);
void printInactive(Employee & employees);
//Display Main Menu
int menu()
{
int choice;
cout << "1. Print out Employee Report. " << endl;
cout << "2. Search Employee Records. " << endl;
cout << "3. Display the Report in Sorted order on Last Name or ID. << endl;
cout << "4. Calculate Pay. " << endl;
cout << "5. Display Active Employees." << endl;
cout << "6. Display Inactive Employees." << endl;
cout << "7. Quit" << endl;
cout << "Enter your choice. ";
cin >> choice;
return choice;
}
//Display the employee data in a formatted order
void printReport(Employee & employees)
{
cout << setw(10) << "First Name" <<
setw(10) << "Last Name" <<
setw(10) << "ID" <<
setw(10) << "Hours" <<
setw(10) << "Rate" <<
endl;
ios::fixed;
for (int index = 0; index < SIZE; index++)
cout << setw(10) << employees.firstName[index] <<
setw(10) << employees.lastName[index] <<
setw(10) << employees.id[index] <<
setw(10) << employees.hoursWorked[index] <<
setw(10) << employees.payRate[index] << endl;
}
//Search for employee ID
//Show "Not Found" if unable to locate
void search(Employee & employees)
{
bool found = false;
int idNumber;
int pos = -1;
cout << "Enter id number ";
cin >> idNumber;
for (int index = 0; index < SIZE && !found; index++)
{
if (employees.id[index] == idNumber)
{
found = true;
pos = index;
}
}
if (!found)
cout << "Not Found. " << endl;
else
cout << setw(10) << employees.firstName[pos] <<
setw(10) << employees.lastName[pos] <<
setw(10) << employees.id[pos] <<
setw(10) << employees.hoursWorked[pos] <<
setw(10) << employees.payRate[pos] << endl;
}
//Calculate total weekly pay
void calculatePay(Employee & employees)
{
cout << setw(10) << "First Name" <<
setw(10) << "Last Name" <<
setw(10) << "ID" <<
setw(10) << "Hours" <<
setw(10) << "Rate" <<
setw(10) << "Total Pay" <<
endl;
ios::fixed;
for (int index = 0; index < SIZE; index++)
cout << setw(10) << employees.firstName[index] <<
setw(10) << employees.lastName[index] <<
setw(10) << employees.id[index] <<
setw(10) << employees.hoursWorked[index] <<
setw(10) << employees.payRate[index] <<
setw(10) << employees.hoursWorked[index] * employees.payRate[index] << endl;
}
//Sort employee data by last name
void orderByLastName(Employee & employees)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE - 1; j++)
{
if (employees.lastName[j] > employees.lastName[j + 1])
{
string temp = employees.lastName[j];
employees.lastName[j] = employees.lastName[j + 1];
employees.lastName[j + 1] = temp;
temp = employees.firstName[j];
employees.firstName[j] = employees.firstName[j + 1];
employees.firstName[j + 1] = temp;
int tempid = employees.id[j];
employees.id[j] = employees.id[j + 1];
employees.id[j + 1] = tempid;
int temphours = employees.hoursWorked[j];
employees.hoursWorked[j] = employees.hoursWorked[j + 1];
employees.hoursWorked[j + 1] = temphours;
int temppayrate = employees.payRate[j];
employees.payRate[j] = employees.payRate[j + 1];
employees.payRate[j + 1] = temppayrate;
}
}
}
}
//Sort employee data by ID
void orderByid(Employee & employees)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE - 1; j++)
{
if (employees.id[j] > employees.id[j + 1])
{
string temp = employees.lastName[j];
employees.lastName[j] = employees.lastName[j + 1];
employees.lastName[j + 1] = temp;
temp = employees.firstName[j];
employees.firstName[j] = employees.firstName[j + 1];
employees.firstName[j + 1] = temp;
int tempid = employees.id[j];
employees.id[j] = employees.id[j + 1];
employees.id[j + 1] = tempid;
int temphours = employees.hoursWorked[j];
employees.hoursWorked[j] = employees.hoursWorked[j + 1];
employees.hoursWorked[j + 1] = temphours;
int temppayrate = employees.payRate[j];
employees.payRate[j] = employees.payRate[j + 1];
employees.payRate[j + 1] = temppayrate;
}
}
}
}
void printActive(Employee & employees)
{
bool found = false;
int pos = -1;
for (int index = 0; index < SIZE && !found; index++)
{
if (employees.stat[index] == 1)
{
found = true;
pos = index;
}
if (!found) {
cout << "No active employees." << endl;
}
else {
cout << setw(10) << employees.firstName[pos] <<
setw(10) << employees.lastName[pos] <<
setw(10) << employees.id[pos] <<
setw(10) << employees.hoursWorked[pos] <<
setw(10) << employees.payRate[pos] << endl;
}
}
}
void printInactive(Employee & employees)
{
bool found = false;
int pos = -1;
for (int index = 0; index < SIZE && !found; index++)
{
if (employees.stat[index] == 0)
{
found = true;
pos = index;
}
if (!found) {
cout << "No inactive employees." << endl;
}
else {
cout << setw(10) << employees.firstName[pos] <<
setw(10) << employees.lastName[pos] <<
setw(10) << employees.id[pos] <<
setw(10) << employees.hoursWorked[pos] <<
setw(10) << employees.payRate[pos] << endl;
}
}
}
int main()
{
struct Employee employees[SIZE];
//Read first and last name from user
for (int index = 0; index < SIZE; index++)
{
cout << "Enter first name : ";
cin >> employees[index].firstName[index];
cout << "Enter last name : ";
cin >> employees[index].lastName[index];
//Read ID, hours, and pay rate from user
while (employees[index].id[index] < 0)
{
cout << "Enter id : ";
cin >> employees[index].id[index];
if (employees[index].id[index] < 0)
{
cout << "Invalid Id number. " << endl;
cout << "Enter id : ";
cin >> employees[index].id[index];
}
}
while (employees[index].hoursWorked[index] < 0)
{
cout << "Enter hours worked : ";
cin >> employees[index].hoursWorked[index];
if (employees[index].hoursWorked[index] < 0)
{
cout << "Invalid hours. " << endl;
cout << "Enter hours worked : ";
cin >> employees[index].hoursWorked[index];
}
}
while (employees[index].payRate[index] < 0)
{
cout << "Enter Pay Rate : ";
cin >> employees[index].payRate[index];
if (employees[index].payRate[index] < 0)
{
cout << "Invalid pay rate. " << endl;
cout << "Enter Pay Rate : ";
cin >> employees[index].payRate[index];
}
}
while (employees[index].stat[index] < 0)
{
cout << "Enter your status. (0 - Inactive, 1 - Active) : ";
cin >> employees[index].stat[index];
if (employees[index].stat[index] < 0)
{
cout << "Enter your status : ";
cin >> employees[index].stat[index];
}
cout << endl;
}
}
//Loop to display menu options until user quits program
while (true)
{
//Call menu with options
int ch = menu();
//Different options to choose
switch (ch)
{
case 1:
printReport(employees[SIZE]);
break;
case 2:
search(employees[SIZE]);
break;
case 3:
int sortType;
cout << "1.Sort by Last Name." << endl;
cout << "2.Sort by ID." << endl;
cout << "Enter your choice. ";
cin >> sortType;
if (sortType == 1)
orderByLastName(employees[SIZE]);
else if (sortType ==2)
orderByid(employees[SIZE]);
break;
case 4:
calculatePay(employees[SIZE]);
break;
case 5: printActive(employees[SIZE]);
case 6: printInactive(employees[SIZE]);
break;
case 7:
exit(0);
}
}
system("pause");
return 0;
}
Thank you in advance!
I ran your program, and I received more than 20 errors before the compiler involuntarily stopped trying to compile any longer. All of them are connected to the lines of code using cout, cin, string, and setw. You need to use these keywords with the std namespace to fix this, and you need to include the iostream header file (#include <iostream>). Either you can write std:: in front of all of these keywords (i.e. std::cout) or you can declare the namespace in the preprocessor (using namespace std;). Also, for std::setw, you need to include the iomanip header file (#include <iomanip>). After that, you're missing a quotation in the menu function and a bracket at the end of the main function. (There's also a couple of problems with initializing the size of the struct members in the declaration, but I'll leave that up to you.) Hope this helped!