Thread: Problem Running Program After Compiling

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Problem Running Program After Compiling

    I have written a program and it compiles successfully (or at least I am not getting any errors), but it will not run. I am sure I am missing whatever it is that I did wrong. Can someone take a look at this and please tell me what is wrong and why it's wrong, so I can avoid this in the future?
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <cstdlib>     
    #include <fstream>  
    #include <cstring>  
    
    
    using namespace std;
    
    int openFile(fstream& f, fstream& f1, fstream& f2);//function to open the files
    void readWriteData(fstream& f, fstream& f1, fstream& f2);//function to read and write data
    
    
    int main()
    {
        fstream outFile; //output
        fstream inFile1;//input
        fstream inFile2;//input
        
        openFile(outFile, inFile1, inFile2);
        cin.get();
        return 0;    
    }
    
    
    int openFile(fstream& outFile, fstream& inFile1, fstream& inFile2)
    {
        char outName[50];//storage of user input filename for the data file that is to be written to
        char inName1[50];//storage of user input filename for the first data file that is to be read from
        char inName2[50];//storage of user input filename for the second data file that is to be read from
        
        cout << "Please type the name of the first input file, including the extension: ";//requests user input of filename
        cin >> inName1;
        cout <<'\n';
        inFile1.open(inName1, ios::in);   
        if (inFile1.fail())//if file cannot be opened, a message will display
            {
                cerr << "Could not open " << inName1 << "!" << endl;
                return EXIT_FAILURE;
            }
        
        cout << "Please type the name of the second input file, including the extension: ";//requests user input of filename
        cin >> inName2;
        cout <<'\n';
        inFile2.open(inName2, ios::in);
        if (inFile2.fail())//if file cannot be opened, a message will display
            {
                cerr << "Could not open " << inName2 << "!" << endl;
                return EXIT_FAILURE;
            }    
            
        cout << "Please type the name of the output file, including the extension: ";//requests user input of filename
        cin >> outName;
        cout <<'\n';
        outFile.open(outName, ios::out);
        if (outFile.fail())//if file cannot be opened, a message will display
            {
                cerr << "Could not open " << outName << "!" << endl;
                return EXIT_FAILURE;
            }
       
        readWriteData(outFile, inFile1, inFile2);
        return 0;
        
    }
    void readWriteData(fstream& outFile, fstream& inFile1, fstream& inFile2)
    {
         string in1;
         string in2;
         string out;
    
         
         while(!inFile1.eof() && !inFile2.eof())
          {
               inFile1 >> in1;
               inFile2 >> in2;
               
                 if (strlen(in1.c_str())  < strlen(in2.c_str()))
                 {
                    in1 = out;
                    outFile << in1;
                    inFile1 >> in1;
                  }
                  
                  else
                  {
                      in2 = out;
                      outFile << in2;
                       inFile2 >> in2;
                  }
                  if (!inFile1.eof() && !inFile2.eof())
                  {
                     do
                     {
                               inFile1 >> in1;
                                inFile2 >> in2; 
                               outFile << in1;
                               outFile  << in2;
                      }
                      while (!inFile1.eof() && !inFile2.eof());
                  }
    
           }
           inFile1.close();
           inFile2.close();
           outFile.close();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It compiles without errors or warnings on the MinGW port of g++ 3.4.2, and it also appears to run (I did not check beyond the first prompt for input). How exactly does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I see a lot of dubious use of eof()
    See the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Quote Originally Posted by laserlight View Post
    It compiles without errors or warnings on the MinGW port of g++ 3.4.2, and it also appears to run (I did not check beyond the first prompt for input). How exactly does it not work?
    The program is supposed to prompt the user to name two different input files, and one output file, read from both input files to merge the data and write it to the output file. When I try to run it, nothing happens. the program does not open as far as I can tell. The command window does not even open up for a second.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Strange. It runs fine (except I'm not sure if it merges the input files properly because of some rather bizarre logic).

    Don't expect the window to stay open, though, because you are using cin >> and cin.get() without cin.ignore()
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Quote Originally Posted by anon View Post
    Strange. It runs fine (except I'm not sure if it merges the input files properly because of some rather bizarre logic).

    Don't expect the window to stay open, though, because you are using cin >> and cin.get() without cin.ignore()
    I probably did it all wrong, but here is the way it's supposed to work. The program reads input file 1 and then reads input file 2. Then it is supposed to use strlen to compare and see which one is larger, if file 1 is smaller htan file 2, then it is supposed to write from file 1 to the output line and read a new line from file 1. Otherwise, it writes the line from file 2 to the output file and reads a new line from file 2. The program is supposed to do this while the end of neither file 1 of file 2 has been reached. Then if there is any line in file 1 or file 2 that still has not been written to the output file, the program should write those lines.

    Do you think the problem could be with my compiler? I am using Dev-C++ Version 4.9.9.2.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Do you think the problem could be with my compiler? I am using Dev-C++ Version 4.9.9.2.
    You might check to make sure you don't already have a window open from a previous run. Dev-C++ won't open a new command window if the previous one is still running.

    If this isn't the case, try exiting Dev-C++ and restarting it.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kristentx View Post
    Then it is supposed to use strlen to compare and see which one is larger, if file 1 is smaller htan file 2, then it is supposed to write from file 1 to the output line and read a new line from file 1.
    So you want the longer of the strings, as opposed to the shorter, or do you want to actually compare the value of the string. So, for example AAAAA is larger than ZZZZ.

    It seems a bit unnecessary to go through strlen() when there is string::size() that does the same thing (but more efficiently, since strlen counts each character, rather than just returning the number of characters within the string that the string class already knows).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Quote Originally Posted by swoopy View Post
    >Do you think the problem could be with my compiler? I am using Dev-C++ Version 4.9.9.2.
    You might check to make sure you don't already have a window open from a previous run. Dev-C++ won't open a new command window if the previous one is still running.

    If this isn't the case, try exiting Dev-C++ and restarting it.
    I have tried shutting down Dev-C++ several times; it's still not working. I am going to try running again in the morning, after I shut down tonight, to see if that helps. I just wanted to make sure that there wasn't any problem with the code that prevented the program from running. If it still doesn't work in the morning, I'll be back begging for help.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the window doesn't stay open at all it is hard to tell what the problem might be. If the program is already running, it would compile it successfully but fail to link.

    Otherwise you might also post your input. For example, do the file names you want to input contain spaces?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can always open a console and run the application in there, to see what it outputs.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    I don't even get the chance to put in input. When I compile and run, nothing happens. I have tried double clicking on the exe file, but still nothing happens. I am going to try and install a different compiler, and see what happens.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be you could post the compile log.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Some nits:


    Code:
    int openFile(fstream& outFile, fstream& inFile1, fstream& inFile2)
    There appears to be no reason to pass these variables into the function, unless you plan on removing the call to readWriteData and placing it after the call to openFile in the main function later on. It's not hurting anything... but there is no real reason for it as your program currently stands. Looks like these variables can be made local to the function.



    Code:
    char outName[50];//storage of user input filename for the data file that is to be written to
    char inName1[50];//storage of user input filename for the first data file that is to be read from
    char inName2[50];//storage of user input filename for the second data file that is to be read from
        
    cout << "Please type the name of the first input file, including the extension: ";//requests user input of filename
    cin >> inName1;
    cout <<'\n';
    inFile1.open(inName1, ios::in);
    You should prefer the use of strings here versus character arrays, you won't have to worry about potentially storing more than the array is capable of holding:
    Code:
    string outName;//storage of user input filename for the data file that is to be written to
    string inName1;//storage of user input filename for the first data file that is to be read from
    string inName2;//storage of user input filename for the second data file that is to be read from
        
    cout << "Please type the name of the first input file, including the extension: ";//requests user input of filename
    cin >> inName1;
    cout <<'\n';
    inFile1.open(inName1.c_str(), ios::in);



    Code:
    void readWriteData(fstream& outFile, fstream& inFile1, fstream& inFile2)
    {
         string in1;
         string in2;
         string out;
    
         
         while(!inFile1.eof() && !inFile2.eof())
          {
               inFile1 >> in1;
               inFile2 >> in2;
               
                 if (strlen(in1.c_str())  < strlen(in2.c_str()))
                 {
                    in1 = out;
                    outFile << in1;
                    inFile1 >> in1;
                  }
                  
                  else
                  {
                      in2 = out;
    Where is out ever initialized?



    Code:
    if (strlen(in1.c_str())  < strlen(in2.c_str()))
    The string container provides a handy length member function to replace this.
    Code:
    if ( in1.length()  < in2.length() )
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Problem Compiling Program
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 11-06-2007, 12:56 PM
  3. Problem running program
    By JOCAAN in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2007, 04:09 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM