I need to fallow these directions for homework but I'm stuck on some things:

// open output filestream for appending
    // get valid file name
    ofstream fout( getOutFileName().c_str() );
    
    
    // if the file opened successfully
    if ( fout.good() )
    {
         cout << " The file was succesfully opened." <<endl;
         
      // repeatedly do the process
Q: What does he mean by repeatedly do the process? And how do I code that in?
            // accept data pairs
 Q: And this?           
            // and send each pair to be appended to the output stream
 Q: And this?   
        // until user is finished, then
Q: And this?
        // close the output file.
        fout.close();
    }  
    
    else // otherwise
    {    // display error message
         cout << "The file did not open." 
              << endl;
        
    }

>What does he mean by repeatedly do the process?
Do the same thing again, and again, and again...

>And how do I code that in?
You would use a while() loop, which keeps going until the user decides to quit.

>accept data pairs
You read in 2 numbers from cin. Implement it any way you choose; you could have the user enter both numbers at the same input prompt, or you could ask for each number separately.

>and send each pair to be appended to the output stream
In this case, your "output stream" is your fout. One thing to be careful of though: you need to open fout with append mode (ios::app) turned on. See this.

>until user is finished, then
Check to see if the user has entered 'q' or some other character of your choosing. If it's found in the input, break out of your loop.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

string getFileName();
void appendData( ofstream&, int, int );

int main(int argc, char *argv[])
{
    ofstream fout;
    // declare other variables
    
    int fun1 = 2;
    int fun2 = 45;
    
    
    
    // open output filestream for appending
    // get valid file name
    ofstream fout( getOutFileName().c_str() );
    
    
    // if the file opened successfully
    if ( fout.good() )
    {
         cout << " The was succesfully opened." <<endl;
         
         while ( !good )
         { // repeatedly do the process
            cout << "Enter the first number." << endl;
            cin >> firstNum;
            cout << "Entet the second number." << endl;
            cin >> secondNum;      // accept data pairs
            
            // and send each pair to be appended to the output stream
            writeData ( fout, firstNum );
            writeData ( fout, secondNum );
            cout >> "Are you satisfied? Y or N? " << endl;
            cin << Y;
                      if ('Y' == Y || 'y' == Y  )// until user is finished, then
                      {
                          break;
                      }
         }
           
        // close the output file.
        fout.close();
    }  
    
    else // otherwise
    {    // display error message
         cout << "The file did not open." 
              << endl;
        
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

// define getFileName();
string getFlieName()
{
       string aName = "";
       ifstream handle;
       bool valid = false;


// test for existence by opening for input
// reject if opening fails
// and keep trying
// accept only if open succeeds
}
// define appendData()

Now as you can see it wont compile. I get this error:
redeclaration of `std:: ofstream fout'
Why? I'm not declaring that twice?

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

string getFileName();
void appendData( ofstream&, int, int );

int main(int argc, char *argv[])
{
    ofstream fout;
    // declare other variables
    
    int fun1 = 2;
    int fun2 = 45;
    
    
    
    // open output filestream for appending
    // get valid file name
    ofstream fout( getOutFileName().c_str() );

Now you know why...

Oh ok thanks. That one sliped by me.
Now I need to add an expression for the while loop. Cause !good does not work. So how can I write an expression that loops for the file to open? I dont really understand that. Why do I need a loop to open a file? Should it not be open good not open spit out an error? Why a loop?

while ( fout.getOpenFile ) ???

That does not sound like it could work.

How about something like this:

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

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while (myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

Ok I got this far, but now I'm getting an error saying brek not in a loop. How do I fix that?

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

string getOutFileName();
void appendData( ofstream&, int, int );

int main(int argc, char *argv[])
{
    
    // declare other variables
    
    int fun1 = 2;
    int fun2 = 45;
    double firstNum;
    double secondNum;
    string respons;
    
    
    // open output filestream for appending
    // get valid file name
    fstream fout( getOutFileName().c_str() );
    
    
    // if the file opened successfully
    while ( true )
    {
         cout << " The was succesfully opened." <<endl;
         
         // and send each pair to be appended to the output stream
         void appendData ( double firstNum );
         void appendData ( double secondNum );
         cout << "Are you satisfied? Y or N? " << endl;
         bool good = true;
         cin >> respons;
             if (respons == "Y" || respons == "Y" ||
                 respons == "N" || respons == "n"  )// until user is finished,
             {                                      
                  good = true;
             }
             else
             {
                 cout << "Invalid respons." << endl;
             }  
     }
     
             if ( respons == "Y" || respons == "y" ) // otherwise
             {    // display error message

                  break;
             }
       
               // close the output file.
        fout.close();
              
    system("PAUSE");
    return EXIT_SUCCESS;
}

// define getFileName();
string getFlieName()
{
       string aName = "";
       ifstream handle;
       bool valid = false;
       double firstNum;
       double secondNum;
       
       
               
         while ( !valid )
         { // repeatedly do the process
            cout << "Enter the first number." << endl;
            cin >> firstNum;
            cout << "Entet the second number." << endl;
            cin >> secondNum;      // accept data pairs
            

         }

// test for existence by opening for input
// reject if opening fails
// and keep trying
// accept only if open succeeds
}
// define appendData()

>Ok I got this far, but now I'm getting an error saying brek not in a loop.
Um, maybe because there's nothing to break out of?

>How do I fix that?
Put it inside the loop?

Well if I put it in the loop, by moving the } right before the
// close file output.

I get this error:
[Linker error] undefined reference to `getOutFileName()'
ld returned 1 exit status
[Build Error] [extendPolar.exe] Error 1

Is this how you would it?

Your prototype doesn't match the implementation.

string getOutFileName();

// blah blah

// define getFileName();
string getFlieName()
{
       string aName = "";
       ifstream handle;
       bool valid = false;
       double firstNum;
       double secondNum;

Even if I change to match:

string getFlieName()

or
string getOutFlieName().

I get the same error.

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