Computing.Net > Forums > Programming > printing variable to text file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

printing variable to text file

Reply to Message Icon

Name: Spyked
Date: August 30, 2006 at 12:58:23 Pacific
OS: Windows XP sp2
CPU/Ram: Pentium 4/256mb
Product: generic
Comment:

hello all, i just recently got started programming in C++ using tutorials on the web and i'm learning how to read and wright to text files however i am stuck. i want my program to simply output the # 1-100 to a text file which is created, however i can't figure out exactly how to do this, the code i have so far is

[code]
#include <cstdlib>
#include <iostream>
#include <conio.h> // for getch()
#include <fstream> // for ofstream
using namespace std;

int main()
{
ofstream a_file( "numbers.txt" );
int x;
cout<<"this program will make a txt file called numbers and output 0 - 100 in it";
for ( int x = 0; x < 100; x++); // make x inrement up to 100

a_file << x; // only prints the actual x not the variable??
a_file.close(); // closes file


getch(); // wait for keypress
};

[/code]
hopefully someone can help me with this problem, as i said before i'm really new to this language so i might of made some other mistakes as well...thanks in advance,
also i am using Dev c++ latest version




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: August 31, 2006 at 06:30:20 Pacific
Reply:

#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <fstream.h>

int main()
{
ofstream a_file( "numbers.txt" );
int x;
for ( x = 0; x <= 100; x++) a_file << x << '\n';
a_file.close();
}



=====================================
If at first you don't succeed, you're about average.

M2



0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: printing variable to text file

passing a value to text file www.computing.net/answers/programming/passing-a-value-to-text-file-/18831.html

HTML How do I write to text file? www.computing.net/answers/programming/html-how-do-i-write-to-text-file/9317.html

Pass id & pw variables from bat to text file www.computing.net/answers/programming/pass-id-pw-variables-from-bat-to-text-file/19299.html