Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
Undeclared Identifier in do while loop, C
#include <stdio.h>
#include <cs50.h>

int main(void)
{
    do
    {
        //ask for input with 1-8
        int height = get_int("Height: ");
    }
    while (height > 0);
}

And I got the error code: use of undeclared identifier "height" (in the while statement) I'm completely green to programming and I have no idea how to fix this. Can someone help?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The scope of the variable height is the body of the do..while loop. The condition of this loop is outside of the body, therefore height is not in scope in the loop condition.

Move the definition of height outside of the loop.

int height;
do
{
    //ask for input with 1-8
    height = get_int("Height: ");
}
while (height > 0);
over 4 years ago · Santiago Trujillo Report

0

Height is not visible (nor exists) after the closing brace of the do body block. You must declare it outside of the loop. I've marked the points where height ceases to exist in comments below:

int main(void)
{
    do
    {
        //ask for input with 1-8
        int height = get_int("Height: ");
    } // <- the scope and visibility of height ends here
    while (height > 0); // ... so it doesn't exist here.
}

the solution is to declare height before the do keyword, as in:

int main(void)
{
    int height;
    do
    {
        //ask for input with 1-8
        height = get_int("Height: ");
    }
    while (height > 0); // now, height is visible here.
} // <-- visibility and scope of height ends here.

Another thing is that, if your intention is to ask for a height and repeat the question until height > 0, then you should write while (height <= 0) instead, (you repeat the question while the answer is not correct)

over 4 years ago · Santiago Trujillo Report

0

EDIT: It has been pointed out to me that I misread your loop’s termination condition. (Sorry, insomnia.)

Possible Answer 1

As I read it, you appear to want to repeatedly do something with positive values of height, and terminate when an invalid value is given.

To make that happen, the correct thinking on this should be:

  1. Get a value
  2. If the value is zero, terminate the loop
  3. Else do stuff with the value

Hence:

while (true)
{
  int height = get_int("Height: ");
  if (height <= 0) break; // invalid height terminates loop

  // use height here //
}

Possible Answer 2

If your desire is to repeatedly ask for a height until the user gives you a valid value in order to continue, then the only answer is to move the value declaration out of the loop, as anything declared local to the loop will not be usable outside of the loop:

int height;
do {
  height = get_int("Height: ");
} while (height is not valid);

// use height here //

This is exactly as Luis Colorado explains, because he was awake enough to properly read your loop condition.

Useful stuff for C++ people finding this by title only

As I had previously misread the solution to terminate if height becomes zero (a common semaphore for homework problems involving integers), and because C++ people will also find this answer, I had also given a C++ solution where the condition and body share locality as follows:

In current versions of C++ you can integrate the variable into the loop condition:

while (int height = get_int("Height: "))
{
  // use height here //
}

I think this is true of C++11 or later, but it might have been introduced in C++14, IDK and don’t care to look it up. You should be using nothing less than C++17 these days...

This solution only works for variables that can be converted to truthy values — in this case, the loop terminates when height is zero.

In OP’s particular case, this won’t work.

Now go vote for Mr Colorado’s answer, because it explains the locality problem better.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!