Menu

program closes even with system pauses

2002-11-25
2012-09-26
  • Nobody/Anonymous

    there are no errors or warnings but every time i try to get to the read() function the program closes.  the other functions used to close the entire program too, but that was after they completed.  i fixed those by adding main(); at the end (which i heard was very bad to do).  the read() function still closes the entire program before running.

    here is my code:

    /* headers */
    #include <iostream>                                   
    #include <string>                                   
    #include <fstream>
    #include <apvector.h>   
    #include <conio.c>                                   
    /* End of headers */

    int debug(int error);
    int instr();
    int read();
    int remove();
    int add();
    int main();

    /* Declaration of namespace */
    using namespace std;                                   
    /* End of decaration of namespace */

    /* Error Vars */
    int error1, error2, error3;
    /* End of Error Vars */

    /* Functions */

    int read()
    {
        ifstream fin;                                   
           
        apvector<char> vectors(100);
                                                                               
        string line="";                                                       
        int v;   
        fin.open("Database.txt", ios::in);   
       
        if(fin)
        {                                                                                
                                   
        for(v=1;v<=100;v++)
            {
            fin>>vectors[v];
            }
       
        for(v=1;v<=100;v++)
        {                                       
        cout<<vectors[v]<<endl;
        v=v++;
        cout<<vectors[v]<<endl;
        v=v++;
        cout<<vectors[v]<<endl;
        cout<<endl;
        }                   
                                       
        fin.close();
        }
        else
        {
        error1=13100;
        debug(error1);
        }
       
        system("pause");
        clrscr();
        main();
    }

    int main()                                               
    {       
    int menuoption;                                               
    cout<<"Database v1.0.1"<<endl;
    cout<<"---------------"<<endl;
    cout<<endl;
    if(error2!=10064)
    {
    cout<<" [1] Add to database"<<endl;
    }
    cout<<" [2] Remove files from database"<<endl;
    if(error1!=13100)
    {
    cout<<" [3] Read files from database"<<endl;
    }
    cout<<" [4] Instructions"<<endl;
    cout<<" [0] Leave"<<endl;

    cin>>menuoption;

    switch(menuoption)
    {
    case 0:
    return 0;
    break;
    case 1:
    add();
    break;
    case 2:
    remove();
    break;
    case 3:
    read();
    break;
    case 4:
    instr();
    break;
    default:
    error3=12002;
    debug(error3);
    break;
    }
    }

    int debug(int error)
    {
    switch(error)
    {
    case 12002:
    cout<<"Error 12002:  Unidentified User Input"<<endl;
    cout<<"Restarting Program"<<endl;
    system("pause");
    clrscr();
    break;
    case 13100:
    cout<<"sjlfdk"<<endl;
    system("pause");
    clrscr();
    break;
    case 10064:
    cout<<"sldfkj"<<endl;
    system("pause");
    clrscr();
    break;
    default:
    cout<<"Unidentified Error: Please contact your local distributor for assistance."<<endl;
    system("pause");
    return 0;
    break;
    }
    main();
    }

    int instr()
    {
    // instructions here
    system("pause");
    clrscr();
    main();
    }

    int remove()
    {
    // remove files
    system("pause");
    clrscr();
    main();
    }

    int add()
    {
        ofstream fout;                                   
                                                       
        char status[201] ="";                               
        char age[201] = "";                               
        char name[201] = "";                                                       
           
        cout<<"Name: ";
        cin.getline(name, 200);
        cout<<"Age: ";
        cin.getline(age, 200);
        cout<<"Status: ";
        cin.getline(status, 200);   
           
        if(fout)
        {                                                                                
        fout.open("Database.txt", ios::app);                               
                                                   
        fout<<name<<endl;
        fout<<age<<endl;
        fout<<status<<endl;
                                                           
        fout.close();
        }
        else
        {
        error2=10064;
        debug(error2);
        }
        system("pause");
        clrscr();
        main();
    }

                   
    thx

     
    • Nobody/Anonymous

      Since I don't have apvector, I am a little handicapped debugging, but a first observation, you might want to look into the exit function, rather than your calling main() all the time.

      My brain is a little fried, you wrapped stuff around main making it pretty hard to read, I can't even tell if main returns 0 as promised.  Will look later, or after one of the other guys fixes it.

      Wayne

       
    • Nobody/Anonymous

      This program is different that the other one with the same name.  I thought it was a double post, but I am wrong.  The answer to this post is the main(); at the end making this a recursive program in a infinite loop.  As for the other post... well I also need the apvector.h file.  If you are using pointers in loops, I have found that they will crash the system if you loop it forever.  This will cause the program to exit where it is and not finish the code to the system("PAUSE");

      Curtis

       
    • Nobody/Anonymous

      OK, I cheated a little.  I commented out your apvector include, then all the code that used it.  I ran the code with no appropriate file present, and it printed the error you expected.

      I ran cygwin's indent on the code by the way.  It looks like your main function is never returning the promised integer.

      I dooubt any of this really helps, but I have a run-on complex...

      Wayne

       
    • Nobody/Anonymous

      So if i can set a limit to the loop with pointers it will not crash?
      instead of:

      for(v=1;v<=100;v++)
      {
      cout<<vectors[v]<<endl;
      v=v++;
      cout<<vectors[v]<<endl;
      v=v++;
      cout<<vectors[v]<<endl;
      cout<<endl;
      }

      instead of above, have it like this?:

      cout<<vectors[v]<<endl;
      v=v++;
      cout<<vectors[v]<<endl;
      v=v++;
      cout<<vectors[v]<<endl;
      cout<<endl;

      ... then when i need to add more to the database i can just restart the function thru the main function??? (i hope this makes sense)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.