NO

Author Topic: problems with #include  (Read 3764 times)

sannemander

  • Guest
problems with #include
« on: October 10, 2011, 04:26:40 PM »
Hello!
I am starting with c programming and I have an error i cannot fix!
who can help me????

#include <stdio.h>
#include <ctype.h>

int main() {

int iResponse = 0;
int RandomNum = 0;
srand(time());

iRandomNum = (rand()%10)+1;

printf("quess a number between 1 and 10: ");
scanf("%d", &iResponse);

if (isdigit(iResponse) != 0){
printf("/nThis is not a number moron/n");
}

if (iResponse == RandomNum){
printf("\nLucky guess!!!!\n");

else(iResponse != RandomNum)
printf("\nWRONG!!!!!"\n);
printf("\nThe number was %d\n");}
}

Now I get an error 1036: Syntax error in #include. before i got much more errors but i could deal with them.

thx!




Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: problems with #include
« Reply #1 on: October 10, 2011, 08:41:06 PM »
I think you code should more look like the below one.

Code: [Select]
#include <stdio.h>
#include <ctype.h>

int main(void) {

    int iResponse = 0;
    int RandomNum = 0;
    srand(time());

    iRandomNum = (rand()%10)+1;

    printf("quess a number between 1 and 10: ");
    scanf("%d", &iResponse);

    if (isdigit(iResponse) != 0) {
        printf("/nThis is not a number moron/n");
    } else {
        if (iResponse == RandomNum) {
            printf("\nLucky guess!!!!\n");
        } else {
            printf("\nWRONG!!!!!"\n);
            printf("\nThe number was %d\n");
        }
    }
}
---
Stefan

Proud member of the UltraDefrag Development Team

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: problems with #include
« Reply #2 on: October 11, 2011, 12:44:06 AM »
Hello!
I am starting with c programming and I have an error i cannot fix!
who can help me????
We can try...
Quote
#include <stdio.h>
#include <ctype.h>
In which of those lines do you get the error 1036?
Beside that, srand is defined in stdlib.h, which you do not include and time() is defined in time.h (also missing the include) but in a different format...
Quote
int main() {

int iResponse = 0;
int RandomNum = 0;
srand(time());
See comments above...
Quote
iRandomNum = (rand()%10)+1;
Typo at least, you define above a variable of type int named RandomNum but then you are trying to use a var named iRandomNum here...
Quote
if (iResponse == RandomNum){
Here, you are opening up a code block with a curly bracket, but you don't close that code block...
Quote
printf("\nLucky guess!!!!\n");
}
Here you need to add a closing curly bracket
Quote
else /* (iResponse != RandomNum) */
There's no condition without a preceding "if" and in this case, you can ommit it in the first place (if the "==" condition does not apply, "!=" is automatic, there is no "maybe" condition)
Quote
printf("\nWRONG!!!!!"\n);
The trailing "\n" belongs inside the string...
Quote
printf("\nThe number was %d\n");}
You are actually missing to include the value for the %d in the format string, which should be RandomNum
Quote
}
Quote
Now I get an error 1036: Syntax error in #include. before i got much more errors but i could deal with them.
You get a lot more than that...

At the very least, you program should look like this...
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

int main() {

  int iResponse = 0;
  int RandomNum = 0;
  srand(time());

  RandomNum = (rand()%10)+1;

  printf("quess a number between 1 and 10: ");
  scanf("%d", &iResponse);

  if (isdigit(iResponse) != 0){
printf("/nThis is not a number moron/n");
  }

  if (iResponse == RandomNum){
printf("\nLucky guess!!!!\n");
  }
  else // (iResponse != RandomNum)
printf("\nWRONG!!!!!\n");

printf("\nThe number was %d\n", RandomNum);
} // end  of main

This still complains about the time() call, I leave that as the last obstacle for you,as we do not do homework for other here in the forum...  ;D

You also can do yourself a huge favor if you get used to a proper indentation style, it really helps to write readable programs. And as far a s proper sequences of opening/closing brackets goes, the editor of the IDE gives you a lot of hints, so there is no real excuse to make mistakes here...  ;)

Ralf

sannemander

  • Guest
Re: problems with #include
« Reply #3 on: October 11, 2011, 11:58:42 AM »
thanks, gonna work on that, though it´s strange that i really just get this one error!

CommonTater

  • Guest
Re: problems with #include
« Reply #4 on: October 11, 2011, 12:15:41 PM »
Try .... srand(time(NULL));