1

I posted this two days ago, but since my question has since changed to something else and I now need help regarding a new problem in my code, I thought it would be alright to make a new post. If I'm wrong, please close this question; I'm new to the community and don't want any trouble. I just want some help with my problem.

I've received some feedback: I know my two problems with this code: I need to be checking that the input by the user is within range (0~2^32 - 1; if it isn't in range, it gets the "warning" message), and I need to check for negative numbers (these should get the "invalid" warning). But I'm completely stumped. Could I please perhaps get some more help on working my code to do the above two things?

Update: I think this code finally did the trick. Please let me know if I'm overlooking an error that isn't showing up after being compiled. Thanks!

#include <stdio.h>

int main() {
long long int num;
int count = 0;

printf("Please enter an integer: ");
scanf("%lld", &num);

if (num >= 0 && num <= 4294967295) {
    while(num){
        count += num & 1;
        num >>= 1;
    }
    printf("%d\n", count);
} else {
    printf("Warning: this is not a valid integer\n");
}
return 0;
}
  • Huh I don't know this either. Thing is, even if you overflow the unsigned int it will just go back to 0 and start going up again, so it will always store a valid int even if you input a number bigger than 2^32-1 – savram Sep 20 '17 at 23:55
  • Have you tried something like `if(num < 0)` for example? Is your question really 'how to check for negative numbers?' – alain Sep 21 '17 at 00:02
  • Since `num` is unsigned, it can't be negative. – Barmar Sep 21 '17 at 00:04
  • Instead of using `scanf`, use `fgets` to read a line. Then use a function to parse it as a number, and check if it's in the range you want. – Barmar Sep 21 '17 at 00:05
  • @Barmar: Yes, that was my response to my professor. He then said this: "You can read in an input number as signed number and check whether it is a negative number. The valid number should be 0<= input <= 2^32 - 1." My question is: if we read in a regular signed int value, doesn't that greatly decrease the range? 0 ~ 2,147,483,647 is a lot less than 2^32 - 1. How would I check the range (or how would the user be able to enter a number outside of the int range) when our range for the project is of an unsigned int, but the user enters a signed int? –  Sep 21 '17 at 00:06
  • @Barmar: I believe we are meant to use scanf, as part of the instructions. –  Sep 21 '17 at 00:07
  • You'll need to use `long long int` to be able to hold numbers bigger than 2^32, then test if it's less than 2^32. – Barmar Sep 21 '17 at 00:08
  • For future reference, if you have a new, separate problem, you should make a new post, but if you're asking essentially the same thing you were already asking, just edit the existing post (though not in a way that totally invalidates existing answers). In this case, from a quick glance it seems like you made the right choice to make a new post. – David Z Sep 21 '17 at 00:10
  • @Barmar: I see! So the range for a long long int is bigger than an unsigned int? I didn't know that (haven't been coding for very long). I'll try to modify the code and see if it works. –  Sep 21 '17 at 00:11
  • For the range 0 ~ 2,147,483,647 you can use an `int` and a simple `if(num >= 0 && num < 2147483647)` – alain Sep 21 '17 at 00:11
  • @alain: I believe our range is meant to be 0 ~ 4294967295. –  Sep 21 '17 at 00:12
  • @alain That's 2^31-1, not 2^32-1. – Barmar Sep 21 '17 at 00:12
  • He mentioned this range in a comment above. (But yes it seems the requirement is 2^32) – alain Sep 21 '17 at 00:12
  • @GarrettMcClure There are different sizes of int: `char`, `short`, `int`, `long int`, `long long int`. – Barmar Sep 21 '17 at 00:12
  • @alain He wrote "the valid number should be 0<= input <= 2^32 - 1". Then he mentioned `2,147,483,647` as being too small to check for that range. – Barmar Sep 21 '17 at 00:14
  • @Barmar: Does long long int use %l? Or %d? –  Sep 21 '17 at 00:14
  • @GarrettMcClure Read the `scanf` documentation to learn. – Barmar Sep 21 '17 at 00:14
  • Update: it looks like something is still incorrect. When I tested the program, 2 returned a value of zero, which is incorrect. What could have caused this? I've edited the top post with my updated/corrected code. –  Sep 21 '17 at 00:21
  • New update: I think I figured it out! Thanks guys. I'll post the code that seems to have worked above, just in case someone else wants to reference it. Hope there's no more errors that I'm not seeing. –  Sep 21 '17 at 00:25

1 Answers1

0

Using scanf might be problematic and dangerous. A better way would be to use fgets to get input from the user, and then use strtoul/strtoul to convert the string to an integer.

Idan
  • 333
  • 2
  • 8