View Full Version: problem on program

C++ Learning Community > C++ Help > problem on program


Title: problem on program


homeslice - January 29, 2007 12:50 AM (GMT)
My program compiles, but it doesnt get past the "overwrite?" query:

CODE

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

using namespace std;
int main()
{
   int num;
   char filename[16], cont, again;
   ifstream instream;
   ofstream outstream;
   
   cout << "This program allows the user to open a file and write "
        << "some positive integers on it, ending at -1. The program "
        << "will then echo back the input.";
   
   do
   {
        do
        {
                 cout << "Please enter in the name of a file\n";
                 cin >> filename;
       
                 outstream.open(filename);

                 if (outstream.fail() )
                 {
                 cout << "The file failed to open\n";
                 system ("pause");
                 exit (1);
                 }
       
                 cout << filename << " opened succesfully, overwrite? "
                      << "(enter \"Y\" if yes.\n";
                 cin >> cont;
                 
        }while (cont != 'Y' || cont != 'y');
       
        cout << "Enter a bunch of positive integers and when you "
             << "are done, enter a -1\n\n";
                     
        num = 1;
                 
        while (num != -1)
        {
              cout << "Enter a number\n";
              cin >> num;
                       
              if (num >= 0)
              {
                 outstream << num << "  ";
              }
              else if(num != -1)
              {
                   cout << num << "must be greater or equal "
                        << "to 0 ";
              }
        }
                 
        outstream << num;
                 
        outstream.close();
                 
        instream.open(filename);
                 
        cout << "I will now read the file you have modified :\n\n";
                 
        instream >> filename;
                 
        instream.close();
       
        cout << "Would you like to run this program again? enter "
             << "\"y\" if yes.";
        char again;
 
  }while (again == 'y' || again == 'Y');
 
  return 0;
 

charlie - January 29, 2007 08:31 PM (GMT)
You are missing the cin >> again again. Does that fix it?

If not, can you give sample input and its output. I don't know what you mean by "it doesnt get past the "overwrite?" query".

Shonoby - January 29, 2007 08:44 PM (GMT)
Well, he also forgot to put the ending brackets on main (' } '). Also i think what he mean was. After you input the file, and u try to overwrite it, it always says that it could not open the file. How are u trying to overwrite the code?




Hosted for free by InvisionFree