4

So basically I'll start with the code and my problem is written below.

string words = "";
fstream outFile;
outFile.open(name, fstream::out);
for (int i = 0; i < number; ++i)
{
    getline(cin, words);
    outFile << i << ": " << words << endl;
    words == "";
}

number is given by user.

The thing I wanted to do was to create a file, put as many records as the number says, start each record with its number in order (starting from 0) and then put the text that user writes in console (it is put by getline into the "words" string).

Unfortunately I have a problem because for the record number 0 after colon it breaks a line and in fact it starts copying text from the next record.

Why does it happen? Could someone please explain it?

EDIT:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
using namespace std;


int main()
{       
short int counter_of_commands;
string cmd = "";
string name = "";
cin >> counter_of_commands >> cmd;

if (cmd == "create")
{
    string words = "";
    int records;
    cin >> name;
    cin >> records;
    fstream outFile;

    outFile.open(name, fstream::out);
    for (int i = 0; i < records; ++i)
    {
        if (i == 0)
            getline(cin, words);
        getline(cin, words);
        outFile << i << ": " << words << endl;
        words == "";
    }
    outFile.close();
}

return 0;
}

For input: 1

create example.txt 3

example 111 222

example1 333 444

example 2 444 555

I get:

0: example 111 222

1: example1 333 444

2: example 2 444 555

<- However it creates a fourth row (row #3) just as if after the end of line #2 it clicked enter one more time but didn't write anything. That's not how I would like it to work, it should create an exact number of rows as specified by a user.

  • 3
    I think you'll find the issue is that you didn't read the newline after reading the number, so your first `getline` is actually reading what is left of the line after the number. – Vaughn Cato Dec 25 '15 at 03:13
  • There's nothing particularly wrong with your code - see it run [here](http://coliru.stacked-crooked.com/a/7553d6d75b00c78d) and produce reasonable output. If you can adjust the input in that sample program to show what's breaking, we'd have something to explain. Or, copy and paste the full text from your console showing you typing the input, and the output file content. – Tony Delroy Dec 25 '15 at 05:36
  • @TonyD I added an if to omit that issue and edited the original post as well. – cooktheprogrammer Dec 25 '15 at 20:04
  • @cooktheprogrammer: I don't see how the `if` is intended to help. You'll need to use a `getline` or `get` to read the newline character after reading `records`. – Vaughn Cato Dec 25 '15 at 20:09
  • Or use `ignore()`. See this related question: http://stackoverflow.com/q/477408/951890 – Vaughn Cato Dec 25 '15 at 20:11
  • @VaughnCato It helped because the problem was that the getline took a blank mark and put it after 0: and only starting from the next line it was taking the input from me. This also resulted in missing the last line since there was one more line of text to be put. With the if at the start it takes that blank mark as well but then it takes the input from the user and puts it properly. In next rows there is only one getline and everything works except that blank mark in the additional, unpurposely made row. – cooktheprogrammer Dec 25 '15 at 20:13
  • @cooktheprogrammer: Got it -- was looking at the wrong `if`. – Vaughn Cato Dec 25 '15 at 20:15
  • 1
    Not sure what you mean by `it creates a fourth row`. You mean that there is an extra blank line at the end? You might be misinterpreting the results. Having the last character in the file be a newline is correct for a text file. – Vaughn Cato Dec 25 '15 at 20:20
  • @VaughnCato I edited, it creates a fourth row but named #3. I meant that when I open the file and click under "2: " my cursor goes there without a problem as if an enter was clicked after last letter in row #2. I check and it's possible to create a file with 3 rows, save it and then open, and when I try to click under "2: " nothing happens, the last place possible to go is the end of line #2. Is it impossible to write it in a way that doesn't create a newline as a last character? – cooktheprogrammer Dec 25 '15 at 20:23
  • 1
    @cooktheprogrammer: It is possible, just be sure to not use the `endl` for the last record. Note that this is non-standard though. See http://stackoverflow.com/q/729692/951890 – Vaughn Cato Dec 25 '15 at 20:30
  • @VaughnCato Ah of course, added another if to avoid it in the last row :D Thank you very much for help. – cooktheprogrammer Dec 25 '15 at 20:34

1 Answers1

0

Did you forget a ; here?

for (int i = 0; i < number; ++i)

instead of

for (int i = 0 i < number; ++i)

edit (after comment from OP):

Well, this works fine:

int number = 3;

string words = "";
fstream outFile;
outFile.open("c:/test/test1.txt", fstream::out);
for (int i = 0; i < number; ++i) {
    getline(cin, words);
    outFile << i << ": " << words << endl;
    words == "";
}

But as Vaughn Cato already pointed out, you're probably reading in a previous newline.

Danny_ds
  • 10,507
  • 1
  • 17
  • 42