PDA

View Full Version : C++


omgitsroy326
Jan21-04, 08:34 AM
I have a program due in couple hours. Professor expects us to know C++ when we never really went over programming; he expects us to just know it .. anyhow


I have two inputs used with scanf

it's toll and norm

these two values need to be put into a data file created by the program

I need a key that allows me to
create a "data.txt" file and if the file exist to overwrite it by erased etc.. doesn't matter and put in the things as

printf(" Toll = %e \m Norm = %e", toll, norm);

and just exit the program

i know it's coded in C... but that's all i know
anyhow if i can code in C or C++ doesnt' matter also could you tell me what library i can use.

enigma
Jan21-04, 10:18 AM
Originally posted by omgitsroy326
I have a program due in couple hours. Professor expects us to know C++ when we never really went over programming; he expects us to just know it .. anyhow


I have two inputs used with scanf

it's toll and norm

these two values need to be put into a data file created by the program

I need a key that allows me to
create a "data.txt" file and if the file exist to overwrite it by erased etc.. doesn't matter and put in the things as

printf(" Toll = %e \m Norm = %e", toll, norm);

and just exit the program

i know it's coded in C... but that's all i know
anyhow if i can code in C or C++ doesnt' matter also could you tell me what library i can use.

Hi omgitsroy

I don't have my C++ book on me, so the syntax may be a bit off.


#include <fstream> // File operations

.
.
.

ofstream output("data.txt" , ios::out) ; //Opens a file object 'output' to file "data.txt"
//for output, overwrites if existing

.
.
.

output<<"Toll = "<< toll <<"\m Norm = "<< norm << endl; //Outputs to file just like 'cout' command



I don't remember how to explicitly close the file, but I don't think it's really necessary unless your program is complex. Formatting can be done with standard i/o commands.

Hope that's all correct...

omgitsroy326
Jan21-04, 06:25 PM
thanks a lot