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.
10 2634
i think your 3 functions are like these, try to use and modify them... :D:D:D - void printMyName()
-
{
-
cout<<"Your name here"<<endl;
-
cout<<"Program descriptions here"<<endl;
-
}
-
-
int findLargest(int a, int b, int c)
-
{
-
int largest=a;
-
if(largest<b)
-
largest=b;
-
if(largest<c)
-
largest=c;
-
return largest;
-
}
-
-
double findAverage(int a, int b, int c)
-
{
-
return (a+b+c)/3.0;
-
}
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.
I was able to get 1 function to work, but I'm not sure how to add 2 more functions to it. Please help. -
#include <iostream>
-
#include <fstream>
-
#include <math.h>
-
using namespace std;
-
int findLargest(int a, int b, int c)
-
{
-
cin >> a;
-
cin >> b;
-
cin >> c;
-
int largest = a;
-
if(largest < b)
-
largest = b;
-
if(largest < c)
-
largest=c;
-
return largest;
-
}
-
int main ()
-
{
-
int a, b, c, largest;
-
largest = findLargest(a, b, c);
-
cout << "The Largest Number is: " << largest << endl;
-
system("pause");
-
}
-
I was able to get 1 function to work, but I'm not sure how to add 2 more functions to it. Please help. -
#include <iostream>
-
#include <fstream>
-
#include <math.h>
-
using namespace std;
-
int findLargest(int a, int b, int c)
-
{
-
cin >> a;
-
cin >> b;
-
cin >> c;
-
int largest = a;
-
if(largest < b)
-
largest = b;
-
if(largest < c)
-
largest=c;
-
return largest;
-
}
-
int main ()
-
{
-
int a, b, c, largest;
-
largest = findLargest(a, b, c);
-
cout << "The Largest Number is: " << largest << endl;
-
system("pause");
-
}
-
Try this one, just compiled it and it should work: -
-
#include <iostream>
-
#include <fstream>
-
#include <math.h>
-
using namespace std;
-
-
int findLargest(int a, int b, int c);
-
void printMyName();
-
double findAverage(int a, int b, int c);
-
-
int main ()
-
{
-
int a, b, c, largest;
-
double average;
-
-
// First function
-
printMyName();
-
-
// Get variable values
-
-
cout << "Enter three values:" << endl;
-
cin >> a;
-
cin >> b;
-
cin >> c;
-
-
// Second function
-
largest = findLargest(a, b, c);
-
cout << "The Largest Number is: " << largest << endl;
-
-
//Third function
-
average = findAverage(a, b, c);
-
cout << "The Average Number is: " << average << endl;
-
-
-
system("pause");
-
}
-
-
int findLargest(int a, int b, int c)
-
{
-
int largest = a;
-
if(largest < b)
-
largest = b;
-
if(largest < c)
-
largest=c;
-
return largest;
-
}
-
-
void printMyName()
-
{
-
cout<<"Your name here"<<endl;
-
cout<<"Program descriptions here"<<endl;
-
}
-
-
double findAverage(int a, int b, int c)
-
{
-
return (a+b+c)/3.0;
-
}
-
-
Thank you, The program works great, I really appreciate all your help!
Just one more thing, i modified the program to work in a while loop, how do I make all output go into a file using ofstream outfile("file.out");? Here's a piece of this program with the while loop part: -
#include <iostream>
-
#include <fstream>
-
#include <math.h>
-
using namespace std;
-
int findLargest(int a, int b, int c);
-
void printMyName();
-
double findAverage(int a, int b, int c);
-
int main ()
-
{
-
int a, b, c, largest;
-
double average;
-
char resume;
-
while (resume != 'n'){//After 3 integers are read; user enters 'n' to end loop
-
// First function
-
printMyName();
-
// Get variable values
-
cout << "Enter three numbers:" << endl;
-
cin >> a;
-
cin >> b;
-
cin >> c;
-
// Second function
-
largest = findLargest(a, b, c);
-
cout << "The Largest Number is: " << largest << endl;
-
//Third function
-
average = findAverage(a, b, c);
-
cout << "The Average Number 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 << printMyName() << endl;
-
}
-
Thank you, The program works great, I really appreciate all your help!
Just one more thing, i modified the program to work in a while loop. How do I make all output go into a file using ofstream outfile("file.out");? Here's a piece of this program with the while loop part: -
#include <iostream>
-
#include <fstream>
-
#include <math.h>
-
using namespace std;
-
int findLargest(int a, int b, int c);
-
void printMyName();
-
double findAverage(int a, int b, int c);
-
int main ()
-
{
-
int a, b, c, largest;
-
double average;
-
char resume;
-
while (resume != 'n'){
-
// First function
-
printMyName();
-
// Get variable values
-
cout << "Enter three numbers:" << endl;
-
cin >> a;
-
cin >> b;
-
cin >> c;
-
// Second function
-
largest = findLargest(a, b, c);
-
cout << "The Largest Number is: " << largest << endl;
-
//Third function
-
average = findAverage(a, b, c);
-
cout << "The Average Number 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"
-
}
-
Thank you, The program works great, I really appreciate all your help!
Just one more thing, i modified the program to work in a while loop. How do I make all output go into a file using ofstream outfile("file.out");? Here's a piece of this program with the while loop part: -
#include <iostream>
-
#include <fstream>
-
#include <math.h>
-
using namespace std;
-
int findLargest(int a, int b, int c);
-
void printMyName();
-
double findAverage(int a, int b, int c);
-
int main ()
-
{
-
int a, b, c, largest;
-
double average;
-
char resume;
-
while (resume != 'n'){
-
// First function
-
printMyName();
-
// Get variable values
-
cout << "Enter three numbers:" << endl;
-
cin >> a;
-
cin >> b;
-
cin >> c;
-
// Second function
-
largest = findLargest(a, b, c);
-
cout << "The Largest Number is: " << largest << endl;
-
//Third function
-
average = findAverage(a, b, c);
-
cout << "The Average Number 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"
-
}
-
There's quite a bit involved with the ofstream functions, check this out and see if it helps. If not, let me know, and I'll see if I can put something together for you. Thanks!
http://www.cplusplus.com/ref/iostream/ofstream/
Thank you, The program works great, I really appreciate all your help!
Just one more thing, i modified the program to work in a while loop. How do I make all output go into a file using ofstream outfile("file.out");? Here's a piece of this program with the while loop part: -
#include <iostream>
-
#include <fstream>
-
#include <math.h>
-
using namespace std;
-
int findLargest(int a, int b, int c);
-
void printMyName();
-
double findAverage(int a, int b, int c);
-
int main ()
-
{
-
int a, b, c, largest;
-
double average;
-
char resume;
-
while (resume != 'n'){
-
// First function
-
printMyName();
-
// Get variable values
-
cout << "Enter three numbers:" << endl;
-
cin >> a;
-
cin >> b;
-
cin >> c;
-
// Second function
-
largest = findLargest(a, b, c);
-
cout << "The Largest Number is: " << largest << endl;
-
//Third function
-
average = findAverage(a, b, c);
-
cout << "The Average Number 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"
-
}
-
Nevermind, I worked it out for you. Again, try and see if you can get it by looking at the instructions, if not, this will work:
(FYI - I changed your while loop to a do/while loop... char isn't initialized at a value, so the do/while will occur at least once, to be safe, in all cases) -
#include <iostream>
-
#include <fstream>
-
#include <math.h>
-
using namespace std;
-
-
int findLargest(int a, int b, int c);
-
void printMyName();
-
double findAverage(int a, int b, int c);
-
-
int main ()
-
{
-
int a, b, c, largest;
-
double average;
-
char resume;
-
-
ofstream outfile;
-
outfile.open("hw3.out", ofstream::out);
-
-
do
-
{
-
-
// First function
-
printMyName();
-
-
// Get variable values
-
-
cout << "Enter three values:" << endl;
-
cin >> a;
-
cin >> b;
-
cin >> c;
-
-
outfile << a << ", " << b << ", " << c << endl;
-
-
// Second function
-
largest = findLargest(a, b, c);
-
cout << "The Largest Number is: " << largest << endl;
-
outfile << "The Largest Number is: " << largest << endl;
-
-
//Third function
-
average = findAverage(a, b, c);
-
cout << "The Average Number is: " << average << endl;
-
outfile << "The Average Number is: " << average << endl;
-
-
cout << "Do you want to continue? (y/n)?" << endl;
-
cin >> resume;
-
}
-
while (resume != 'n');
-
-
outfile.close();
-
}
-
-
int findLargest(int a, int b, int c)
-
{
-
int largest = a;
-
if(largest < b)
-
largest = b;
-
if(largest < c)
-
largest=c;
-
return largest;
-
}
-
-
void printMyName()
-
{
-
cout<<"Your name here"<<endl;
-
cout<<"Program descriptions here"<<endl;
-
}
-
-
double findAverage(int a, int b, int c)
-
{
-
return (a+b+c)/3.0;
-
}
-
Thank you apusateri, you cleared up a lot of things for me. Also, thats pretty useful source you gave me, Im trying to figure out how to make all output automatically go to file without specifying "outfile" each time (if thats possible).
Thank you apusateri, you cleared up a lot of things for me. Also, thats pretty useful source you gave me, Im trying to figure out how to make all output automatically go to file without specifying "outfile" each time (if thats possible).
No problem, glad I could help - as far as automatically putting the data in a file, I'm not sure if, or how, it could be done, but I would check around on that resource, it has quite a bit of good information.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: MLH |
last post by:
A programmer developed an AMP (Apache/MySQL/PHP) application
for me. When he was done, he sent me the PHP files and the MySQL
dump file. Now, when I connect to the application on my LAN using...
|
by: johkar |
last post by:
When the output method is set to xml, even though I have CDATA around
my JavaScript, the operaters of && and < are converted to XML character
entities which causes errors in my JavaScript. I know...
|
by: Jeff |
last post by:
Hello everybody,
I have a question concerning function declarations. When exactly do you have
to declare functions if you want to use them? I have two functions main()
and foo(), respectively...
|
by: Bob Rock |
last post by:
Hello,
in the last few days I've made my first few attempts at creating mixed
C++ managed-unmanaged assemblies and looking afterwards with ILDASM
at what is visible in those assemblies from a...
|
by: SHC |
last post by:
Hi all,
I saw the following 2 files used in the OpenGL and DirectX 9 programming:
1) resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by...
|
by: D. Shane Fowlkes |
last post by:
I posted this on another forum, and as I feared, the response(s) were too
complex and sophisticated. I certainly don't mind learning new methods, in
fact, that's why I asked, but I was hoping to...
|
by: EoRaptor013 |
last post by:
Not sure where to ask this question, but...
I'm using a TreeView component to enable browsing file folders in a
specific directory (for test purposes /Program Files/). Some users use
an ampersand...
|
by: John Nagle |
last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally
hit content which has something like "Design & Advertising", that is,
an "&" instead of an "&". Is there some way I can get...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |