C++ Write line to text document in original order

Hello,

I'm beginner, just trying to write words and phrases into text document, on each cycle one line under another like this.

1
2
3
4
word1 
word2 
word3 
etc.


it is works with numbers but if I'm use char I got separated characters like this:

1
2
3
4
5
w
o
r
d
1


Can you show me some example to figure out with this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


using namespace std;

std::ofstream outfile("doc.txt");

int main()
{

   std::string a;

    for (int i = 0; i < 10; i++)
    {
        cout << "Write word: " << endl;
        cin >> a;

        cout << "ID: " << i << a << endl;

        outfile << a << std::endl;

    }


    _getch();
    return 0;
}
Last edited on
Hi,
This :
int a;

Should be :

1
2
3
4
5
#include<string>

//...

std::string a;
Does that help you? :)
Why do you use srand(time(NULL));? This line is redundant.
Hello, thank you, I've edited above with your help, now works. what about time, I'm just wondering how to make time interval for each cycle
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/ctime/clock/
Topic archived. No new replies allowed.