# Unused Variable - Beginner Question

• 11-24-2012
intex
Unused Variable - Beginner Question
Hey guys, I am writing a program to output a half pyramid of hashes, although when I go to compile it says the variable "height is unused" even though I have declared it with the GetInt().

Code:

```#include <stdio.h> #include <cs50.h> int main(void) {     int height,line,hash;         do     {     int height = GetInt();     }     while (height>=1 || height<=23);         for(int line=1;line<=height;line++)         {                          for(int hash=1;hash<=(line+1);hash++)             {                 printf("#");             }                     printf("\n");         } return 0; }```
Code:

`error: unused variable 'height' [-Werror,-Wunused-variable]    int height = GetInt();`
• 11-24-2012
c99tutorial
Quote:

Originally Posted by intex
Hey guys, I am writing a program to output a half pyramid of hashes, although when I go to compile it says the variable "height is unused" even though I have declared it with the GetInt().

You have two variables called height and one of them is unused. Look here:

Code:

```int height,line,hash;          do    {   int height = GetInt();     }  while (height>=1 || height<=23);```
notice you have "int height" declared in the outer block and "int height = GetInt()" in your inner block -- this inner block will have its own height variable. The attached while statement uses the version from the outer block, which is is actually undefined, so as it stands your do-while loop does not have a predictable end. You probably want this:

Code:

``` int height,line,hash;        do {        height = GetInt();  }  while (height>=1 || height<=23);```
• 11-24-2012
Nominal Animal
Quote:

Originally Posted by intex
Hey guys, I am writing a program to output a half pyramid of hashes, although when I go to compile it says the variable "height is unused" even though I have declared it with the GetInt().

Code:

```#include <stdio.h> #include <cs50.h> int main(void) {     int height,line,hash;```

Above you declare the original height variable.

Quote:

Originally Posted by intex
Code:

```        do     {     int height = GetInt();```

The above one is a separate height variable. Because you have int height, you are declaring another variable, one local to this scope (existing only within this pair of braces, the body of the do..while loop).
Quote:

Originally Posted by intex
Code:

```        }     while (height>=1 || height<=23);```

These refer to the first height variable, not the one you defined within the loop body.

If you wanted to assign the result of the GetInt() function into your existing height variable, you should use height = GetInt();, right?

Edited: C99tutorial posted the same thing while I was typing mine. :)
• 11-24-2012
intex
Hey thanks for the prompt reply, I changed to the inside block to " height = GetInt(); " but and it executes smoothly but now when I input an integer it continuously loops inside of the do-while loop. Any ideas?
• 11-24-2012
camel-man
Code:

`while (height>=1 || height<=23);`
That covers all possible numbers. Perhaps you want && instead.