Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ help...

  • 19-04-2009 04:46PM
    #1
    Closed Accounts Posts: 636 ✭✭✭


    Can anybody tell me why the following code will compile but when I try to run it the console opens and just closes immediately!!! It works when I comment out #include m.h .
    #include <cstdlib>
    #include <iostream>
    #include "m.h"
    
    
    int main(int argc, char *argv[])
    {
        
        cout<<"HELLO"<<endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    
    //THIS IS m.h
    #include<vector>
    #include<string>
    #include<unistd.h>
    using namespace std;
    
    bool OPTION_LANGUAGE=false;     //trigger for choosing language
    bool OPTION_TEXT_FILE=false;    //trigger for inserting optional text file -T
    bool OPTION_ABR_FILE=false;     //trigger for inserting optional abreviation file -a
    bool OPTION_SHOW_TEXT=false;    //trigger for showing text file -f (file)
    bool OPTION_SHOW_HELP=false;    //trigger for help page -h
    bool PARAMS_GIVEN=false;        //trigger to say whether or not a command line argmument has been entered at all!
    bool OPTION_RESULT_FILE=false;  //trigger to indicated if an output file is required
    bool OPTION_PIPE_INPUT=false;   //trigger for piping input???
    int OPTION_SENT_LEN=0;          //trigger for sent length
    
    string TEXT_FILE=NULL;          //becomes name of optional text file where necessary
    string ABBREVIATION_FILE=NULL;  //becomes name of optional abbreviation file if needed;
    string LANGUAGE=NULL;           //becomes name of optional language
    string RESULT_FILE_NAME=NULL;   //becomes the name of the file to be written too if necessary
    
    
    
    void print_help_page(void);               //funtion to print help page
    int parse_main_args(int,char**);          //parser funtion
    bool member(int,vector<int>);             //funtion to check if an integer is contained in vector;
    
    
    
    
    int parse_main_args(int argc, char** argv){
        system("PAUSE");
        int result=0;
    
        int c;
    
    
        while((c=getopt(argc,argv,"PThva:d:f:l:s:r:"))!=-1){
        PARAMS_GIVEN=true;
        switch(c){
    
        case 'T':
             OPTION_SHOW_TEXT=true;
             cout<<"T"<<endl;
    
        break;
    
        case 'h':
             cout<<"H"<<endl;
             OPTION_SHOW_HELP=true;
             print_help_page();
             exit(1);
    
        break;
    
        case 'a':
             cout<<"A"<<endl;
             OPTION_ABR_FILE=true;
             ABBREVIATION_FILE=optarg;
             //cout<<tmp1<<endl;
    
        break;
    
        case 'f':
             cout<<"f"<<endl;
             OPTION_TEXT_FILE=true;
             TEXT_FILE=optarg;
             //cout<<tmp2<<endl;
        break;
    
        case 'v':
             cout<<"V"<<endl;
             //fprintf(my_stderr," Hi, This is the Program EOS, Version = %s, Filename = %s\n",VERSION,__FILE__);
    
        exit (1);
    
        break;
    
        case 's':
             cout<<"S"<<endl;
             OPTION_SENT_LEN = true;
             //sscanf(optarg,"%d",&sent_len);
        break;
    
    
    
        case 'l':
             cout<<"l"<<endl;
             OPTION_LANGUAGE = 1;
             LANGUAGE=optarg;
             // strncpy(LANGUAGE,optarg,STRSIZE);
    
        break;
    
    
    
    
    
    
    
        case 'P':
             cout<<"P"<<endl;
             OPTION_PIPE_INPUT = 1;
    
        break;
    
    
        case 'r':
             cout<<"R"<<endl;
             OPTION_RESULT_FILE = 1;
             RESULT_FILE_NAME=optarg;
             //strncpy(result_file_name,optarg,STRSIZE);
    
        break;
    
    
    
        case 'd':
             cout<<"D"<<endl;
             // sscanf(optarg,"%d",&Debug);
    
        break;
    
    
    
    
    
    
    }
    
    }
    
    
    
     /*if(!PARAMS_GIVEN){
    
     print_help_page();
    
     exit (1);
    
     }*/
    
    
    
    /* if (Debug >= 0)
    
    {
    
    
    
    if (OPTION_LANGUAGE)
    
    {
    
    fprintf(my_stderr,"Getopt: option_language is set, Langugae = %s\n",language);
    
    }
    
    
    
    if ( option_show_only_eos )
    
    {
    
    fprintf(my_stderr,"Getopt: option_only_eos_tag is set\n");
    
    }
    
    
    
    if (OPTION_ABR_FILE )
    
    {
    
    fprintf(my_stderr,"Getopt: Abk&#252;rzungsfile verwenden, Filename = %s\n",in_file_abk);
    
    }
    
    
    
    
    
    if ( OPTION_ABR_FILE )
    
    {
    
    fprintf(my_stderr,"Getopt: option_abk_file is set, Tag = %s\n",abk_file);
    
    }
    
    
    
    if ( OPTION_TEXT_FILE )
    
    {
    
    fprintf(my_stderr,"Getopt: Read Data From File Filename = %s\n",in_file_data);
    
    }
    
    
    
    if ( OPTION_SENT_LENGTH )
    
    {
    
    fprintf(my_stderr,"Getopt: Minimale Anzahl von W&#239;&#191;&#189;rtern in der Zeile = %d\n",sent_len);
    
    }
    
    
    
    
    
    fprintf(my_stderr,"Debug_Level %d\n",Debug);
    
    }
    
    
    
    /// if (Debug >= 1) { fprintf(my_stderr,"Wait 2 Sec.\n\n\n"); _sleep(2); }
    
    
    
    } */
    
           result=1;
    return result;
    }
    
    
    /*This function is just for printing help page whatever that should look like */
    void print_help_page(){
         cout<<"BANG"<<endl;
    }
    
    /*funtion I originally thought I would need but don't. Still no harm to have it anyway*/
    bool member(unsigned int i,vector<unsigned int> v){
         for(unsigned int j = 0; j < v.size();j++){
                      if(v.at(j)==i){
                       return true;
                       }
                       }
                       return false;
    }
    
    


Comments

  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    I'm guessing that it executes and exits too quickly for you to see. Include the <stdio.h> library and make use of the getch() function (use it in place of your system("pause") call) - it waits for the user to press a key. This will hold the program/console window long enough for you to see your HELLO message.

    Regards,
    RD


  • Closed Accounts Posts: 636 ✭✭✭NADA


    Thanks mate. I'll give that a shot in the morning!


  • Registered Users, Registered Users 2 Posts: 885 ✭✭✭clearz


    system("pause") should work fine at least on a windows machine.
    The line 'return result' seems to be in no mans land in the header m.h this might be causing your problem.
    Also cin.getch();can be used to pause and is part of the iostream header.


Advertisement