我正在編寫一些編程作業,我正在嘗試使用for-loop
以促進編碼過程。這裏是循環:輸出嵌套For循環(C++)無法正常工作
#ifndef DIVSALES_H
#define DIVSALES_H
class DivSales
{
public:
DivSales(){ quarterSales[4] = {0}; };
double getTotalSales() { return totalSales;}
static void setTotalSales(double);
static void addTotalSales(double);
double getQuarterSales(int numQuarter) {return quarterSales[numQuarter];}
void setQuarterSales(int numQuarter, double numAmount) { quarterSales[numQuarter] = numAmount;}
private:
static double totalSales;
double quarterSales[];
};
double DivSales::totalSales = 0;
void DivSales::setTotalSales(double totalAmount) {totalSales = totalAmount; }
void DivSales::addTotalSales(double addAmount) {totalSales += addAmount; }
#endif // DIVSALES_H
#include <iostream>
#include "DivSales.h"
using namespace std;
int main()
{
const int NUMDIVS = 6;
const int NUMQUARTERS = 4;
double amount = 0;
DivSales divs[NUMDIVS];
for(int division = 0; division < NUMDIVS; division++)
{
cout << "Division " << (division + 1) << endl;
for(int quarter = 0; quarter < NUMQUARTERS; quarter++)
{
cout << "Quarter " << (quarter + 1) << ": ";
cin >> amount;
divs[division].setQuarterSales(quarter, amount);
DivSales::addTotalSales(amount);
}
}
return 0;
}
例子:
Division 1
Quarter 1: 500
Quarter 2: 500
Quarter 3: 500
Quarter 2: 500
Quarter 3: 500
Quarter 2: 500
Quarter 3: 500
Quarter 2: 500
Quarter 3: 500
Quarter 2:
我所試圖做的是讓這個當我輸入了4個數字它將轉移到下一個部門。但是,在4個輸入之後,它不是遞增for-loop
的分區變量,而是繼續詢問更多輸入。到底是怎麼回事?
所以這是無限的循環? – clcto
是的,非常。 – Mourneris
輸入4個季度後,它會再次輸出「Division 1」? – clcto