当我输入内容或尝试阅读某些内容时c ++程序退出?

时间:2013-04-23 22:16:40

标签: c++

任何帮助都会非常感激,我的程序一退出菜单就会退出并尝试输入一些东西,绞尽脑汁试图弄清楚这一点并且非常烦人,因为我无法完成任何其他工作直到我解决这个问题。我是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(){




}

3 个答案:

答案 0 :(得分: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函数并在那里初始化为零。我不明白你为什么在main函数中有row变量。

此外,我不明白为什么你将menuchoice传递给InputRecords,它不会在那里使用。这看起来有点随机,也许你应该检查函数和参数传递。

答案 1 :(得分:2)

看起来您的row变量永远不会被初始化。这是为什么?

初始化像menuchoice

这样的变量也是一种很好的做法

答案 2 :(得分: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