I'm cryptically told nothing more then "your code is almost completely wrong"
this is supposed to be a program that reads from an input file (infile.dat) one line of "programming advice" to a user. Then the user enters their own advice for the next person who runs the program to see. When you're done entering the advice supposedly you press enter twice to end the program

Not looking for someone to just give me code...just looking to understand what I'm doing so wrong -_-


#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main (void)
	string first, words;

	ifstream in_stream;
	ofstream out_stream;


	if (in_stream.fail())
		cout << "Input file opening failed" << endl;

	if (out_stream.fail())
		cout << "Output file opening failed" << endl;

	cout << "Press enter to see some advice to help you with programming" << endl;

	getline (cin, first);

	cout << first << endl;
	cout << "Now enter some advice for the next person to see this" << endl;

          getline (cin, words); 
          out_stream.put (words); 
    while (words != '/n');

	system ("pause");
	return 0;

you can't input and output to the file at the same time without some very fancy footwork by repositioning the file pointer to the beginning of the file before the write. The way I understand what you want, the file only contains one line of text, which you want to change each time you run the program. The simplest way to do that

open input file
read the line of text
close input file
open output file, with ios::truncate flag so that the current line is erased.
write new line of text to the file
close output file

Notice that it is not necessary to have both input and output files opened at the same time.

Hope this helps :)

This article has been dead for over six months. Start a new discussion instead.