0

I have started working on Caesar and I am stuck with how to repromt user for a command line argument incase they input some wrong values.

Here is the code i wrote and I can't find any apparent errors

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>

int main (int argc, string argv[])
{
    do
    {
        printf("Usage: ./caesar key");
        return 1;
    }
   while (argc != 2 || !isdigit(argv[1]));
    
}

I am getting the printf message even when the input values are correct. What exactly is going wrong here?

5
  • 1
    So what your looping is doing, is printing "Usage: bla bla" when there's more or less than 2 arguments AND the first digit of the second argument is not a number. If the user input ./caesar 2a -> you have two arguments and the FIRST digit of Argv[1] is a number. Im guessing that this is your mistake – leonardo antunes Oct 5 '20 at 15:46
  • I have 2 queries isn't it saying OR as i am using || and not &&? Also isn't Argv[1] the entire string? so in your example 2a would be the string right? and isdigit would output false as it is not a digit? Am I making some mistake here @leonardoantunes – FoundABetterName Oct 5 '20 at 15:54
  • 1
    The first one, i confused the signs. About the second one perhapes this help you -> [ geeksforgeeks.org/isalpha-isdigit-functions-c-example ] – leonardo antunes Oct 6 '20 at 16:35
  • thanks :) @leonardoantunes – FoundABetterName Oct 6 '20 at 18:29
  • 1
    you're welcome, good luck with your studies !!! – leonardo antunes Oct 7 '20 at 0:45
1

Perhaps a review of the class material on command line arguments is in order?

https://cs50.harvard.edu/x/2020/notes/2/#command-line-arguments

1

You can't reprompt a user for command line arguments. You will have to exit the program after printing the usage.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .