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

printing variable to text file

Reply to Message Icon

Original Message
Name: Spyked
Date: August 30, 2006 at 12:58:23 Pacific
Subject: printing variable to text file
OS: Windows XP sp2
CPU/Ram: Pentium 4/256mb
Model/Manufacturer: 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



Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: August 31, 2006 at 06:30:20 Pacific
Subject: printing variable to text file
Reply: (edit)

#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



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: printing variable to text file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge