-2

I'm trying to enter the format of

"prog1 1 all file1"

the first input has to be a argv[1] should be an int.

So i need a way to determine if argv[1] is entered as a string "xxx" ( "prog1 xxx" ) it should return "NO PHRASE LENGTH" but its returning "INVALID PHRASE LENGTH".

I see there is a isdigit() function but im not sure how i would use that.

int main(int argc, char *argv[]){


try{
if(argc == 1){

   cout << "NO PHRASE LENGTH" << endl;  exit(1); 
}
else if((stoi(argv[1])) <= 0 ){ 

    cout << "INVALID PHRASE LENGTH" << endl;  exit(1); 
}
else if(argc == 2){

   cout << "NO MODE" << endl;  exit(1); 
}
else if(!(std::string(argv[2]) == "all") && !(std::string(argv[2]) == "top")){

    cout << "INVALID MODE" << endl;
}
else if(argc == 3){

    cout << "NO FILES GIVEN" << endl;
}
else if(argc >= 4){


    ifstream f;
    for(int i = 4; i < argc; i--){

        f.open( argv[i] );

        if( ! f.good() ) { 
            cout << "BAD FILE " << argv[i] << endl; exit(1); 
        }

        //cout << "OK" << endl;
        //f.close();

    }




}
else
    return 0;
}
catch(exception e){
}} 
Darko
  • 7
  • 2
  • According to your if structures "NO PHRASE LENGTH" is only written if you call your program without command line parameters ("prog1"). Calling a program without parameters sets `argc` to 1 and the code you provided will only print "NO PHRASE LENGTH" for that case. – AchimGuetlein Sep 28 '18 at 05:48
  • Still didn't answer my question. /// if(argc == 1){ cout << "NO PHRASE LENGTH" << endl;; } /// I need this if statement to also work when someone inputs a string rather than an integer. – Darko Sep 29 '18 at 06:18

2 Answers2

2

Your code is quite confusing. I am not sure what you were doing with the for loop to open the file. Anyway, I have given an example of what it could be.

int main(int argc, char *argv[])
{
    if(argc != 4)
    {
        cout << "Program requires 3 parameters!" << endl;
        return -1;
    }

    if(std::string(argv[2]) != "all" && std::string(argv[2]) != "top")
    {

        cout << "INVALID MODE" << endl;
        return -1;
    }

    try
    {
        if(stoi(argv[1]) < 1)
        {
            cout << "ZERO OR NEGATIVE PHRASE LENGTH" << endl;
            return -1;
        }

        ifstream f(argv[3]);
        if(!f) 
        { 
            cout << "BAD FILE " << argv[3] << endl;
            return -1; 
        }

        // Now do whatever you want with the opened file
    }
    catch(out_of_range e)
    {
        cout << "NON-INT PHRASE LENGTH" << endl; 
        return -1;
    }
    return 0;
} 
Sharath
  • 1,348
  • 2
  • 12
  • 25
0

Don't use std::endl when all you want to say is '\n' (or "...\n"). std::endl not only inserts a newline into the stream but also flushes it. If you *really* want to flush a stream be explicit and use std::flush.

Don't use exit() to end a program if there are other ways to do so. If you use exit() no stack unwinding will occur.

#include <cstdlib>
#include <string>
#include <iostream>
#include <exception>
#include <fstream>

int main(int argc, char **argv)
{
    if (argc == 1) {
        std::cerr << "NO PHRASE LENGTH\n";
        return EXIT_FAILURE;
    }

    if (argc == 2) {
        std::cerr << "NO MODE\n";
        return EXIT_FAILURE;
    }

    if (argc == 3) {
        std::cerr << "NO FILES GIVEN\n";
        return EXIT_FAILURE;
    }

    int phrase_length;
    try {
        phrase_length = std::stoi(argv[1]);
        if (phrase_length < 0)
            throw std::out_of_range{ nullptr };
    }
    catch (std::invalid_argument) {
        std::cerr << "NO PHRASE LENGTH";
        return EXIT_FAILURE;
    }
    catch (std::out_of_range) {
        std::cerr << "PHRASE LENGTH OUT OF RANGE\n";
        return EXIT_FAILURE;
    }

    std::string mode{ argv[2] };

    if ( mode != "all" && mode != "top") {
        std::cerr << "INVALID MODE\n";
        return EXIT_FAILURE;
    }

    for (int i = 3; i < argc; ++i) {
        std::fstream input_file{ argv[i] };
        if (!input_file.is_open()) {
            std::cerr << "BAD FILE \"" << argv[i] << "\"\n";
            return EXIT_FAILURE;
        }
    }
}
Swordfish
  • 1
  • 3
  • 17
  • 42
  • Can you explain that for loop at the end, what does it do? – Sharath Sep 28 '18 at 06:13
  • The for loop allows you to process more than just one file. You can call the programm with a list of files ("prog1 1 all file1 file2 file3") and it will process all files. – AchimGuetlein Sep 28 '18 at 06:16