C/C++ Help! Coding a Program with Limited Knowledge of C/C++

AI Thread Summary
The discussion revolves around a programming assignment requiring the creation of a data file in C or C++. The user needs to read two inputs, "toll" and "norm," and write them to a file named "data.txt," ensuring that the file is overwritten if it already exists. A suggested solution involves using the `<fstream>` library for file operations, specifically utilizing `ofstream` to create or open the file for output. The syntax provided includes commands to format the output as "Toll = [value] Norm = [value]." There is also a mention that explicitly closing the file may not be necessary for simple programs. The overall focus is on achieving the task with minimal programming knowledge.
omgitsroy326
Messages
29
Reaction score
0
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.
 
Technology news on Phys.org


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.

Code:
#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...
 
thanks a lot
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top