当我输入一些东西或试图读取一些东西时,c++程序会退出

c++ program quits when i input something or try to read something?

本文关键字:c++ 程序 退出 输入 读取      更新时间:2023-10-16

任何帮助将不胜感激,我的程序退出一旦我走出菜单,并试图进入一些东西,一直在绞尽脑汁试图弄清楚这一点,是非常恼人的,因为我不能得到任何其他事情做,直到我解决这个问题。我是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 << " nnnnn                             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(){


}

问题就在这里

int main()
{
    struct cust c[100];
    int menuchoice, row;
    Menu(menuchoice);
    if (menuchoice == 1){
        system("CLS");
        InputRecords(c, row, menuchoice);
    }

您没有给变量row一个值,但是当您调用InputRecords时使用了row

从你的代码来看,在我看来,行变量应该被移动到InputRecords函数,并在那里初始化为零。我不明白你为什么要在main函数中使用row变量。

我也看不出为什么你传递menuchoice到InputRecords,它不会在那里使用。这看起来有点随意,也许你应该复习一下函数和参数传递。

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

这也是一个很好的做法,初始化你的变量,如menuchoice
 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