-4

I have a problem with my code. I would like to know what the "variation" value is. But it always gives me extremely high or negative values. So when I type in terminal for example ./NAME 3 I end up with another number. How can i fix it?

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


int main(int argc, string argv[])
{
    int variation =  (int)argv[1];
    if (argc == 2)
    {

        printf("%i\n", variation);

        return 0;
    }

    else
    {
        return 1;
    }

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    you need a string to number converter and c does not have any type named `string` – reyad Aug 08 '20 at 06:53
  • https://cs50.stackexchange.com/ – M.M Aug 08 '20 at 07:12
  • For better or worse, the `cs50.h` header provides `typedef char *string;` — and people taking the CS50 course can't really be expected to understand the niceties of "C does not have a type `string`" since the C they're encouraged to work with does have the type `string`. – Jonathan Leffler Aug 10 '20 at 19:22
  • for heaven's sake, there is no `%i` , no type '`string`'... why does CS50, such a reputed course, insult C like this ? – A P Jo Aug 11 '20 at 10:29

1 Answers1

0

[Note: C does not have builtin string type. What C has is character array]

This is a very easy to solve problem. Here's the code given, it uses stdlib's builtin atoi function to convert string to int:

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

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

    if (argc == 2) {
    
        char *var_str = argv[1];
        
        int variation = atoi(var_str);

        printf("variation: %d\n", variation);
    
    }
    else if (argc < 2) {
    
        printf("no agrument provided.\n");

    } else {

        printf("only one argument was expected. %d provided.\n", argc);
    }
    
    return 0;
}

Update (A more secure version using strtol):

As strtol is a better function to convert string to long int, than atoi, because of it's error detecting utility, it's better to use strtol in this regard. This practice is heavily enforced by user @klutt. I would like to thank her/him for advising me to include the code using strtol too. It is given below:

// a more secure version

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

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

    if (argc == 2) {
        char *var_str = argv[1];

        char *text = NULL;
        int base = 10; // default base

        errno = 0; // intializing errno

        long int variation = 0;
        variation = strtol(var_str, &text, base);

        // now on to error detection
        // full error trace
        if (var_str == text) {
            printf ("error: no number found\n");

        } else if (errno == ERANGE && variation == LONG_MIN) {
            printf ("error: underflow\n");

        } else if (errno == ERANGE && variation == LONG_MAX) {
            printf ("error: overflow\n");

        } else if (errno != 0 && variation == 0) {
            printf ("error: unknown error\n");

        } else if (errno == 0 && var_str && !*text) {
            printf ("variation: %ld\n", variation);

        } else if (errno == 0 && var_str && *text != 0) {
            printf ("variation: %d, but some text included: %s\n", variation, text);
        }

    } else if (argc < 2) {
    
        printf("no agrument provided.\n");

    } else {

        printf("only one argument was expected. %d provided.\n", argc);
    }
    
    return 0;
}

If you have any questions, ask me in comment...

reyad
  • 1,392
  • 2
  • 7
  • 12
  • Thanks man. This will function as long as variation is not 0, right? I say for atoi() – Daniel velin Aug 08 '20 at 07:05
  • 1
    Advocate `strtol` instead. `atoi` cannot be error checked. – klutt Aug 08 '20 at 07:05
  • @Danielvelin, it'll function as long as `variation` is `integer`, i.e. when `variation` is `negative`, it'll function, when `variation` is `0`, it'll function, when `variation` is `positive`, it'll function... – reyad Aug 08 '20 at 07:09
  • @reyad.Thanks again – Daniel velin Aug 08 '20 at 07:11
  • `var_str` is redundant – M.M Aug 08 '20 at 07:12
  • @klutt, i guess, OP is passing only one parameter `./Name 3`. So, it won't be a problem in this case, if it has a restricted usecase...but otherwise, you're right `strtol` better to use... – reyad Aug 08 '20 at 07:13
  • 1
    @reyad Safe functions are always preferable to non-safe unless you can guarantee that the input is safe, so `strtol` would be the default function to use. Especially in a case like this where it's about user input, which is unreliable by definition. What if the user passes `./Name hello` or `./Name 3.4` or `./Name 2147483648`? (The last one is 1 bigger than `INT_MAX`) – klutt Aug 08 '20 at 07:51
  • 1
    @reyad What I'm saying is that if you have to pick only one function to learn, then it should be `strtol` instead of `atoi` :) – klutt Aug 08 '20 at 08:02
  • @klutt, I completely agree with you in all cases, but this: `strtol` returns `long int`, which in most machine is `4 byte`, same as `integer`. So, it's much not a help `./Name 2147483648` in this case. But, all other cases I agree... – reyad Aug 08 '20 at 08:09
  • @klutt, I also agree with your second point...One of the reason, I've provided `atoi`, so that I can make OP[(s)he seems like a newbie] easily understand that, what we need here is to convert 'string' to 'number', that's all. `strtol` uses three params(with a base), so, `atoi` seems easier and understantable to deliver for a `newbie`. If (s)he sticks around with, of course (s)he'll start using `strtol`. But, in this case, I though, it is better to deliver the understandings simply....I hope I make it clear to you... – reyad Aug 08 '20 at 08:15
  • @reyad You can check if the conversion has succeeded with `if(errno == ERANGE)`. For your second point, my opinion is that the dangers in C is too often hidden from newbies. If you want a language where everything is safe, don't pick C. If you pick C, you need to learn about the dangers. – klutt Aug 08 '20 at 08:35
  • @klutt, you'll only get stronger by facing danger, if that doesn't kill you...:P...jokes apart, I will edit my answer...and also include the secure version...I hope you could agree to that... – reyad Aug 08 '20 at 08:42
  • @reyad Hehehe, sure. Well, it's just that if you teach beginners unsafe methods, they will soon have some weird undefined behavior, and then they post a new question where the answer is how to use the safe methods that can be error checked. :) – klutt Aug 08 '20 at 08:47
  • @klutt, I've included the code with `strtol`, pls have a look...tell me if i can improve it in some way... – reyad Aug 08 '20 at 09:09