【问题标题】:How to call a function within a function c++如何在函数c ++中调用函数
【发布时间】:2014-03-21 14:03:19
【问题描述】:

我有以下代码:

/*****************************************************************/
/*                                                               */
/*        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文件?

【问题讨论】:

  • 嘿,这样一个伟大的标题和如此糟糕的格式。第二个答案是\"
  • 标题看起来很有趣,但这种包含风格不太可能赢得任何赞赏奖。
  • 博士。 Azzar 会很高兴他的学生在 Stack Overflow 上完成作业
  • @BartekBanachewicz 根据标准 # 必须是第一个字符
  • @MaciejLichoń 与我今天看到的其他家庭作业问题不同,这个问题至少有提问者提出的相当多的努力。我最近看到了几个简单的任务剪切和粘贴,没有其他明显的努力。

标签: c++ function arguments


【解决方案1】:

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

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

所以,文件位置在文件末尾,函数会返回0。

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

【讨论】:

  • 我从那行代码收到一个错误。它说 inFile 没有在范围内声明
  • 不,这是您应该从 main 中删除的一行。在 print_results 中,您应该使用上面提到的行:int i = process_employee(in, hours, name, wage);
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2013-02-25
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多