简体   繁体   中英

How to call a function within a function c++

I have the following code:

/*****************************************************************/
/*                                                               */
/*        Program Greated By: Dani Smith                         */
/*        Dr. Azzar Introduction to Computer Science             */
/*        Spring 2014                                            */
/*        Due Date: March 19, 2014 (Extended to March 24, 2014)  */
/*        Lab 2: Array Processing                                */
/*        This program places information from an infile into    */
/*        arrays, processes those arrays, and then prints the    */
/*        processed information to an outfile.                   */
/*                                                               */
/*****************************************************************/
/***********************Include Statements************************/
/**/                   #include <iostream>                     /**/
/**/                   #include <iomanip>                      /**/
/**/                   #include <fstream>                      /**/
/**/                   #include <string>                       /**/
/**/                   #include <stdio.h>                      /**/
/**/                   #include <cstdlib>                      /**/
/*****************************************************************/
//Other Pre-declerations
using namespace std;
/*********************Function Prototypes**************************/
int process_employee(ifstream&, int[], string[], double[]);
int process_payroll(ifstream&, int[], string[], double[]);
void print_results(ifstream&, ofstream&, int[], string[], double[], int);
int highest_pay(double[], int);
int lowest_pay(double[], int);
/******************************************************************/
int main(){
typedef int intArray[15];
double wage[15];
string name[15];
intArray hours;
int i = 0, j, index;
ifstream inFile;
ofstream outFile;
//opens the files that will be used in the program
inFile.open("C:/Users/Dani Smith/Documents/lab2In.txt");
outFile.open("C:/Users/Dani Smith/Documents/lab2out.txt");
//displays error message if one or both of the files failed to open
if (inFile.fail()){
    cout << "Cannot find input file" << endl;
    exit(1);
}
if (outFile.fail()){
    cout << "Cannot find output file" << endl;
    system("pause");
    exit(1);
}

i = process_employee(inFile, hours, name, wage); //this is what I am trying to move into the print_results function

//Call Process Payroll
//Print Items
print_results(inFile, outFile, hours, name, wage, i);
//system("start C:/Users/Dani Smith/Documents/lab2out.txt");
return 1;
}

int process_employee(ifstream& in, int hours[], string name[], double wage[]){
int i = 0;

while (in >> name[i] >> wage[i] >> hours[i])
    i++;

return i;
}
//highest_pay finds the highrest payed worker.
int highest_pay(double rate[], int items){
double high = -9999;
int y = 0, highIndex = 0;

for (y = 0; y < items; y++)
    if (rate[y] > high){
        high = rate[y];
        highIndex = y;
    }
return highIndex;
}
//lowest_pay finds the lowest payed worker.
int lowest_pay(double rate[], int items){
double low = 9999;
int y = 0, lo`wIndex = 0;

for (y = 0; y < items; y++)
    if (rate[y] < low){
        low = rate[y];
        lowIndex = y;
    }
return lowIndex;
}
//calculate how much each employee should be paid
int process_payrol(ifstream& in, int hours[], string name[], double wage[]){

}
//prints the report to the outfile
void print_results(ifstream& in, ofstream& out, int hours[], string name[], double wage[], int empNum){
int j, index i = empNum;
out << "Number of Employees: " << i << endl; //prints the number of employees
index = highest_pay(wage, i);//changes index to the highest pay rate
out << "Maximum Pay Rate: " << name[index] << " @ $" << wage[index] << endl;//prints the maximum pay rate
index = lowest_pay(wage, i);//changes index to the lowest pay rate
out << "Minimum Pay Rate: " << name[index] << " @ $" << wage[index] << endl;//prints the minimum pay rate
//prints the titles on the top of the outfile
out << endl << setw(15) << "Name" << setw(8) << "Hours" << setw(8) << "Gross" << setw(7) << "Bonus" << setw(19) << "Adjusted Gross\n" << endl;
//for loop prints the rest of the report
for (j = 0; j < i; j++)
    out << setw(15) << name[j] << setw(8) << hours[j] << setw(8) << hours[j] << endl;

system("pause");
}

I am trying to move int i from the int main function into the print_results function. When I do

int i = process_employee(in, hours, name, wage);

int i remains 0. So my question is how to call a function within a function using the first function's arguments in c++. Also, another question is how to I start the outFile when the program ends? I tried to do a system("start path here");, but it can't find the file because there is a space in the path. So in summery I have two questions: 1. How do I call a function within a function using the first function's arguments? 2. How can I open a txt file with a space in the path?

This should work, probably you did not remove the original line

i = process_employee(inFile, hours, name, wage); //this is what I am trying to move into the print_results function

So, the file position is at the end of file, and function will return 0.

By the way, your code has a few mistypes, did you ever try to compile it?

for your second question just rename the directory "Dani Smith" to "Dani_Smith"
also make sure you do outFile.close so the file can open
also you should send the integer i to the function rather than recalling it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM