功能&档 [英] Functions & Files
问题描述
你好,
我似乎有一个新手问题,写了一个由3个函数组成的简单程序。以下是对我的要求:
您的主要功能应该调用printMyName函数,该函数将打印出您的姓名和程序说明(详见下文) />
接下来,在while循环中你应该执行以下操作:
读入三个整数
将所有三个数字打印到输出文件中(hw3。出)。
调用函数findLargest,它将返回3个值中最大的一个。
调用函数findAverage,它将返回三个数字的平均值。
打印出三个数字的最大和平均值。
再次执行此循环,即打印一条好消息,询问继续(是或否)?。
以下是三个函数的描述:
printMyName - 此函数不返回任何内容,也没有参数。它会将您的姓名和程序说明打印到输出文件中。
findLargest - 将接受三个整数参数,并返回一个整数,这是三个数中最大的一个。
findAverage - 将接受三个整数参数,并返回一个double,即三个数字的平均值。
到目前为止,我设法让它以原始的方式工作,没有任何功能,请帮助我按照我要求的方式将其修改为函数:
#include< iostream>
#include< fstream>
#include< math.h>
using namespace std;
int main()
{
int a,b,c,max,average;
char resume;
while(resume!=''n''){//读取3个整数后;用户输入''n''到结束循环
cin>> a;
cin>> b;
cin>> c;
双倍平均值=(a + b + c)/ 3.0;
如果(a> b){
max = a ;
}
else {
max = b;
}
if (c>最大){
max = c;
}
cout<< 最大数字是: << max<< endl;
cout<< 平均值是: <<平均<< endl;
cout<< 你想继续(是/否)?\ n;
cin>>恢复; //用户输入''n'到结束循环。或''y''继续。
ofstream outfile(" hw3.out"); //将outfile信息发送到文件hw3.out
outfile<< "名称" << endl;
outfile<< 该程序读取3个整数,找到最大和平均值,然后将信息发送到文件。 << endl;
outfile<< a<< " " << b<< " " << c<< endl;
outfile<< 最大数字是: << max<< endl;
outfile<< 平均值是: <<平均<< endl;
outfile.close();
}
返回0;
}
感谢任何帮助。
Hello,
I seem to be having a newbie problem with writng a simple program consisting of 3 functions. Here is what''s being asked of me:
Your main function should call the function printMyName which will print out your name and a description of the program (see details below)
Next, in a while loop you should do the following:
read in three integers
print out all three numbers to your output file (hw3.out).
call the function findLargest, which will return the largest of the 3 values.
call the function findAverage which will return the average of the three numbers.
print out the largest and average of the three numbers.
Do this loop again i.e. print a nice message asking "Continue(y or n)?"
Here is the description of the three functions:
printMyName - This function will not return anything and will have no parameters. It will print your name and a description of the program to your Output File.
findLargest - will take in three integer parameters, and return an integer, which is the largest of the three numbers.
findAverage - will take in three integer parameters, and return a double, which is the average of the three numbers.
So far I managed to make it work in primitive way without any functions, please help me modify this into functions the way Im being asked to do:
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
int main()
{
int a, b, c, max, average;
char resume;
while (resume != ''n''){ //After 3 integers are read; user enters ''n'' to end loop
cin >> a;
cin >> b;
cin >> c;
double average = (a + b + c) / 3.0;
if ( a > b ){
max = a;
}
else{
max = b;
}
if ( c > max ){
max = c;
}
cout << "The Largest Number is: " << max << endl;
cout << "The Average is: " << average << endl;
cout << "Do you want to continue (y/n)?\n";
cin >> resume; //User enters ''n'' to end loop. or ''y'' to continue.
ofstream outfile("hw3.out"); //sends outfile info to file "hw3.out"
outfile << "name" << endl;
outfile << "This program reads 3 integers, finds largest and average, then sends information to file" << endl;
outfile << a << " " << b << " " << c << endl;
outfile << "The Largest Number is: " << max << endl;
outfile << "The Average is: " << average << endl;
outfile.close();
}
return 0;
}
Any help is appreciated.
推荐答案
我认为你的3个功能就是这些,试着用修改它们......:D:D:D
i think your 3 functions are like these, try to use and modify them... :D:D:D
我正在尝试类似的功能,它总是提出相同的功能错误,你提供的代码也会出现:
[链接器错误]未定义引用`WinMain @ 16''
ld r eturned 1退出状态
您能否对我需要返回的内容稍微具体一点,我是否需要在此程序中使用int main()?
感谢您的帮助。
I was trying similar functions and it always comes up with same error, the code you provided comes up with it as well:
[Linker error] undefined reference to `WinMain@16''
ld returned 1 exit status
Can you please be little more specific on what I need to return, and do I need to use int main () in this program?
Thanks for your help.
我能够获得1个功能,但我不知道如何再添加2个功能。请帮忙。
I was able to get 1 function to work, but I''m not sure how to add 2 more functions to it. Please help.
这篇关于功能&档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!