编写一个录入函数:

来源:百度知道 编辑:UC知道 时间:2024/05/10 11:25:54
编写一个录入函数:用来从键盘接收n 个整型数并存放在一个整型的数组中,在主函数中调用该函数计算出这n个整数之和。
用C++编写

#include <iostream>

int func(n){
int temp;
int arr[n];
int i = 0;
std::cout<<"请输入"<<n<<"个整数:"<<std::endl;
while(i < n){
std::cin>>temp;
arr[i] = temp;
i++;
}
int result;
for(i = 0; i < n; i++){
result += arr[i];
}
return result;
}
int main){
const int MAX = 10; //随你改成多少
std::cout<<"您输入数字的总和是:"<<func(MAX)<<std::endl;
}

没调试过. 你自己调试一下排除一下可能的语法错误吧.