1

I'm trying to get a 15 or 16 digits numbers (integers) from users and then using a do while loop to check if the user entered a valid number, otherwise, they will be re-ask to enter the number.

THE PROBLEM IS
clang is generating error and running help50 suggest that the problem is within the while loop block. I'M CONFUSE as the logic appears right.

Here's my code below: ================================

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

 int main(void)
{
  long C;
  do
  {
 // below code prompt user for a number abd store it
    C = get_long("Enter your card number: ");
      //below code checks if user entered integer min. of 15 or max. of 16 digits 
  }while(C< 0 && C < 999999999999999 || C < 0 && C > 9999999999999999);

  printf("%li \n", C);

}

================================

  • Thanks All, it now works fine after using the () && (). But, one more thing: is there a more better way of check IF A USER ENTERED A 15 OR 16 DIGITS NUMBER AND NOTHING ELSE without using while (C > 9999999999999998) Or something similar one 9999999....? Tia – KeleosWilliam Dec 23 '19 at 5:58
  • Oh sorry, I didn't read well what you tried to achieve... You can use a helper function like ``` int numberLength (int value) { int counter = 1; while (value > 9) { counter++; value /= 10; } return counter; } ``` To get the length of a number... Also in the function you can directly add more validations to only check if it returns true or false . – Jason David Espinoza Siles Dec 23 '19 at 21:52
2

Assuming the error you are getting is not related to a linking error, which is common, then the error I assume you are getting is

    loop_check.c:14:15: warning: '&&' within '||' [-Wlogical-op-parentheses]
  }while(C< 0 && C < 999999999999999 || C < 0 && C > 9999999999999999);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~ ~~

Technically this is not an actual error, but the CS50 staff impose a flag on the compiler to treat warnings as errors when you compile. So the issue I see is your conditions to check for your while loop are ambiguous. Parentheses are a good solution to this issue. So extracting your conditions out, which appears more readable to you?

C < 0 && C < 999999999999999 || C < 0 && C > 9999999999999999
(C < 0 && C < 999999999999999) || (C < 0 && C > 9999999999999999)

I'm accepting the fact that my answer is a bit of a shot in the dark though. Its helpful when you ask questions that you provide the actual error as well. Help me help you.

| improve this answer | |
  • Thanks @Nicknapali82 that's exactly what the compiler showed (________^___ stuff). – KeleosWilliam Dec 23 '19 at 5:51
1

I'm not entirely sure about the logic of your conditions but putting the and (&&) conditions in brackets () should fix it. This is what you have:

while(C< 0 && C < 999999999999999 || C < 0 && C > 9999999999999999);

change to:

while((C < 0 && C < 999999999999999) || (C < 0 && C > 9999999999999999));

Otherwise the compiler taps out because it wouldn't know which conditional would need preference.

Aside from that the first condition seems redudant, as checking if a number is smaller than 99999... should also account for checking if it is lesser than 0. The OR branch you're looking for a number lesser than 0 and greater than your maximum? So either a negative or something greater than your maximum, think you mean something that is neither of ranges - !(C < 0 && C > 9999999999999999)

| improve this answer | |
  • "Otherwise the compiler taps out" is totally not right. Operator precedence dictates how the condition would be evaluated. Just FYI. Technically the && operator would be evaluated before the ||. – Nicknapoli82 Dec 23 '19 at 4:51
  • @Nicknapoli82 cheers yeah I should've realised. Thanks. Only seeing your answer to this now as well. – donyd Dec 23 '19 at 7:08
0

What is the error message? Also, it looks like the conditions are Chech if number is greater than zero and smaller than 999999999999999 Or Check if number is greater than zero and greater than 999999999999999 If you see both options it could be possible that your loop is never ending. Try removing the first condition and try again... Note: when you ask for help with an error please share the error message so it will be easier to help you. Let me know if this helps you. Kind regards

| improve this answer | |

You must log in to answer this question.

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