View Full Version: syntax question

C++ Learning Community > C++ Help > syntax question


Title: syntax question


homeslice - January 26, 2007 01:45 AM (GMT)
Yeah...Me again (sorry)...I just started a new chapter on "I/O streams as an introduction to objects and classes" and there's a program we ahve to do for it. I can do MOST of the program, but i need help on the syntax of part of it which does not seemed to be explained well in the book. It should have the user enter positive numbers and will write these to the file, and will terminate when the user enters -1.

My question is on the syntax while loop for this that checks the number before it is written to the file. I know you use 'ofstream' to have the user write things to a file, but how would you have the user entering the number to be immediately written to the file while still checking the numbers and disregarding all negatives (and terminating at -1).

Rmstn1580 - January 26, 2007 01:55 AM (GMT)
CODE
#include <iostream>
#include <fstream>
using namespace std;

int main() {
   ofstream file;
   file.open("output.txt", ios::app);
   int num = 0;
   while(num != -1) {
             cin >> num;
             if(num != -1)
                    file << num;
   }
   file.close();
   system("PAUSE");
}


There ya go lil buddy.

homeslice - January 26, 2007 02:04 AM (GMT)
Thanks =P. I think the book si saying only do this:

CODE
file.open("output.txt")


without the ios:app thing, but yeah thank you.

Edit: Hey to read out all of the numbers entered do you just do instream >> num?

Rmstn1580 - January 26, 2007 02:14 AM (GMT)
If you want to record all of the numbers you'll need ios::app because it allows your file to be appended instead of written over every time. If you don't include ios::app, every time a new number is entered, the file will get written over to contain that.

One more thing. Change "file << num;" to "file << num << endl;" :)

homeslice - January 26, 2007 02:23 AM (GMT)
To read out all the numbers is it just "instream >> file_name" (which I don't think it is), and how do you get some spaces between the numbers, is it like

CODE
instream >> file_name >> "    ";


or something?

Zaqufant - January 26, 2007 12:52 PM (GMT)
Um, what do you mean? I don't get what you're saying.

myork - January 26, 2007 02:29 PM (GMT)
QUOTE (homeslice @ Jan 25 2007, 09:04 PM)

CODE
file.open("output.txt")


without the ios:app thing


OK The difference is:

CODE
file.open("output.txt")
This opens a file for writting. But it destroys the old file before it opens the new one.

CODE
file.open("output.txt",ios:app)
This opens a file for writting. But the old file is untouched so any new values are appended onto the end of the old file.


QUOTE
CODE
if(num != -1)
                   file << num;

Close. But doing this concatenates all the numbers together into a single long stream.

Example:
If the user entered:
QUOTE
10
20
30
40
-1

The file would look like:
QUOTE
10203040
To compensate for this you should at least stick a space after each number.
CODE
file << num << " ";




Hosted for free by InvisionFree