473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Functions & Files

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.
Oct 10 '06 #1
10 2634
ltgbau
41
i think your 3 functions are like these, try to use and modify them... :D:D:D
Expand|Select|Wrap|Line Numbers
  1. void printMyName()
  2. {
  3.     cout<<"Your name here"<<endl;
  4.     cout<<"Program descriptions here"<<endl;
  5. }
  6.  
  7. int findLargest(int a, int b, int c)
  8. {
  9.     int largest=a;
  10.     if(largest<b)
  11.         largest=b;
  12.     if(largest<c)
  13.         largest=c;
  14.     return largest;
  15. }
  16.  
  17. double findAverage(int a, int b, int c)
  18. {
  19.     return (a+b+c)/3.0;
  20. }
Oct 10 '06 #2
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.
Oct 10 '06 #3
I was able to get 1 function to work, but I'm not sure how to add 2 more functions to it. Please help.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5. int findLargest(int a, int b, int c)
  6. {
  7.        cin >> a;
  8.        cin >> b;
  9.        cin >> c;
  10.     int largest = a;
  11.     if(largest < b)
  12.         largest = b;
  13.     if(largest < c)
  14.         largest=c;
  15.     return largest;
  16. }
  17. int main ()
  18. {
  19. int a, b, c, largest;
  20. largest = findLargest(a, b, c);
  21. cout << "The Largest Number is:  " << largest << endl;
  22. system("pause");
  23. }
  24.  
Oct 10 '06 #4
I was able to get 1 function to work, but I'm not sure how to add 2 more functions to it. Please help.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5. int findLargest(int a, int b, int c)
  6. {
  7.        cin >> a;
  8.        cin >> b;
  9.        cin >> c;
  10.     int largest = a;
  11.     if(largest < b)
  12.         largest = b;
  13.     if(largest < c)
  14.         largest=c;
  15.     return largest;
  16. }
  17. int main ()
  18. {
  19. int a, b, c, largest;
  20. largest = findLargest(a, b, c);
  21. cout << "The Largest Number is:  " << largest << endl;
  22. system("pause");
  23. }
  24.  

Try this one, just compiled it and it should work:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. int findLargest(int a, int b, int c);
  8. void printMyName();
  9. double findAverage(int a, int b, int c);
  10.  
  11. int main ()
  12. {
  13.     int a, b, c, largest;
  14.     double average;
  15.  
  16.     // First function
  17.     printMyName();
  18.  
  19.     // Get variable values
  20.  
  21.     cout << "Enter three values:" << endl;
  22.     cin >> a;
  23.     cin >> b;
  24.     cin >> c;
  25.  
  26.     // Second function
  27.     largest = findLargest(a, b, c);
  28.     cout << "The Largest Number is:  " << largest << endl;
  29.  
  30.     //Third function
  31.     average = findAverage(a, b, c);
  32.     cout << "The Average Number is:  " << average << endl;
  33.  
  34.  
  35.     system("pause");
  36. }
  37.  
  38. int findLargest(int a, int b, int c)
  39. {
  40.     int largest = a;
  41.     if(largest < b)
  42.         largest = b;
  43.     if(largest < c)
  44.         largest=c;
  45.     return largest;
  46. }
  47.  
  48. void printMyName()
  49. {
  50.     cout<<"Your name here"<<endl;
  51.     cout<<"Program descriptions here"<<endl;
  52. }
  53.  
  54. double findAverage(int a, int b, int c)
  55. {
  56.     return (a+b+c)/3.0;
  57. }
  58.  
  59.  
