C++/Arrays
Expert: vijayan - 12/3/2009
QuestionI have lowered the number of errors down to three. I am not sure if my arrays and statements are logically correct. Can you help me solve the last three errors and maybe check to see if I used the arrays correctly.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int const size = 20;
void startArrays(string[], int[], double[]);
void minMaxPay(double&, double&);
int main()
{
int choice, emp, i; //emp for employee query
double min, max;
string name[size];
int hours[size];
double pay[size];
startArrays(name[], hours[], pay[]);
do
{
cout << "1) Display Employee Information" << endl;
cout << "2) Query Employee Information" << endl;
cout << "3) Display Min and Max Salary" << endl;
cout << "4) Quit" << endl, endl;
cin >> choice;
switch (choice)
{
case 1 : for (i = 0; i < size; i++)
{
cout << name[i] << hours[i] << pay[i] << endl;
}
break;
case 2 : cout << "Enter an employee number (integers 0-19)";
cin >> emp;
cout << name[emp] << hours [emp] << pay[emp] << endl;
break;
case 3 : minMaxPay(min, max);
cout << "Minimum Pay = " << min << endl;
cout << "Maximum Pay = " << max << endl;
break;
case 4 : break;
default : cout << "Invalid Input -- please try again" << endl;
break;
}
}
while (choice != 4);
cout << "Thank You -- Goodbye!" << endl;
return 0;
}
void startArrays(string name[], int hours[], double pay[])
{
int i, time;
double money;
string word;
ifstream inFile1, inFile2, inFile3;
inFile1.open("pa9.emp");
inFile2.open("pa9.hours");
inFile3.open("pa9.pay");
if (inFile1.fail())
{
cout <<"Error opening pa9.emp -- please try again"<< endl;
return;
}
if (inFile2.fail())
{
cout << "Error opening pa9.hours -- please try again" << endl;
return;
}
if (inFile3.fail())
{
cout << "Error opening pa9.pay -- please try again" << endl;
return;
}
for(i=0; i < size; i++)
{
inFile1 >> word;
name[i] = word;
inFile2 >> time;
hours[i] = time;
inFile3 >> money;
pay[i] = money;
}
inFile1.close();
inFile2.close();
inFile3.close();
return;
}
void minMaxPay(double& min, double& max)
{
double number;
ifstream inFile3;
inFile3.open("pa9.pay");
if (inFile3.fail())
{
cout << "Error opening pa9.pay -- please try again" << endl;
return;
}
inFile3 >> number;
min = number;
max = number;
while(inFile3)
{
if (number > max)
{
max = number;
}
if (number < min)
{
min = number;
}
}
inFile3.close();
return;
}
AnswerThere are just two errors in your code.
The function
void startArrays( string[], int[], double[] );
when invoked, should be called this way:
startArrays( name, hours, pay ); // ok
and not this way:
startArrays(name[], hours[], pay[]); // **** error ****
This too is an obvious error:
cout << "4) Quit" << endl, endl; // **** error ****
instead of the comma(,), use the stream insertion operator(<<)
cout << "4) Quit" << endl << endl; // ok
or simply
cout << "4) Quit\n\n" // ok