gpt4 book ai didi

c++ - 查找用户输入值的总和

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:01:55 29 4
gpt4 key购买 nike

我需要帮助使用 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com