-1

I need to keep prompting the user until they give me 1 argument. I have a while(1) loop with a break statement inside but it exits early when it is run with 2 arguments. How do I continue prompting instead of exiting? I want to do something like this:

bool flag = true; 
while(flag)
{
 // ..code
 flag = false;
}

but that does not work in C. This is what I have so far.

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

    while(1)
    {
        if(argc == 2)
        {
            reverse(argv[1]);
            printf("Reversed string: %s\n", argv[1]);
        }
        else
        {
            printf("Error, wrong number of arguments. Please provide only one argument");

        }

        break;

    }

    return 0;
}

I tried deleting the break statement and got an infinite loop, as expected, but no "pause" of execution for the input argument.

  • 1
    Which line do you think gets the input? – Prabhu Apr 08 '16 at 21:25
  • If you want to get user input and not command line arguments then there are plenty of solutions described in several posts here, see for example: http://stackoverflow.com/questions/9278226/which-is-the-best-way-to-get-input-from-user-in-c. If you really mean command line args the read the answer from @Transcendental – terence hill Apr 08 '16 at 21:33

1 Answers1

3

You cannot do that. Command line arguments are passed to a program when it is started. You can't ask for more arguments just like that. The best you can do is notify the user to run the program with one argument only. I recommend simply ignoring the second argument if it is not needed, but still notifying the user that it was not needed.

Transcendental
  • 983
  • 2
  • 11
  • 27