function help

This is a discussion on function help within the C++ Programming forums, part of the General Programming Boards category; Hi, Im trying to write the output from each function all to the same file "PROCOUT.DAT" but it will only ...

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    function help

    Hi,
    Im trying to write the output from each function all to the same file "PROCOUT.DAT" but it will only write the last function to the file.

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    void GETA(float);
    void GETB(float);
    void GETC(float);
    void GETD(float);
    void GETE(float);
    void GETF(float);
    
    void main()
    {
    		
    	float VALUEA;
    	VALUEA = 0;
    	GETA(VALUEA);
    
                    float(VALUEF);
    	VALUEF = 0;
    	GETF(VALUEF);
    }
    void GETA(float AVALUE)
    {
    	ifstream FILEIN;
    	ofstream FILEOUT;
    	FILEIN.open("A:DATFILE1.txt");
    	FILEOUT.open("A:PROCOUT.DAT");
    
    	FILEIN >> AVALUE;
    
    	FILEOUT << "A.) ";
    	while(FILEIN)
    	{
    		FILEOUT << AVALUE << ' ';		
    		FILEIN >> AVALUE;
    	}
    	FILEOUT << endl;
    	FILEIN.close();
    	FILEIN.clear();
    }
    void GETF(float FVALUE)
    {
    	ifstream FILEIN;
    	ofstream FILEOUT;
    	FILEIN.open("A:DATFILE1.txt");
    	FILEOUT.open("A:PROCOUT.DAT");
    
    	float NUM1;
    	NUM1 = 0;
    	float NUM2;
    	NUM2 = 0;
    
    	FILEIN >> NUM1;
    
    	while(FILEIN)
    	{
    		FILEIN >> NUM1;
    		if(abs(NUM1-200)==33)	
    			NUM2 = NUM1;		
    	}
    	FILEOUT << endl << "F.) " << NUM2 << endl;
    	FILEIN.close();
    	FILEIN.clear();
    }
    The functions are A through F but i only posted A and F so it would be shorter. But the output file only shows the last function F. If I change them from writing to the file to cout, then they all show up fine.
    i use microsoft visual studio.net 2003.
    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    every function is probably working, but getting overwritten by the functions used after. look for functions to add to the end of the file (if this is what you want).

    to see if this is the case, edit the code to only call one function. run the code, check the file, then do this again for each function A-F.

    also, i think capitol letters for variables are for global variables, try to use a more standard format for local variables (and function names), ie: FILEOUT = fileOut or file_out or fileout

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    how do i get them to add to the file instead of overiting the previous one?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    read information on the constructor

  5. #5
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    i read that over, and made some changes but i still can't get it to work. it still overwrites the previous function.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    what did you put for the constructor? with programming if something isnt working as you like it, youll always need code or examples.

    notice im not giving you the answer, and wont. im trying to help you to learn it.

  7. #7
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    well, i'm not totally sure what a "constructor" even is. in the class i'm in, we have not learned what a "constructor" is. In fact, it is a few chapters ahead of what we are learning.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    is this a structured class in a college or university? tell the teacher theyre not teaching properly then. i dont see how you can be on problems such as this and they havent introduced what contructors are yet.. oh well.

    lets look at the 'open' function instead of the constructor. _read_ all of this page about the 'open' function of the ofstream class.

    the function has a signature of: void open ( const char * filename, openmode mode = out | trunc );

    a function is declared with an outline like this: return_type function_name (parameter_list);

    lets break this down for open:

    void = return type.. void means no value is returned to the function that called it.

    open = the name of the function

    parameter #1: const char * filename = a constant (const) pointer (*) to a character (char) named 'filename'. its basically an array of characters that doesnt change.. a string, more or less, such as "file.txt"

    parameter #2: openmode mode = out | trunc = the name of this variable is 'mode' and the type of variable is of 'openmode'. read this to see what values 'mode' can be. this is the solution to your problem..

    an example call of this function would be: fileout.open("myfile.txt", ios_base::out);
    this will open the file called "myfile.txt" in the current directory and allow output (writing) to this file.

    if your serious about programming then please read this, for example. or read C tutorials.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21