我需要帮助使用 C++ 将多个用户输入的值相加
double total; // Accumulates total
double price; // Gets next price from user
int numItems; // Number of items
int counter = 1; // Loop control counter
cout << "How many items do you have? ";
cin >> numItems;
cout << endl;
while (counter <= numItems) {
total = 0;
cout << "Enter the price of item " << counter << ": ";
cin >> price;
cout << endl;
total += price;
counter++;
}
cout << "The total price is: " << total << endl;
当我运行我的代码时,我最终只从用户那里得到一个值的总和
最佳答案
是的,正如 Chetan Ranpariya 所说:
total = 0;
应该在 while 循环之前。
关于c++ - 查找用户输入值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58194131/