not writing to file.

Hello, this is my first post so go easy if I break a rule or several.
I'm new to coding and I'm trying to teach myself through the tutorials and reading others questions and replies. I have a working code that asks for an input code to unlock the program. Once its opened it starts a calc function.
Once a calc function is done it asks if there is anything else. Depending on the user response it either closes or goes back to the calc function.
I'm trying to get it to write out to a file so I can record what code and time
The program was started.
Everything works but the write out. It compiles without errors
But doesn't write out.
I'm working on windows 7, using MS visual 2010 exspress.


[code]
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>

Using namespace std;

Inline void delay (int sec)
{
Clock_t start_time = clock ();
Clock_t end_time = sec * 1000 + start_time;
While (clock () != end_time);
}

Int main ()
{
Char operation;
Cout <<"input code" << endl;
Cin >> operation ;
Switch (operation)
{
Case '2':
Cout << "Hello Bob." <<endl;
Break;
Case '4':
Cout << "Hello Lee." <<endl;
Break;
Default:
Cout << "invalid code." <<endl;
Cout << "program terminated." <<endl;
Delay (3);
Return -1;
}
{ //this is where the write out code starts
Std::ofstream ofs;
Ofs.open ("Mcd.txt", std::ofstream::out);
Ofs <<"why don't this write?" <<operation << clock ;
Ofs.close ();
} //this is where it ends
Calc:
Float a, b, result;
Cout <<"calculator ready...."<<endl;
Cin >> a >> operation >> b;
Switch (operation)
{
Case '+':
Result = a + b;
Break;
Case '-':
Result = a - b;
Break;
Default:
Cout << "invalide operation" << endl;
Cout << "program terminated" << endl;
Delay (3)
Return -2;
}
Cout << "the answer is " <<result << "." << endl;
Delay (1);
Cout <<"anything else?" <<endl;
Cout <<"y or n?" << endl;
Cin >> operation ;
If (operation == 'y' )
Goto Calc;
Cout << "good bye." << endl;
Delay (3);
Return 0;
}


So that's my code minus a couple of the calc functions to shorten it up a bit.
For some stupid reason the first letter of your sentences is capitalized, fix that first.
closed account (j3Rz8vqX)
Something to look at:
http://www.cplusplus.com/doc/tutorial/files/

Edit:
 
ofs.open ("Mcd.txt", std::ios::out);

Last edited on
ne555 wrote:
For some stupid reason the first letter of your sentences is capitalized, fix that first.
ne555 means the first letter of your lines of code, not your sentences.
The first letter is capped because I sent this from my cell phone. It auto caps every code line thinking it was a new sentence. The real code was not typed this way. I stated that the code actually worked except for writing out portion. For some stupid reason I guess ne555 never read that part.

Dput thanks a lot this actually worked. Where exactly does this write the file to? The only way I locate it is to use the start button and search for Mcd. Its there and written in notepad just like I wanted but I wood like to be able to make it save the file in documents if possible.
Last edited on
closed account (j3Rz8vqX)
It writes it to where you executed the program from.

Example:

If you're using an IDE:
Project folder has your source files.
In the project folder, there should be a debug folder.
It will have your executable as well as your output file.
Last edited on
Dput, I have found the file. I have it writing the operation to record the input code that unlocked the program. I still have some work to do to get the time and date the input code was entered. Also I will need to learn how to write to the end of the file without erasing what's already there so I can have a record of the use of the program. You have set me on the right track to solvi g these problems. Thank you again. HAPPY NEW YEAR to you.
Dput, I just got my code writing out to file with date time and the unlock code all in a nice easy to read format. You really helped a lot. I'm hugely gratful. Thank you again.
closed account (j3Rz8vqX)
No problem, it was all you.
Happy New Years.
Topic archived. No new replies allowed.