-4

I'm using a function of the library CS50 on cs50's environment, and this is how you store a value on a variable gathering it with an input:

long i = get_long("Enter a long: ");

Here comes my code:

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

int main(void)
{

do
{
    long cardNum = get_long("Card number:\n");
}
while (0 <= cardNum <= 9999999999999999);


if (340000000000000 <= cardNum <= 379999999999999)
{

...and so on.

The issue WAS

use of undeclared identifier 'cardNum'

I've added:

long cardNum;

BEFORE the do-while loop; PROBLEM SOLVED.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

0

You are using cardNum in the do block. Declare it outside the block of do and your error will be gone.

Correct way -

long cardNum = 0;
do
{
    cardNum = get_long("Card number:\n");
}
while (0 <= cardNum && cardNum <= 9999999999999999);

In your case, you were getting error because cardNum is only scoped for do block and isn't visible inside the while because its scope ends as soon as it leaves the do block. This is why you are getting use of undeclared identifier 'cardNum' as error. The compiler sees it as an undeclared variable inside the while() function.

NOTE : you are better off using long long int because maximum range of long is implementation based and might sometimes just be limited to 2,147,483,647 (most probably on 32 bit systems). You can also use int cardNum[20] or whatever your maximum limit is instead of 20.

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • Well, `LONG_MAX` is `9223372036854775807`. So, I guess it can hold that. Corrected the second point – Abhishek Bhagate Jun 01 '20 at 19:45
  • `LONG_MAX` is implementation-defined. – Eric Postpischil Jun 01 '20 at 19:55
  • I have no idea what you are suggesting me now. Only Abhishek seems to be replying to the error I'm referring; No, I can't use a string as this variable, it's for the CS50's problem set 2, and it says it shall be a float. The input is a float, and I should not use arrays. – Susanna Dafna Jun 01 '20 at 20:46
  • Plus, I tried to declare the variable outside of the do while loop, it generated basically the same error, saying that the variable was already declared before the loop, (0 in the case), so... – Susanna Dafna Jun 01 '20 at 20:47