2013-04-23 40 views
0

任何帮助将不胜感激,我的程序退出菜单,并尝试输入的东西,一直在绞尽脑汁,试图弄清楚这是非常讨厌的,因为我不能做任何事情,直到我修复这个问题。我是一个在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(){ 




} 
+0

您是否善意地给出您的想法为什么会发生这样的事情......您是否尝试过使用调试器来查看问题所在?查看你的代码,我可以看到至少有两个错误,显然是它退出的原因 - 因为这就是它的原因。=) – evilruff 2013-04-23 22:21:18

+1

从InputRecords()递归调用'main()'可能不是你真正想要的去做。 – jxh 2013-04-23 22:22:57

+0

这些functoins的返回值在哪里? – gongzhitaao 2013-04-23 22:23:41

回答

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功能和initalised零那里。我看不出为什么你在主函数中有行变量。

另外我不明白为什么你传递menuchoice到InputRecords,它不习惯于那里。这一切似乎都是随机的,也许你应该审查函数和参数传递。

+0

我需要在其他地方使用行,但我没有完成该程序,我不明白为什么我通过menuchoice,但它只工作,如果我这样做,我没有问题,非常感谢您的帮助! – user2312710 2013-04-24 09:52:51

2

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

这也是很好的做法来初始化像menuchoice

+1

哇这是很菜鸟,不敢相信我被困在它那么久,非常感谢您的帮助! – user2312710 2013-04-24 09:44:53

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 
相关问题