How do I use multiple for loops C++ while obtaining the sum of income and expense? -
i'm uber new c++ , learning loops.
this homework problem:
the sales manager @ mystore wants program allows enter company's income , expense amounts, integers. number of income , expense amounts may vary each time program run. example, sales manager may need enter 5 income amounts , 3 expense amounts. or, may need enter 20 income amounts , 30 expense amounts. program should calculate , display company's total income, total expenses, , profit (or loss).
i trying use 2 loops sum of expense , income keep getting wrong sum second loop. i'm not sure if should use multiple loops or different together. appreciate help. thanks!
here part of i've got far:
if (incnum > 0) { // income items , calculate sum total. (int count = 1; count <= incnum; count++) { cout << "enter income items: " << count << ": "; cin >> income; sumincome += income; // calculate sum total. } } // display total income. cout << fixed << showpoint << setprecision(2); cout << "the total sum of income $" << sumincome << endl; cout << "income=" << income << endl; // number of items income. cout << "how many items have enter expenses?"; cin >> expnum; if (expnum > 0) { // expense items , calculate sum total. (int count = 1; count <= expnum; count++) { cout << "enter expense items" << count << ":"; cin >> expense; sumexpense += expense; // calculate sum total. } } // display total income. cout << fixed << showpoint << setprecision(2); cout << "the total expense items $" << sumexpense << endl;
initialise sumincome , sumexpense zero
int sumincome=0; int sumexpense=0; int profitorloss=0;
if don't initialize variable , used in arithmetice operation garbage value come play might cause
if(sumexpense>sumincome) { cout<<"total loss"<<sumexpense-sumincome; } else { cout<<"total profit"<<sumincome-sumexpense; }
Comments
Post a Comment