Question

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

int main (void)
{
        printf("Welcome to blabla\n");


// This next part gets user input and checks if the integer given by user is between 1 and 8

        int n;
        do
        {
            printf("Give me a integer between 1 and 8.\n");
            int n = GetInt();
        }
        while (n >= 1 && n <= 8);

        printf("You picked the number %d.\n", n);   

        return(0);
}
Was it helpful?

Solution 2

This line:

int n = GetInt();

tries to declare a new local variable n. It's completely separate from the local variable you declared earlier:

int n;

When you call GetInt(), you just want to assign the value to the existing variable, like this:

n = GetInt();

OTHER TIPS

Change

int n = GetInt();

with

n = GetInt();

You were introducing a second variable n with block scope that was going to be discarded after the first }. The lifetime of an automatic object ends at the end of the block where it has been declared.

You're redeclaring n inside the loop, so the outside declaration is not used. It's probably not what you want.

Replace

int n = GetInt();

with:

n = GetInt();

Why are you declaring n as int n once again inside the do {} ? just change it to :

n = GetInt();

The scope of inner n is till the '}' of do. After that it was never used. The n outside the loop is the used n

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top