0

I have been working on pset1 for a few days now and can't figure out, for the life of me, what my error is. I've gone about it a million ways and I'm stumped halfway. I'll be attaching the gedit code along with the terminal error below:

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

int main(void)
{
int h;

{
do
{
    printf("Give me a number: ");
    h = GetInt(); 
 if (h < 1 || h > 23)
    {
        printf("Sorry! Pick a number between 1 and 23 \n");
    }   
while (h < 1 || h > 23);  
}
}

TERMINAL ERROR

enter image description here

2

Check your curly braces and punctuation. The proper form of a C program

int main(void)
{
    //program code here
}

For a loop

do
{
   //do this
}
while (this is true);

For an if

if (this)
{
    //do this
}

If you put it all together:

int main (void)
{
    // blah blah
    do
    {
        // something  
        if (this)
        {
           // do this
        } 
    }
    while (this);
}
9
  • I did. I added the curly brace you suggested but received the exact same error. I tried playing around with the curly braces but it either gave me more errors or the same one. Oct 26 '14 at 2:28
  • int main(void); <--- a semi-colon tells the compiler it is the end of the statement. Notice how I don't have that?
    – curiouskiwi
    Oct 26 '14 at 2:29
  • now its two errors. expected 'while' in do/while loop signaling to h=GetInt(); to match do Oct 26 '14 at 2:31
  • see my edited answer
    – curiouskiwi
    Oct 26 '14 at 2:34
  • I continue to see the two same errors plus the original one. Oct 26 '14 at 2:51
0

Move the while() statement at the end of the do-while loop to after the curly brace. Make it look like this.

do
{
    printf("Give me a number: ");
    h = GetInt(); 
    if (h < 1 || h > 23)
    {
        printf("Sorry! Pick a number between 1 and 23 \n");
    }
} while (h < 1 || h > 23);
0

I think u don't need the opening curly braces after int h declaration i.e, int h; { <-----

N the corresponding closing brace

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .