因此,我在我的頭文件中放了一個while循環,我想將它連接到我的main.cpp,但是然後我的main.cpp不會播放它,除非它在我的頭文件中聲明。所以我想知道是否有必要在標題中的while循環?該任務是創建一個程序,用於計算銷售額下的用戶工資。在標題或主文件中循環
這是我的頭文件:
class Salary
{
public:
Salary(){};
Salary(double employeeSales, double employeeSalary)
:sales{employeeSales},salary(employeeSalary)
{
while(employeeSales != -1)
{
salary = 200 + (.09 * sales);
}
}
void setSales(double employeeSales)
{
sales = employeeSales;
}
void setSalary(double employeeSalary)
{
salary = employeeSalary;
}
double getSales() const{return sales;}
double getSalary() const{return salary;}
private:
double sales;
double salary;
};
這是我的main.cpp
#include <iostream>
#include <iomanip>
#include "Salary.h"
using namespace std;
int main()
{
Salary mySalary;
double employeeSales;
double employeeSalary;
cout << fixed << setprecision(2);
cout << "Enter sales in dollars(-1 to quit): ";
cin >> employeeSales;
mySalary.setSales(employeeSales);
while(true)
{
employeeSalary = 200 + (.09 * employeeSales);
mySalary.setSalary(employeeSalary);
cout << "Salary is: $" << mySalary.getSalary();
cout << "Enter sales in dollars(-1 to quit): ";
cin >> employeeSales;
mySalary.setSales(employeeSales);
}
}
然而,在主while循環不會工作,除非我把一段時間(employeeSales = -1 )
「不行」意味着什麼? – tadman
你的第二個構造函數從不使用。因此,添加'while(employeeSales!= -1)'不可能阻止任何失敗。 – GigaRohan
使用調試程序遍歷代碼,您將看到爲什麼您的循環永遠不會執行。 –