#Argc and Argv

22 messages · Page 1 of 1 (latest)

edgy mica
#

Can someone explain to me how to use argc and argv to make command line wait until the user will type something, and then do the code, call functions that I want to be called after the specific string like "-i" or "graph"?

Here's the code

int main(int argc, char* argv[]) {
bool printTree = false; // Flaga określająca, czy drzewo ma być wypisane
if (std::strcmp(argv[0], "-i") == 0) {
printTree = true;
return 0;
}

if (printTree) {
    Wezel* korzen = nullptr;

    for (int i = 1; i < argc; ++i) {
        if (std::isdigit(argv[i][0])) {
            int wartosc = std::stoi(argv[i]);
            wstaw(korzen, wartosc);
        }
    }

    std::cout << "Drzewo Red-Black:" << std::endl;
    inorder(korzen);
}

return 0;

}

gaunt gateBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

primal bough
#

i don't understand the question

#

argc and argv have nothing to do with "making the command line wait"

edgy mica
#

well, basically i have to wait until the user prompt "-i" and then my code has to be ran, but it can't be done with cin

#

it's about command line arguments

primal bough
#

command line arguments are defined when the user launches your program

#

before your program even begins to run

#

you don't need to wait for them

#

by the time main is called, they are all already there

edgy mica
#

good to know
so is there any way to make it work without using 'cin'?
should i define the command line argument as "-i" and then wait until user will type string equal to this?

primal bough
#

no, command line arguments are passed in argc and argv

#

not cin

#

you get an array of all the arguments you're given

#

argc is the number of arguments

#

argv is the array of argument values

#

argument count
argument value

edgy mica
#

understandable,

primal bough
#

do note that the first entry in argv is generally the name of the executable

#

the actual arguments start at index 1, not 0

edgy mica
#

got it

#

thanks for help