problems with #include

Started by sannemander, October 10, 2011, 04:26:40 PM

Previous topic - Next topic

sannemander

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!




Stefan Pendl

I think you code should more look like the below one.


#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

Bitbeisser

Quote from: sannemander 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????
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...
QuoteiRandomNum = (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...
Quoteif (iResponse == RandomNum){
Here, you are opening up a code block with a curly bracket, but you don't close that code block...
Quoteprintf("\nLucky guess!!!!\n");
}
Here you need to add a closing curly bracket
Quoteelse /* (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)
Quoteprintf("\nWRONG!!!!!"\n);
The trailing "\n" belongs inside the string...
Quoteprintf("\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}
QuoteNow 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...
#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

thanks, gonna work on that, though it´s strange that i really just get this one error!

CommonTater