Functions & Files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • varusnyc
    New Member
    • Oct 2006
    • 12

    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.ou t"); //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.
  • ltgbau
    New Member
    • Sep 2006
    • 41

    #2
    i think your 3 functions are like these, try to use and modify them... :D:D:D
    Code:
    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;
    }

    Comment

    • varusnyc
      New Member
      • Oct 2006
      • 12

      #3
      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.

      Comment

      • varusnyc
        New Member
        • Oct 2006
        • 12

        #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.
        Code:
        #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");
        }

        Comment

        • apusateri
          New Member
          • Oct 2006
          • 27

          #5
          Originally posted by varusnyc
          I was able to get 1 function to work, but I'm not sure how to add 2 more functions to it. Please help.
          Code:
          #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:

          Code:
          #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;
          }

          Comment

          • varusnyc
            New Member
            • Oct 2006
            • 12

            #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.o ut");? Here's a piece of this program with the while loop part:
            Code:
            #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;
            }

            Comment

            • varusnyc
              New Member
              • Oct 2006
              • 12

              #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.o ut");? Here's a piece of this program with the while loop part:
              Code:
              #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"
              }

              Comment

              • apusateri
                New Member
                • Oct 2006
                • 27

                #8
                Originally posted by varusnyc
                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.o ut");? Here's a piece of this program with the while loop part:
                Code:
                #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.c om/ref/iostream/ofstream/

                Comment

                • apusateri
                  New Member
                  • Oct 2006
                  • 27

                  #9
                  Originally posted by varusnyc
                  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.o ut");? Here's a piece of this program with the while loop part:
                  Code:
                  #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)

                  Code:
                  #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;
                  }

                  Comment

                  • varusnyc
                    New Member
                    • Oct 2006
                    • 12

                    #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).

                    Comment

                    • apusateri
                      New Member
                      • Oct 2006
                      • 27

                      #11
                      Originally posted by varusnyc
                      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.

                      Comment

                      Working...