I am looking for tips on improving my short program. I am using system("PAUSE")
because this was for an assignment.
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void Display(string* , int* , double*, int);
void Percentage(int*, double*, int);
void Intro(string*, int*, int&);
void Highest(int&, string&, double*, string*, int);
int main()
{
int students;
string names[10];
int hours[10];
double percents[10];
int highest;
string most;
Intro(names, hours, students);
Percentage(hours, percents, students);
Display(names, hours, percents, students);
Highest(highest, most, percents, names, students);
system("PAUSE");
}
void Intro(string* names, int* hours, int& students)
{
int team;
cout << "A team is made up of atleast 2 students. How many students are on the team?: ";
cin >> team;
cout << "Enter student's first name and the hours worked on the final project: " << endl;
for (int i = 0; i < team; i++)
{
cout << i + 1 << ": ";
cin >> names[i] >> hours[i];
}
students = team;
}
void Display(string* names, int* hours, double* percent, int students)
{
cout << setw(20) << "Students";
cout << setw(20) << "Hours Worked";
cout << setw(20) << "% of Total Hours";
cout << endl;
cout << "------------------------------------------------------------" << endl;
for (int i = 0; i < students; i++)
{
cout << setw(20) << names[i];
cout << setw(9) << hours[i];
cout << setw(16) << percent[i];
cout << endl;
}
}
void Percentage(int* hours, double* percent, int students)
{
int total(0);
for (int i = 0; i < students; i++)
{
total += hours[i];
}
for (int i = 0; i < students; i++)
{
percent[i] = double(hours[i]) / total * 100;
}
}
void Highest(int& highest, string& most, double* percent, string* names, int students)
{
highest = percent[0];
for (int i = 0; i < students; i++)
{
if (highest < percent[i])
{
highest = percent[i];
most = names[i];
}
}
cout << most << " worked the most hours." << endl;
}
getchar()
... \$\endgroup\$std::cin.get()
would be preferred for C++. It also goes with<iostream>
, whereasgetchar()
goes with<cstdio>
/<stdio.h>
. \$\endgroup\$C++
tag! \$\endgroup\$