3

I completed an introduction to programming using Python and I am more used to Python then C, so I might mix the two up. C is giving me all kinds of errors. I tried changing things but noting seems to work; once I figure out C's syntax I am probably going to rewrite this program.

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

int main(void)

{
    int num;

    do 
          {
        int num = getint(); 

    while( int num < 0 or int num > 23 );

          }
}

Errors:

mario.c:13:9: error: expected expression
        while( int num < 0 or int num > 23 );
               ^
mario.c:16:1: error: expected 'while' in do/while loop
}
^
mario.c:9:2: note: to match this 'do'
        do 
        ^
mario.c:16:2: error: expected '}'
}
 ^
mario.c:6:1: note: to match this '{'
{
^
3 errors generated.
jharvard@appliance (~/Dropbox): make mario
clang -ggdb3 -O0 -std=c99 -Wall -Werror    mario.c  -lcs50 -lm -o mario
mario.c:13:9: error: expected expression
        while( int num < 0 or int num > 23 );
               ^
mario.c:16:1: error: expected 'while' in do/while loop
}
^
mario.c:9:2: note: to match this 'do'
        do 
        ^
mario.c:18:1: error: expected '}'
^
mario.c:6:1: note: to match this '{'
{
^
3 errors generated.
0
3

It's a misplaced curly brace.

do 
{
    int num = GetInt(); 
}

while( int num < 0 || int num > 23 );

You want to enclose the do statement in curly braces, but keep the while condition outside. Be careful of capitalisation. Functions are case-sensitive. Also, you can't use the word 'or' but instead use || (the double pipe). (and for future reference, && means 'and').

Give that a try!

Also, the Lecture Notes from Week 1: Programming Constructs in C has lots of examples of syntax for various loops, conditions, etc. Might be worth keeping handy as a reference while you work on this.

Brenda.

4
  • all bugs have been fixed thank you
    – zoot404
    Jul 12 '14 at 5:07
  • @zoot404, if the answer was helpful, please vote and also marked it as accepted. That helps others when looking for their own answers. :)
    – curiouskiwi
    Jul 12 '14 at 5:20
  • @curiouskiwi (sanity check on my part) when you say, "enclose the do statement in parentheses," do you mean curly braces {} rather than parentheses ()?
    – mazal
    Jan 31 '15 at 0:58
  • Yes thanks. I've edited my answer.
    – curiouskiwi
    Jan 31 '15 at 1:15

You must log in to answer this question.

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