home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Writing info to another File. C++ Programming question

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2732
Discussions: 200,998, Posts: 2,379,981, Members: 246,367
Old April 15th, 2009, 06:25 PM   Digg it!   #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 fr
om 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 06:36 PM.
valk181 is offline   Reply With Quote
Old April 19th, 2009, 11:19 AM     #2 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,242
Send a message via AIM to Rootstonian
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 11:26 AM.
Rootstonian is offline   Reply With Quote
Old April 19th, 2009, 02:04 PM     #3 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,242
Send a message via AIM to Rootstonian
I'm curious...can I ask how long you're given to do this assignment?
Rootstonian is offline   Reply With Quote
Old April 19th, 2009, 08: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 08:26 PM.
valk181 is offline   Reply With Quote
Old April 19th, 2009, 08:47 PM     #5 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,242
Send a message via AIM to Rootstonian
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.
Rootstonian is offline   Reply With Quote
Old April 19th, 2009, 08: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.
valk181 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming question RustedPC Certification and Education 5 April 21st, 2009 08:51 PM
C programming question valk181 Webmastering and Programming 7 April 17th, 2009 11:52 PM
PHP & ASP writing to a excel file ShuckyD Webmastering and Programming 2 August 27th, 2007 03:23 PM
Help with writing .bat file MitaDC Applications and Operating Systems 1 June 21st, 2006 03:50 PM
Programming RPGs with dx 8.0 question I want PRPGWDX Webmastering and Programming 0 October 26th, 2002 01:00 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3104)
‘Rogue’ or ‘Rouge’? (7)
Charges against non-tippers dropped.. (22)
Foxconn Blackops x48 MoBo (5)
Nvidia GTX 260 problem (14)
Delete an OS (18)
Laptop with wireless problem. (13)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
windows vista security holes (19)
Regular Build (11)
Point and Shoot Camera Suggestions. (9)
[F@H SPAM 11/16/09] ! 1/2 months to.. (41)
windows 7 problem (7)
Recent Discussions
EVGA 9800 gtx help with finding a goo.. (13)
"Documents and Settings" fo.. (7)
Delete an OS (18)
Outputing 1080p from my PC to my 720p.. (0)
panasonic dmr ez48veb recorder (0)
add ram to existing (3)
Need help getting speakers to work (2)
Nvidia GTX 260 problem (14)
Laptop with wireless problem. (13)
Point and Shoot Camera Suggestions. (9)
Is the PSU I received dead? (16)
FreeAgent drive software not x64 comp.. (1)
Intel 5100 AGN issues fixed yet? (28)
Foxconn Blackops x48 MoBo (5)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (41)
Print spooler problem (17)
Q9650 vs. Q9550 (2)
Desktop Calendar Application (2)
Looking for new motherboard (1)
soundmon.exe (8)
Jedi Academy Problem (3)
Can a page file be "too big".. (1)
Size after cutting 700Mb file is 2.5 .. (0)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)


All times are GMT -4. The time now is 01:16 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28