0

I'm trying to take an input integer between 1 and 100 and store its value as n, which I will then refer to as the scale factor, and use it later on in the redefinition of bi.biWidth *= n; and in similar ways to resize the image by the scale factor, and have written code that will give an error message if it's not an integer between 1 and 100, but my code seems to be giving an error even when a correct input is given. I've tried debug50, and for some reason it suggests that n is 0, even when I input an integer, and then it switches to some garbage (eg -6235) value when you start the if statement. My code is:

    int n = (int) &argv[1];
    // ensure integer is input as scaleFactor
    if (n > 100 || n < 0)
    {
        printf("Usage: scaleFactor infile outfile\n");
        return 91;
    }

What do you think? I seem to be getting stuck and asking for help every pset...this is worrying. Thank you!

1

Interesting issue. So, what's actually stored in n?

int n = (int) &argv[1];

That & that appears before argv[1], means to use the address of argv[1], so it's inserting the address, not the contents of argv[1] in n. If you were to print n, you'd see it change each time you ran the code.

But fixing that still won't give you working code. argv[] holds chars, not ints. Here's a hint: atoi() is your friend! ;-)

As for worrying about asking for help on each question, well, maybe a little concerned, but not much. It sounds like programming is something totally new and foreign to you, but you're tackling it. Everyone has to go through the learning curve to program. Part of that is getting experience with the common tools, functions, syntax and techniques involved. The more you use them, the easier it gets and the less you need to ask for help. Perhaps you need to improve your ability to research how to do things, but someone has almost certainly already done anything you're trying to do. Just remember that google is your friend here. There's a wealth of "how to" info on programming on the internet!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • I've used atoi() before for this purpose, but I remember being told in Lecture 1/2 that if you implicitly cast using (int) it would work too. Is it not working because in this case it's a command-line argument we're inputting? – Tanishq Kumar Jul 27 '19 at 10:07
  • Exactly. ALL command line arguments are always strings, even if they're numbers. If you want to get them into an int, you need to use the atoi() function. Same applies to floats, atof(). – Cliff B Jul 28 '19 at 1:44

You must log in to answer this question.

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