0

I just started working on on . I am having trouble creating an error when the user input is something that is NOT an integer in argv[1]. For example ./caesar hello. Here is what I currently have below:

int main (int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Error. Please re-enter your arguments:\n");
        return 1;
    }

    int k = atoi(argv[1])
    if (int k >= 0) 
    {
        printf("What would you like to encode?: ");
        string s = GetString();
        //other code
    }
    else
    {
        printf("Error. Please re-enter your arguments:\n");
        return 1;
    }
}

When I execute something such as ./caesar hello, my program is still running as if I had passed an integer instead of hello.

Please give any advice on why the code still runs when I am passing a string? Is this due to where I am putting my atoi code?

Also, I have a couple of other questions to help me understand this further.

  1. Can I put chars and other data types in int? Am I just converting hello in ./caesar hello to an integer? I know that a char can hold numbers such as 1,2,3 or symbols, but is the reverse true and can integers also hold other data types?

  2. Is there any way to compare a variable with a data type. For example, is there anyway to write

    if (argv != an interger type of data)
        execute code
    
1

Is this due to where I am putting my atoi code?

actually it's because you didn't read atoi's manual page. carefully, at least. the man page explicitly states that

atoi does not detect errors

which means that it returns some integer value normally even if an error occurs (e.g., the passed string does not start with a number). this returned integer value makes it possible to pass your condition

if (k >= 0)

so your program proceeds normally.

as a side note: you shouldn't re-mention the data type when you use an already defined variable.

Can I put chars and other data types in int? Am I just converting hello in ./caesar hello to an integer? I know that a char can hold numbers such as 1,2,3 or symbols, but is the reverse true and can integers also hold other data types?

you could try that and see what happens and I encourage you to do that before reading the rest of the answer.

it's not always the case that you can store a value of some data type into a variable of some other data type. there are data types that are compatible with each others, but there are others that are not.

for example, a char is typically just an 8-bit int. we say a char variable is capable of storing a character and that is true, but what is a character anyway for a computer? it's just a number as explained in the first lectures. it's not true though that you can store any number in a char.

on the other hand, string is a more complicated data type. it typically consists of multiple values of a simpler data type, namely char.

Is there any way to compare a variable with a data type. For example, is there anyway to write

I'm afraid there is no (easy?) way to get the type of a variable in terms of that variable's name in standard C.

You must log in to answer this question.

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