Thread: Functions and files...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Functions and files...

    Hello,
    I am writing a program that reads a file. If the character read is a C, it will grab the next line which will be a radius of a circle, if the character is an R, it will obtain the next to lines which are the length and width of a reactangle, and so forth. After the program obtains the radius (for example), I need to have a fuction calculate the area of that circle based on the radius given from the file. After that, the program should send that area to a new file. How do I go about doing this? I've been trying for hours, but haven't gotten anywhere.

    Any ideas would be appreciated. Thank you.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Here is my code so far. I'm pretty stumped on how to get this working right.

    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    #include <iomanip>
    
    
    using namespace std;
    
    void heading();
    void printStars();
    void printLines();
    double areaCircle(ifstream& inp);
    
    
    int main()
    {
    	ifstream fin;
    	ofstream fout;
    
    	char shape;
        double radius, length, width, base, height;
    
    	fin.open("datalog.txt");
    	fout.open("myresults");
    
    	if (!fin) {
    		cout << "There was an error in opening that file." << endl;
    		cout << "Program is now shutting down..." << endl;
    		return 1;
    	}
    
    
    	while (!fin.eof()) {
    		fin >> shape;
    		switch (shape) {
    		case 'C': fin >> radius;
    			      fout >> areaCircle(inp);
                break;
    		case 'R': fin >> length;
    			      fin >> width;
    		    break;
    		case 'T': fin >> base;
    			      fin >> height;
    		}
    	}
    
    
    
        fin.close();
    	fout.close();
    
    
    	return 0;
    }
    
    void heading()
    {
    	cout << "SHAPES-R-US COMPUTING" << endl;
    	cout << endl;
    	cout << "Prepared by:" << endl;
    	cout << endl;
    	cout << "Programmer" << endl;
    	cout << endl;
    	cout << "April 1st, 2005";
    }
    
    void printStars()
    {
        int count;
    	
    	for (count = 0; count < 50; count++) {
    		cout << "*";
    	}
    }
    
    void printLines()
    {
    	int count;
    
    	for (count = 0; count < 50; count ++) {
    		cout << "-";
    	}
    }
    
    double areaCircle(ifstream& inp)
    {
    	double area;
    
    	area = 3.14 * pow(inp, 2);
    
    	return 0;
    
    
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about something like
    Code:
    		case 'C': fin >> radius;
    			      fout << areaCircle(radius);
    Along with
    double areaCircle(double inp);

    > return 0;
    return area;
    would be better.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    I originally had that, but I keep getting error messages.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    I am really stumped on this problem because all I need to do is get numbers, calculate numbers, and output the numbers into a new file with a list type format. I can't seem to output void functions in the new file, so how would I go about doing this? I can't rely on my code anymore because I am completely stuck on this and I am at the point of just switching random things around in the code until I get something.

    Any more ideas would be great.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I originally had that, but I keep getting error messages.
    Posting the code you tried, and the error messages you get would be a good idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    I got it under control. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. separating functions into diff files
    By cuizy in forum C Programming
    Replies: 1
    Last Post: 04-29-2009, 03:50 PM
  2. Replies: 7
    Last Post: 02-01-2009, 04:53 PM
  3. Trouble passing args to functions in other files
    By Midnight Coder in forum C Programming
    Replies: 6
    Last Post: 01-03-2009, 05:13 PM
  4. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  5. functions & external files
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-24-2001, 03:13 AM