好吧,所以我根据用户想要添加的数量来构建添加2个数字的内容。但是,一旦它通过用户输入进入部分,b就会切换到最新的部分。所以我无法用它做任何事情,更不用说如何使用它来添加它。
#include <iostream>
using namespace std;
void main(){
cout << "How many numbers do you want to add? ";
int a;
cin >> a;
for (int i = 0; i < a; i ++){
// if i<= a doesn't work use i < a + 1
cout << "Please insert an interger you want to add: ";
int b;
}
}
答案 0 :(得分:1)
你现在遇到的问题是你没有在int b中存储任何东西。您只是声明变量。尝试在for循环中放入一个cin来存储您希望用户添加的值,然后将其添加到另一个变量中。
#include <iostream>
#using namespace std;
void main(){
cout << "How many numbers do you want to add? ";
int a;
cin >> a;
int b = 0;
int sum = 0;
for (int i = 0; i < a; i ++){
// if i<= a doesn't work use i < a + 1
cout << "Please insert an interger you want to add: ";
cin >> b;
sum = sum + b;
}
}