C++ 子程序简介
正在尝试实现编码提示 输入一个正数列表,找到这些数字的平均值,然后输出结果。使用子程序输入数字,使用函数查找平均值,使用子程序输出结果 我用传递参数进行了较小的调用,但这一次需要3个单独的子程序,1个用于输入,1个用于计算,1个用于显示结果。 到目前为止,我的程序没有启动输入的初始调用C++ 子程序简介,c++,C++,正在尝试实现编码提示 输入一个正数列表,找到这些数字的平均值,然后输出结果。使用子程序输入数字,使用函数查找平均值,使用子程序输出结果 我用传递参数进行了较小的调用,但这一次需要3个单独的子程序,1个用于输入,1个用于计算,1个用于显示结果。 到目前为止,我的程序没有启动输入的初始调用 #include <iostream> using namespace std; //prototypes int prompt(int sum, int count ); float avera
#include <iostream>
using namespace std;
//prototypes
int prompt(int sum, int count );
float average(int sum, int count);
void result(float avg);
int main()
{
int num;
cout << "Welcome to Keith's Averaging program";
cout << endl;
int prompt();
int average (int sum, int count);
void result (float avg);
return 0;
}
//Prototype Definitions
//get numbers from users
int prompt()
{
int num, sum, count;
cout << "Enter numbers and I will Average them." << endl;
cout << "Please enter a number: ";
cin >> num;
sum = sum + num;
if(num == 0)
{
cout << "Guess you don't want an average";
cout << endl;
cout << "Goodbye";
}
for(count=0; num !=0; count++)
{
cout << "Please enter a positive number, enter zero to compute the avg: ";
cin >> num;
if(num < 0)
{
cout << "Enter a positive number:";
cin >> num;
}
sum = sum + num;
}
#包括
使用名称空间std;
//原型
整数提示(整数和,整数计数);
浮动平均值(整数和、整数计数);
无效结果(浮动平均值);
int main()
{
int-num;
我已经在你的代码中添加了一些注释作为解释
#include <iostream>
using namespace std;
//prototypes // These are declarations, definitions should also contain
// same function signatures
int prompt(int& sum, int& count); // accept arguments as reference (Read about it)
float average(int& sum, int& count);
void result(float& avg);
int main()
{
// int num; // don't need num in this function, not used
int sum = 0, count = 0; // create variables sum and count and initialize them to 0
float avg;
cout << "Welcome to Keith's Averaging program";
cout << endl;
prompt(sum, count); // don't need function return type and argument return type when calling
// a function
cout << sum << " " << count << endl; // print the values after prompt() call
// prompt() call must have filled the values sum and count
average(sum, count);
result(avg);
return 0;
}
//Prototype Definitions
//get numbers from users
int prompt(int& sum, int& count)
{
int num;
cout << "Enter numbers and I will Average them." << endl;
cout << "Please enter a number: ";
cin >> num;
sum = sum + num;
if(num == 0)
{
cout << "Guess you don't want an average";
cout << endl;
cout << "Goodbye";
}
for(count=0; num !=0; count++)
{
cout << "Please enter a positive number, enter zero to compute the avg: ";
cin >> num;
if(num < 0)
{
cout << "Enter a positive number:";
cin >> num;
}
sum = sum + num;
}
}
float average(int& sum, int& count){
// TODO: implement this
}
void result(float& avg) {
// TODO: implement this
}
#包括
使用名称空间std;
//原型//这些是声明,定义也应该包含
//相同函数签名
int提示符(int&sum,int&count);//接受参数作为引用(阅读相关内容)
浮动平均值(整数与总和、整数与计数);
无效结果(浮动和平均值);
int main()
{
//int num;//此函数中不需要num,未使用
int sum=0,count=0;//创建变量sum和count并将其初始化为0
浮动平均值;
不能使用prompt()
而不是int-prompt();
?编辑:还有许多其他错误,例如prompt()的参数不够。我建议您在这里学习如何使用函数:int-prompt();
声明一个新函数,但不调用它。另外,请选择一个函数进行学习。