Need help please

This is a discussion on Need help please within the C Programming forums, part of the General Programming Boards category; Ok before I ask my question here, please understand that I am totally new to C and I don't know ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    Question Need help please

    Ok before I ask my question here, please understand that I am totally new to C and I don't know a dang thing compared to most of you so please look at my code and laugh your arses off then if you can still see from the tears in your eyes from laughing see if you can tell me where I am screwing this up....The code compiles and runs fine, my problem is I dont know how to get the game to repeat, it just keeps ending. It also keeps allowing guess's after the number has been found

    PS this is the first attempt at writing code so be gentle lol =)

    //************************************************

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    #include <conio.h>

    #define RANGE 100
    #define TRIES 10
    #define TRUE 1
    #define FALSE !TRUE

    int rnd(int range);
    void seedrnd(void);

    void main()

    {

    int guessme,guess,numberTries,keepPlaying;
    char c;
    seedrnd();
    guessme=rnd(RANGE);


    printf("********************************** GUESS!?! *******\
    *****************************\n*************\
    ************* {Guess The Random Number} ***************************\n");


    printf("I am thinking of a number between 1 and %i.\n\n",RANGE);
    printf("Can you guess it in %i tries or less?\n\n",TRIES);


    keepPlaying=TRUE;
    numberTries=1;
    while(numberTries<=TRIES)

    {

    printf("Guess #%i:",numberTries);
    scanf("%i",&guess);
    numberTries++;

    if(guess==guessme)

    {
    printf("You got it!\n\n");

    }

    else if(guess<guessme)

    printf("Sorry to low!\n");

    else

    printf("Sorry to High!\n");

    }

    printf("The answer was %i!\n",guessme);

    // Somewhere in this section I need to edit, I get the Yes or No
    // but program ends on either response, also it keeps allowing guess's
    // after the correct number is found.


    keepPlaying=1;
    while(keepPlaying<=TRIES)
    {
    printf("Wanna Play again\n");
    printf("Type (Y) for Yes, (N) for No!\n");
    keepPlaying++;
    c=toupper(getch());
    if(c=='Y');

    {
    printf("Goodluck\n");
    keepPlaying=TRUE;
    break;
    }
    }
    }



    // This section Interger Range

    int rnd(int range)

    {

    int r;

    r=rand()&range;
    return(r);

    }




    // This section Seed Random Number using NULL

    void seedrnd()

    {

    srand((unsigned)time(NULL));

    }

  2. #2
    Unregistered
    Guest
    So when am I supposed to start laughing? You have absolutely nothing to be ashamed about due to lack of experience. And to be honest, the code is actually pretty good once formatted a bit.

    Check out the differences between the code you posted and the code I posted, I made a few minor changes that cause the program to work properly.
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <time.h> 
    #include <ctype.h> 
    #include <conio.h> 
    
    #define RANGE 100 
    #define TRIES 10 
    #define TRUE 1 
    #define FALSE !TRUE 
    
    int rnd(int range); 
    void seedrnd(void); 
    
    void main() 
    { 
      int guessme,guess,numberTries,keepPlaying = TRUE; 
      char c; 
      seedrnd(); 
      guessme=rnd(RANGE); 
    
      printf("Guess The Random Number\n"); 
      printf("I am thinking of a number between 1 and %i.\n\n",RANGE); 
      printf("Can you guess it in %i tries or less?\n\n",TRIES); 
    
      while(keepPlaying == TRUE){
    	numberTries = 1;
        while(numberTries<=TRIES){ 
          printf("Guess #%i:",numberTries); 
          scanf("%i",&guess); 
          numberTries++; 
          if(guess==guessme){ 
            printf("You got it!\n\n");
            break;
          }
          else if(guess<guessme) 
            printf("Sorry to low!\n"); 
          else 
            printf("Sorry to High!\n"); 
        } 
    
        printf("The answer was %i!\n",guessme); 
    
        keepPlaying=1; 
        while(keepPlaying<=TRIES) { 
          printf("Wanna Play again\n"); 
          printf("Type (Y) for Yes, (N) for No!\n"); 
          keepPlaying++; 
          c=getchar(); 
          if(toupper(c)=='Y'){ 
            printf("Goodluck\n"); 
            keepPlaying=TRUE; 
            break; 
          }
          else
            return;
        } 
      }
    }
    
    int rnd(int range) 
    { 
      int r; 
      r=rand()&range; 
      return(r); 
    } 
    
    void seedrnd() 
    { 
      srand((unsigned)time(NULL)); 
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Haven't you ever heard of a LOOP?

    Anyway, for one, your if() statement should not have a ";" at the end of it...

    Lemme just give it a shot, since you look like you're trying...

    One thing is though, I've never worked with the random functions, so you'll have to get help there elsewhere, but your seedrnd() function has no way of interacting with the program. It should return something...

    #include <conio.c> //for clrscr();

    int main()

    {

    int guessme,guess,numberTries;
    char c;
    //seedrnd(); // huh?

    do{

    guessme=rnd(RANGE);


    printf("********************************** GUESS!?! *******\
    *****************************\n*************\
    ************* {Guess The Random Number} ***************************\n");


    printf("I am thinking of a number between 1 and %i.\n\n",RANGE);
    printf("Can you guess it in %i tries or less?\n\n",TRIES);

    numberTries=1;


    while(numberTries<=TRIES && guess != guessme)
    {
    clrscr();
    printf("Guess #%i: ",numberTries);
    scanf("%i",&guess);
    numberTries++;

    if(guess==guessme)
    printf("You got it!\n\n");

    else if(guess<guessme)
    printf("Sorry to low!\n");

    else
    printf("Sorry to High!\n");

    } // end of while...

    clrscr();
    printf("The answer was %i!\n\n",guessme);

    printf("Wanna Play again\n");
    printf("Type (Y) for Yes, (N) for No!\n");

    c=toupper(getch());

    if(c=='Y')
    printf("Goodluck\n");

    else
    printf("Well thanks for playing--bye!");

    }while(c != 'N'); // end of do...

    return 0;

    }// end of proggie

    ...see if that works...

    One thing though, I omitted some of your flags and moved those mechanisms to the while() and do/while() conditions. You might want to change those changes I made...
    Last edited by Sebastiani; 02-03-2002 at 05:33 PM.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  4. #4
    Unregistered
    Guest
    I've fixed it for ya, removed the 2 functions and put it in main cause it is a waste of time since it isn't much to worth the space for it to become functions. Yeah use puts("your string"); instead of printf(...) if you are only going to output strings unless you don't want an automatic new line from puts. Ya indent your source and use a constant style of writing source code to prevent inaccurately inputting extra brackets and stuffs like that.
    ************************************************** **
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>

    #define RANGE 100
    #define TRIES 10
    #define TRUE 1
    #define FALSE 0

    int main()
    {
    int guessMe, guess,
    numberTries = 1,
    keepPlaying = TRUE,
    cKeepPlaying = 1;
    char c;

    //Set up the seed
    srand((unsigned)time(NULL));

    puts("********************************** GUESS!?! ************************************");
    puts("************************** {Guess The Random Number} ***************************");

    while(keepPlaying == TRUE)
    {
    guessMe = rand() & RANGE;

    printf("I am thinking of a number between 1 and %i.\n\n",RANGE);
    printf("Can you guess it in %i tries or less?\n\n",TRIES);

    do{
    printf("Guess #%i:",numberTries);
    scanf("%i", &guess);
    numberTries++;

    if(guess == guessMe)
    printf("You got it!\n\n");
    else if(guess < guessMe)
    printf("Sorry to low!\n");
    else
    printf("Sorry to High!\n");
    }while(numberTries <= TRIES && guess != guessMe);

    printf("The number was %i\n", guessMe);

    if(keepPlaying <= TRIES && keepPlaying != FALSE)
    {
    printf("Wanna Play again?\n");
    printf("Type (Y) for Yes, (N) for No!\n");

    c=toupper(getch());
    if(c == 'N')
    {
    keepPlaying = FALSE;
    exit(0);
    }
    else
    puts("Good Luck!");
    cKeepPlaying++;
    }
    }

    return 0;
    }

  5. #5
    Unregistered
    Guest
    Alright, i suppose you do have you ident style..hehe this is the first time i posted on this board so i didn't know the ident's disappears. Well the reason i am here again is, i forgot to tell you that I made a few redundant/useless conditions and a bit of other things for you so that if you ever find out what they are and then fully corrected them and optimised it, it'll probably teach you a few things or too. laterz

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    If you wanted to keep the flags, it might look like this:

    #include <conio.c> //for clrscr();

    #define TRUE 1
    #define FALSE 0

    int main()

    {

    int guessme,guess,numberTries, keepPlaying = 0, wonGame = 0;
    char c;
    //seedrnd(); // huh?

    do{

    guessme=rnd(RANGE);

    printf("********************************** GUESS!?! *******\
    *****************************\n*************\
    ************* {Guess The Random Number} ***************************\n");


    printf("I am thinking of a number between 1 and %i.\n\n",RANGE);
    printf("Can you guess it in %i tries or less?\n\n",TRIES);

    numberTries=1;


    while(numberTries<=TRIES && !wonGame)
    {
    wonGame = FALSE;

    clrscr();
    printf("Guess #%i: ",numberTries);
    scanf("%i",&guess);
    numberTries++;

    if(guess==guessme)
    {
    printf("You got it!\n\n");
    wonGame = TRUE;
    }

    else if(guess<guessme)
    printf("Sorry to low!\n");

    else
    printf("Sorry to High!\n");

    } // end of while...

    clrscr();
    printf("The answer was %i!\n\n",guessme);

    printf("Wanna Play again\n");
    printf("Type (Y) for Yes, (N) for No!\n");

    c=toupper(getch());

    if(c=='Y')
    {
    printf("Goodluck\n");
    keepPlaying = TRUE;
    }

    else
    {
    printf("Well thanks for playing--bye!");
    keepPlaying = FALSE;
    }

    }while(keepPlaying); // end of do...

    return 0;

    }// end of proggie
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    Tanks =)

    Thanks Unregistered, I see the corrections you made and how you short cutted some of the stuff like seedrnd(), i pasted what you did into a new .c file and ran it, after the 10th guess it displays the right number to be guessed LOL but thats ok, I really appreciate the help, I will keep messing with it now until it works right, you gave me the steps to maiking it repeat and thats what was confusing me =) Oh and just a tip for you if you ever use the "toupper" (converts lower case into upper case *hence toupper* so you dont have to worry about what case your users type) you have to use #include <ctype.h> in your file

    Seb, yes I have heard of a loop and thats what was confusing me, i didnt know how to place my code to make it work properly, I appreciate the time you took to reply to my question =) I tried to run your code and kept coming up with 76 errors and 3 warnings =P

    But to get a true random number you have to use the seedrnd or srand =)

    James

  8. #8
    Unregistered
    Guest
    hmmm. lol i must've interpreted your game wrong LOL. what i've done to your game is that everytime you play again it will restart to random number so it is different every time. heheh. That's why I've decided that it should display the answer after the tenth guess anyway.. but i guess it is a trivial thing so it didn't matter. Just felt like mentioning it just in case

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21