如何在函数c ++中调用函数

时间:2014-03-21 14:03:19

标签: c++ function arguments

我有以下代码:

/*****************************************************************/
/*                                                               */
/*        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");
}

我正在尝试将int i从int main函数移动到print_results函数中。当我做的时候

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

int i仍为0.所以我的问题是如何使用c ++中的第一个函数的参数调用函数内的函数。另外,另一个问题是如何在程序结束时启动outFile?我试图做一个系统(“在这里开始路径”);但它找不到文件,因为路径中有一个空格。所以在夏天我有两个问题: 1.如何使用第一个函数的参数调用函数内的函数? 2.如何在路径中打开带有空格的txt文件?

2 个答案:

答案 0 :(得分:0)

这应该可行,可能你没有删除原始行

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

因此,文件位置在文件末尾,函数将返回0.

顺便说一下,你的代码有一些错误类型,你有没有试过编译它?

答案 1 :(得分:0)

对于您的第二个问题,只需将目录"Dani Smith"重命名为"Dani_Smith"
还要确保你执行outFile.close以便文件可以打开
你也应该将整数i发送给函数而不是回忆它