Unused Variable - Beginner Question

This is a discussion on Unused Variable - Beginner Question within the C Programming forums, part of the General Programming Boards category; Hey guys, I am writing a program to output a half pyramid of hashes, although when I go to compile ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    Question 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();

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    981
    Quote Originally Posted by intex View Post
    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);

  3. #3
    Left
    Join Date
    Oct 2011
    Posts
    816
    Quote Originally Posted by intex View Post
    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 View Post
    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 View Post
    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.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    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?

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    589
    Code:
    while (height>=1 || height<=23);
    That covers all possible numbers. Perhaps you want && instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2012, 03:28 PM
  2. unused variable in a program?
    By password636 in forum C Programming
    Replies: 3
    Last Post: 09-29-2012, 12:42 AM
  3. Beginner, help with loop and variable
    By keeganstarr in forum C Programming
    Replies: 16
    Last Post: 05-28-2012, 07:31 PM
  4. unused variable causes lots of problems
    By Isnalla in forum C Programming
    Replies: 1
    Last Post: 02-25-2012, 09:40 AM
  5. Replies: 10
    Last Post: 03-28-2010, 01:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21