-
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
-
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
-
how do i get them to add to the file instead of overiting the previous one?
-
read information on the constructor
-
i read that over, and made some changes but i still can't get it to work. it still overwrites the previous function.
-
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.
-
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.
-
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.