Is this C++ Program correct?

2015-09-12 11:02 pm
a bug collector collects bugs every day for seven days. Write a program that keeps a running total of the number of bugs collected during the seven days. The loop should ask for the number of bugs collected for each day, and when the loop is finished, the program should display the total number of bugs collected.
更新1:

#include <iostream> using namespace std; int main() { int userInput = 0; int totalBugs = 0; for (int x = 1; x <= 7; ++x) { cout << "Enter num bugs for day #<< x <<:" << endl; cin >> userInput; totalBugs += userInput; } cout << "The total number of bugs: " << totalBugs << endl; }

更新2:

What am i doing wrong?? Can somebody write down the correct program :( ?

回答 (3)

2015-09-13 9:39 am
#include <iostream>
using namespace std;

int main()
{
int userInput = 0;
int totalBugs = 0;

for (int x = 1; x <= 7; x++)
{
cout << "Enter number of bugs for day #" << x <<": ";
cin >> userInput;

totalBugs += userInput;
}

cout << "The total number of bugs: " << totalBugs << endl;
}
2015-09-13 12:03 am
cout << "Enter num bugs for day #"<< x << endl;
2015-09-12 11:03 pm
You are incrementing x before using it, you need to x++, not ++x.


收錄日期: 2021-04-30 23:16:18
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20150912150217AAdpeLm

檢視 Wayback Machine 備份