Writing info to another File. C++ Programming question  | |
April 15th, 2009, 05:25 PM
|
#1 (permalink)
| | Junior Member
Join Date: Apr 2009
Posts: 11
| Writing info to another File. C++ Programming question We are suppost to write a program that open a text file for output then accepts an integer from the console and a float value also from console while a 0 intered for the interger ends and closes it.
accept an integer from the console and a float value from the console (in
separate input operations) and write the numbers to the file. An integer
value of 0 should be used as a code value to cause the program to quit
accepting numbers from the console and close the file.
Here is an example of how the input should be done:
Enter an integer (0 to quit): 34
Enter a float value: 2.37
Enter an integer (0 to quit): 127
Enter a float value: -42.7
Enter an integer (0 to quit): 0
(program ends)
I don't really understand why we use a float as well... but yeah.
My question is what sort of code would you use to write onto a different text file.
Last edited by valk181 : April 15th, 2009 at 05:36 PM.
|
| |
April 19th, 2009, 10:19 AM
|
#2 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,279
|
Not sure what you mean by "different" text file. Basic assignment to take console input and store in a file. In psuedo code:
main() {
open file
while input <> 0
get integer
write integer to file
get float
write float to file
end while
close file
} //end main
The teacher might be setting you up to read this file back in for your next program to teach you to read in integer and float type data into a program  Ah! Tricky, huh? You read in -27.65 as ASCII...how do you make that numeric so you can do math with it??? (which might make you think.."what happens when I try to write out -42.7 to a FILE?") Hmmmm...
You're using C++; I'll give you a hint...think about your iostream.h and how cout works 
Last edited by Rootstonian : April 19th, 2009 at 10:26 AM.
|
| |
April 19th, 2009, 01:04 PM
|
#3 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,279
|
I'm curious...can I ask how long you're given to do this assignment? |
| |
April 19th, 2009, 07:11 PM
|
#4 (permalink)
| | Junior Member
Join Date: Apr 2009
Posts: 11
|
We were given programs at the beginning of the semester and had to have them all finished by the end of tonight.
That particular program was the 2nd to last one, the last was indeed to read text in the console from an outside source.
__________________________________________________ ______________
This one was actually a great deal easier than I was making it out to be...
For any others that could use help with it simply include the
#include <fstream>------- in your #includes,
ofstream file -----you can use whatever for file, if your doing multiple files file1 would be best though. input this inside of the actual main.
file.open("myfile") -- following ofstream, inside of the "'s goes the name of the file your writing.
then in your program include whatever the person put in say...
cin >>x;
file << x << endl;
The other things you'd have to enter depend on your program. I hope this helps anyone that was having the same problems I was having <,<
Last edited by valk181 : April 19th, 2009 at 07:26 PM.
|
| |
April 19th, 2009, 07:47 PM
|
#5 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,279
|
Like I said, similar to iostream and cout, but instead of standard output (i.e. the screen), you're going to a file with fstream
It would help you to look at the actual include file and see what's in there...you'll find you can use stuff like file.eof(), file.getline() etc. Lots of cool stuff.
And please note what you are doing is Object Oriented Programming (OOP). ofstream is a class and open is a method within that class.
Powerful stuff young Jedi, powerful indeed.  |
| |
April 19th, 2009, 07:56 PM
|
#6 (permalink)
| | Junior Member
Join Date: Apr 2009
Posts: 11
|
Yeah we actually had to use file.getline for a previous assignment... which is kinda silly because it was harder to do than this last program- it wasn't until I figured out how to do that one this one that I could do that one <,<.
I'm just glad first year stuff doesn't get much harder than this haha, the stuff would get far to powerful for this young Jedi to do just through an online class otherwise haha. |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |