How to add a variables to a txt file without an array

Hi there,
I am looking for an answer...
I have a code part which I need all it's variables to be put into a txt(named x1) file.

1
2
3
4
for(int i=1;i<=x;i++)
{
cin >> z;
}


How do I put all the z's into the file?
Didn't answer my question.
Didn't answer my question.


Actually he did. There is all you need in the link that he gave you.
@lastchance
Actually he did not. As you may know. You can only put a variable or anything you want into a text file one at a time, which means, if you have more variables which I am entering with the cin as a z for example ( 12,2,3,45) it only puts one number into the file if you are doing this without an array. But i need them all in the file.. maybe i wasn't clear enough sorry
No Darkiyee, you bother to read the link that he gave you. The examples in that tutorial do not use an array.


Open a file before your loop. One line of code - see the tutorial example for opening files.

Every time you go round that loop you read one more z value. As soon as you have that z value - on the next line for example - you can write it to the file. One line of code - see the tutorial.

When you finish looping you can close the file if you want to. One line of code - see the tutorial.


By then your file will have all the z values you entered while you were looping

I haven't a clue what arrays have got to do with it.
Topic archived. No new replies allowed.