I/O Streams; ios::in | ios::out

This is my code so far. how do i make it so the first user can type in "5 6 pick up sticks" then the user will exit the program by pressing return twice and then the second user will compile the program and will see what the first user had input and then enter "7 8 ain't c++ great!" and so forth. so the first person who runs the program should see some advice/words on the screen. the 2nd user will then see it and continue then close the program. the first user should then see the previous messages and so forth.

i was thinking of appending the file but everything i learned has to do with a file. i can get it into the file but i can't get the user to input words into the screen rather than the file. i was also thinking of using a function, but im not really sure.

i know i need a cin but im not so sure where to put it.

Does this make any sense? please ask me to clarify.





#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;

int main ()
{
cout << "Opening data.txt for appending\n";
ofstream fout;
fout.open("data.txt", ios::app);
if (fout.fail())
{
cout << "Input file opening failed.\n";
}

fout << "5 6 pick up sticks. \n"
<< "7 8 ain't c++ great!\n";

fout.close();
cout << "End of appending to file.\n";

system("PAUSE");
return 0;
}

Last edited on
Does all that need to happen in the same program or can you use one program to write the text and use a different one to read it?
it all needs to happen in the same program
Last edited on
You should think of the program as having two parts, as the specification states.

The first part should create the file and the content you expect it to have.

The second part should read the file.

If you put these pieces in seperate functions, then call them from main(), you my find it a bit easier.
Last edited on
I think I know how to do it :)

Make your code so that whatever the first user writes, will be written on a *.txt file, and so on and so on... so that the next user will read whatever the previous ones wrote. But the code I am thinking of will show you from the advice that the first one wrote till the last one before the one currently compiling this program!

example

First user wrote

1 2 Bla Bla Bla..

This will be written on a file.
The next user who compiles will get that message. When he writes
3 4 Bla Bla Bla

the next user to read will get the first and 2nd advice.. and so on until it's done :)
can someone please help me edit my code to give me an example because i am not sure exactly what it looks like. this is my code, but it doesn't seem to work..

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;

void input(ofstream outStream, ifstream inStream);

int main ()
{

input(outStream, inStream);

fout.open("data.txt", ios::app);
if (fout.fail())
{
cout << "Input file opening failed.\n";
}

/*
fout << "5 6 pick up sticks. \n"
<< "7 8 ain't c++ great!\n"
*/


fout.close();
cout << "End of appending to file.\n";

system("PAUSE");
return 0;
}

void input(ofstream outStream; ifstream inStream)
{
string firstUser, secUser;

cout << "Enter some advice";
cin >> firstUser;

}



Last edited on
Topic archived. No new replies allowed.