Missing File Error

Hey everyone!

My file will compile and run for this assignment, but for some reason it will not locate the file that needs to be appended to. I know this is probably something very simple that I have missed but I can not figure it out and it is due tonight. Can someone please help me?

Here is what needs to be done:

Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice to the screen and asking the user to type in a different piece of advice. The program then ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file, and the contents of the file change after each run of the program. You can use your editor to enter the initial piece of advice in the file so that the first person who runs the program receives some advice. Allow the user to type in advice of any length so that it can be any number of lines long. The user is told to end his or her advice by pressing the Return Key Twice. Your program can then test to see that it has reached the end of the input by checking to see when it reads two consecutive occurrences of the character.

Here is what I have:

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
#include <iostream>
#include <fstream>
using namespace std;
int main(){  
  string input;
  fstream inout;  
   inout.open("input.txt", ios::in);         
     if(inout.fail())         
       { cout<<"file did not open for input please check it\n";
        system("pause");
        return 0;
        }
   getline(inout,input);
   while(inout)
      {cout<<input<<endl;
      getline(inout,input);
      }
   inout.close();
   inout.clear();
   inout.open("input.txt", ios::out | ios::app);         
     if(inout.fail())         
       { cout<<"file did not open for append please check it\n";
        system("pause");
        return 0;
        }    
   cout<<"Please add any advice you have and press enter twice when completed\n";         
  getline(cin,input);                   
  while(input.length()>0)       
     {
     inout<<input<< endl;
     getline(cin, input);
     }
inout.close();
system("pause");
return 0;

}


Again, my program will compile and it will run, but it only gives me this message: file did not open for input please check it.

What am I missing for the program to run based on the requirements? Any help is greatly appreciated!!!!!

what compiler are you using?
I wouldn't expect this to compile because you didn't use #include <string>

judging from your problem description you only need one file. You need to open the file for input, input the advice, close the file, reopen the file for output, output the new advice, then close the file.
Last edited on
I'm using VS2008 and pasting your code 'as is' failed to compile correctly. After adding a #include <string> it compiles, but then got caught up in the same place that you pointed out.

First of all are there any restrictions on what functionality and streams you have to use? I've always been a fan of using file handles. But since it's almost due I'll be sticking to your implementation.

Not necessarily looking into too much detail of the end implementation providing the desired output, I looked through this and found that with your logic and such it should work.

After a while I realized the issue might be that your program tried to open a file for reading that didn't already exist, and that the code you have only accounts for a case in which your original file from the start does exist. After adding a file input.txt into the project and some simple text like Hi this is a test. and running the code, it works. I then tested it further with a response to Please add any advice you have and press enter twice when completed being Don't forget to add an input file next time!!! :) .

Your current code will output to the text file something like
Hi this is a test.Add an input file next time!!! :) in its current state (and my input), but that can be easily remedied by adding a newline character \n somewhere in there!

Hope I wasn't too late, and that you don't run into additional problems.

Cheers!
Last edited on
Topic archived. No new replies allowed.