0

任何帮助将不胜感激,一旦我退出菜单并尝试输入某些内容,我的程序就会退出,我一直在绞尽脑汁试图弄清楚这一点并且非常烦人,因为在我解决这个问题之前我无法完成其他任何事情. 我是 C++ 的初学者,所以如果它是菜鸟的错误,请不要指责我,哈哈!

这是源代码,它还不是一个完整的程序,只是现在无法弄清楚什么是错的。

谢谢你的帮助!

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

struct cust
{
    int employeeno, deptno;
    char fname[10], sname[10], weekend[10];
    float hours, othours, rate, otrate, normalpay, otpay, grosspay, netpay, totalni, totaltax, ni, tax;

};

int Menu(int& menuchoice);
void InputRecords(cust c[], int row, int menuchoice);
int Calculations(cust c[]);

int SearchNumber(cust c[], int &row);
int DeleteRecords();
int TotalPay();

int main()
{
    struct cust c[100];

    int menuchoice, row;

    Menu(menuchoice);

    if (menuchoice == 1){
    system("CLS");
    InputRecords(c, row, menuchoice);
    }

    if (menuchoice == 2){
    system("CLS");
    SearchNumber(c, row);
    }

    if (menuchoice == 3){
    system("CLS");
    DeleteRecords();
    }

    if (menuchoice == 4){
    system("CLS");

    }

    if (menuchoice == 5){
    system("CLS");
    exit(5);
    }

    //Calculations(cust c[]);

}

int Menu(int& menuchoice){

    cout << " \n\n\n\n\n                             1. Input a Payslip" << endl << endl;;
    cout << "                             2. Read a Payslip " << endl << endl;
    cout << "                             3.              " << endl << endl;
    cout << "                             4.              " << endl << endl;
    cout << "                             5. Quit the Program" << endl << endl;
    cin >> menuchoice;

}



void InputRecords(cust c[], int row, int menuchoice){

    char another;

    do{
    cout << "Please Enter Their Employee Number: " << endl;
    cin >> c[row].employeeno;

    cout << "Please Enter Their First Name: " << endl;
    cin >> c[row].fname,9;

    cout << "Please Enter Their Second Name: " << endl;
    cin >> c[row].sname,9;

    cout << "Please Enter Their Department Number 1 - 9: " << endl;
    cin >> c[row].deptno;

    cout << "Please Enter The Hours They Have Worked: " << endl;
    cin >> c[row].hours;

        if (c[row].hours >= 37.5){

            cout << "Please Enter Any Overtime They Have Worked: " << endl;
            cin >> c[row].othours;
        }

    cout << "Please Enter Their Rate of Pay: " << endl;
    cin >> c[row].rate;

    cout << "Please Enter The Date of the Week End (DD/MM/YYYY): " << endl;
    cin >> c[row].weekend, 9;

    row++;

    cout << endl;

    //Putting it in the file.
    ofstream timesheetFile("Timesheet.txt", ios::app);

    if(timesheetFile.is_open()){
        cout << "File has been opened." << endl;

        timesheetFile << c[row].employeeno << "    " << c[row].fname << "    "  << c[row].sname << "    "  << c[row].deptno << "  " << c[row].hours << "  " <<  c[row].othours << "   " << c[row].rate << "  " << c[row].weekend << "\n" << endl;

        timesheetFile.close();
    }else{
        cout << "Error! File is not open." << endl;
    }

    cout << "Would you like to enter another record? Y/N : ";
    cin >> another;

    cout << endl << endl;

    }while(row<100 && another == 'y');

    system("CLS");
    main();
}

//read records
int SearchNumber(cust c[], int &row){

    //system("CLS");

    int empno;

    cout << "Enter Employee Number : ";

    cin >> empno;

    for (int i=0; i < row; i++)
    {
        if (empno == c[i].employeeno){

            system("CLS");
            cout << c[i].employeeno << endl << c[i].fname << c[i].sname << endl;
        }
    }
}

//deleterecords
int DeleteRecords(){

}

//calculations
int Calculations(float normalpay, float& hours, float& rate, float otpay, float otrate, float& othours, float grosspay, float tax, float ni, float netpay, float totalni, float totaltax){

    ni = 6.8 / 100;
    tax = 12.75 / 100;
    otrate = 1.5 * rate;

    normalpay = hours * rate ;
    otpay = otrate * othours;

    grosspay = normalpay + otpay;

    totalni = grosspay * ni;

    totaltax = tax * grosspay;

    netpay = normalpay + otpay - totaltax - totalni;

//    cout << totaltax << endl;
//
//    cout << totalni << endl;
//
//    cout << netpay << endl;


}

int TotalPay(){




}
4

3 回答 3

7

问题就在这里

int main()
{
    struct cust c[100];

    int menuchoice, row;

    Menu(menuchoice);

    if (menuchoice == 1){
        system("CLS");
        InputRecords(c, row, menuchoice);
    }

你没有给变量row一个值,但是你row在调用InputRecords.

从您的代码来看,在我看来应该将行变量移动到 InputRecords 函数并在那里初始化为零。我不明白为什么你在主函数中有行变量。

我也看不出你为什么将 menuchoice 传递给 InputRecords,它在那里没有被使用。这一切似乎有点随机,也许你应该回顾一下函数和参数传递。

于 2013-04-23T22:21:48.873 回答
2

看起来您的row变量从未被初始化。为什么是这样?

初始化变量也是一个好习惯menuchoice

于 2013-04-23T22:21:44.560 回答
0
 int Menu(int& menuchoice);
 void InputRecords(cust c[], int row, int menuchoice);// declared
 int Calculations(cust c[]);

 int SearchNumber(cust c[], int &row);
 int DeleteRecords();
 int TotalPay();

 int main()
 {
     struct cust c[100];

     int menuchoice, row; // declared again but never initialized

     Menu(menuchoice);

     if (menuchoice == 1){ 
     system("CLS");
     InputRecords(c, row, menuchoice); // used
于 2013-04-23T23:46:27.410 回答