Oct 10 '06 #5
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5. int findLargest(int a, int b, int c);
  6. void printMyName();
  7. double findAverage(int a, int b, int c);
  8. int main ()
  9. {
  10.     int a, b, c, largest;
  11.     double average;
  12.     char resume;
  13. while (resume != 'n'){//After 3 integers are read; user enters 'n' to end loop
  14.     // First function
  15.     printMyName();
  16.     // Get variable values
  17.     cout << "Enter three numbers:" << endl;
  18.     cin >> a;
  19.     cin >> b;
  20.     cin >> c;
  21.     // Second function
  22.     largest = findLargest(a, b, c);
  23.     cout << "The Largest Number is:  " << largest << endl;
  24.     //Third function
  25.     average = findAverage(a, b, c);
  26.     cout << "The Average Number is:  " << average << endl;
  27.     cout << "Do you want to continue (y/n)?\n";
  28.     cin >> resume; //User enters 'n' to end loop. or 'y' to continue.
  29.     ofstream outfile("hw3.out");//sends outfile info to file "hw3.out"
  30.     outfile << printMyName() << endl;
  31. }
  32.  
Oct 10 '06 #6
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5. int findLargest(int a, int b, int c);
  6. void printMyName();
  7. double findAverage(int a, int b, int c);
  8. int main ()
  9. {
  10.     int a, b, c, largest;
  11.     double average;
  12.     char resume;
  13.                 while (resume != 'n'){
  14.     // First function
  15.     printMyName();
  16.     // Get variable values
  17.     cout << "Enter three numbers:" << endl;
  18.     cin >> a;
  19.     cin >> b;
  20.     cin >> c;
  21.     // Second function
  22.     largest = findLargest(a, b, c);
  23.     cout << "The Largest Number is:  " << largest << endl;
  24.     //Third function
  25.     average = findAverage(a, b, c);
  26.     cout << "The Average Number is:  " << average << endl;
  27.     cout << "Do you want to continue (y/n)?\n";
  28.     cin >> resume; //User enters 'n' to end loop. or 'y' to continue.
  29.     ofstream outfile("hw3.out");//sends outfile info to file "hw3.out"
  30. }
  31.  
Oct 10 '06 #7
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5. int findLargest(int a, int b, int c);
  6. void printMyName();
  7. double findAverage(int a, int b, int c);
  8. int main ()
  9. {
  10.     int a, b, c, largest;
  11.     double average;
  12.     char resume;
  13.                 while (resume != 'n'){
  14.     // First function
  15.     printMyName();
  16.     // Get variable values
  17.     cout << "Enter three numbers:" << endl;
  18.     cin >> a;
  19.     cin >> b;
  20.     cin >> c;
  21.     // Second function
  22.     largest = findLargest(a, b, c);
  23.     cout << "The Largest Number is:  " << largest << endl;
  24.     //Third function
  25.     average = findAverage(a, b, c);
  26.     cout << "The Average Number is:  " << average << endl;
  27.     cout << "Do you want to continue (y/n)?\n";
  28.     cin >> resume; //User enters 'n' to end loop. or 'y' to continue.
  29.     ofstream outfile("hw3.out");//sends outfile info to file "hw3.out"
  30. }
  31.  
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/
Oct 10 '06 #8
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5. int findLargest(int a, int b, int c);
  6. void printMyName();
  7. double findAverage(int a, int b, int c);
  8. int main ()
  9. {
  10.     int a, b, c, largest;
  11.     double average;
  12.     char resume;
  13.                 while (resume != 'n'){
  14.     // First function
  15.     printMyName();
  16.     // Get variable values
  17.     cout << "Enter three numbers:" << endl;
  18.     cin >> a;
  19.     cin >> b;
  20.     cin >> c;
  21.     // Second function
  22.     largest = findLargest(a, b, c);
  23.     cout << "The Largest Number is:  " << largest << endl;
  24.     //Third function
  25.     average = findAverage(a, b, c);
  26.     cout << "The Average Number is:  " << average << endl;
  27.     cout << "Do you want to continue (y/n)?\n";
  28.     cin >> resume; //User enters 'n' to end loop. or 'y' to continue.
  29.     ofstream outfile("hw3.out");//sends outfile info to file "hw3.out"
  30. }
  31.  
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)

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. int findLargest(int a, int b, int c);
  7. void printMyName();
  8. double findAverage(int a, int b, int c);
  9.  
  10. int main ()
  11. {
  12.     int a, b, c, largest;
  13.     double average;
  14.     char resume;
  15.  
  16.     ofstream outfile;
  17.     outfile.open("hw3.out", ofstream::out);
  18.  
  19.     do
  20.     {
  21.  
  22.     // First function
  23.     printMyName();
  24.  
  25.     // Get variable values
  26.  
  27.     cout << "Enter three values:" << endl;
  28.     cin >> a;
  29.     cin >> b;
  30.     cin >> c;
  31.  
  32.     outfile << a << ", " << b << ", " << c << endl;
  33.  
  34.     // Second function
  35.     largest = findLargest(a, b, c);
  36.     cout << "The Largest Number is:  " << largest << endl;
  37.     outfile << "The Largest Number is: " << largest << endl;
  38.  
  39.     //Third function
  40.     average = findAverage(a, b, c);
  41.     cout << "The Average Number is:  " << average << endl;
  42.     outfile << "The Average Number is:  " << average << endl;
  43.  
  44.     cout << "Do you want to continue? (y/n)?" << endl;
  45.     cin >> resume;
  46.     }
  47.     while (resume != 'n');
  48.  
  49.     outfile.close();
  50. }
  51.  
  52. int findLargest(int a, int b, int c)
  53. {
  54.     int largest = a;
  55.     if(largest < b)
  56.         largest = b;
  57.     if(largest < c)
  58.         largest=c;
  59.     return largest;
  60. }
  61.  
  62. void printMyName()
  63. {
  64.     cout<<"Your name here"<<endl;
  65.     cout<<"Program descriptions here"<<endl;
  66. }
  67.  
  68. double findAverage(int a, int b, int c)
  69. {
  70.     return (a+b+c)/3.0;
  71. }
  72.  
Oct 10 '06 #9
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).
Oct 10 '06 #10
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.
Oct 11 '06 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
4
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...
1
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...
2
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...
0
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...
6
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...
1
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...
7
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 "&amp;". Is there some way I can get...
0
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
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.