Can anyone take a crack at this for me?

This is a discussion on Can anyone take a crack at this for me? within the C Programming forums, part of the General Programming Boards category; I'm trying to do some homework involving custom libraries and such, which surprisingly seems to have turned out alright. The ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    24

    Can anyone take a crack at this for me?

    I'm trying to do some homework involving custom libraries and such, which surprisingly seems to have turned out alright. The main body of my program is giving me trouble though:

    Code:
    /*
    
    Assignment 4 [Source]- Random Number Game
    */
    
    /*============================================================================================================*/
    #include <stdio.h>
    #include <stdlib.h>
    #include "game.h"
    
    /*Define variables.*/
    int z;
    int v = 0;
    int r = 0;
    char go; error
    
    /*============================================================================================================*/
    int main (void)
    {
    
    /*================================================*/
    /*Function 1- Display header.*/
    print_head(); /*Call header function from game.h*/
    
    /*================================================*/
    /*Function 2- Generate number and read guess.*/
    while(v == 0)
    { /*Start the game.*/
    
    z = 1 + rand() %1000; /*Generate random number between 1 and 1000.*/
    
    /*Read guess from the user and assign to v.*/
    	printf("I have a number between 1 and 1000.\n Can you guess my number?\n Please type your first guess:");
    		v = 10001; /*Force start block to end loop.*/
    
    } /*Close start of game.*/
    
    while(v != 0)
    { /*Open the game's main body.*/
    
    	scanf("%i", &v); /*Read the user's guess and store at v.*/
    
    /*================================================*/
    /*Function 3- Call CONT from game.h*/
    CONT();
    
    /*================================================*/
    /*Function 4- Call GES from game.h IF the guess is incorrect.*/
    if(v != z)
    {
    GES();
    }
    
    /*================================================*/
    /*Function 5- Ask the user to restart the game or end the program.*/
    if(v == z) /*Open restart block.*/
    {
    	printf("Would you like to play again?(y/n):");
    	scanf("%c", &go);
    
    if(go == y || go == Y) /*Use input "Yes" to re-enter the start of the game.*/ error
    {
    	v = 0;
    }
    
    } /*Close restart block.*/
    
    /*================================================*/
    } /*Close the game's main body.*/
    
    /*================================================*/
    
    
    return 0;
    } /*Close the main function.*/
    /*============================================================================================================*/
    I marked what the debug told me. Can anyone help?
    Last edited by StateofMind; 04-22-2009 at 04:58 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe it's expecting:

    go == 'y'

    or

    go == 'Y'

    but not

    go == y

    Since you don't indent worth a diddly, I can't help you with the return error.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Code:
    } /*Close the main function.*/
    /*============================================================================================================*/
    
    
    
    return 0; ***ERROR?***
    } ***ERROR?***
    Why do you have code after the brace which closes the main function?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Quote Originally Posted by bithub View Post
    Code:
    } /*Close the main function.*/
    /*============================================================================================================*/
    
    
    
    return 0; ***ERROR?***
    } ***ERROR?***
    Why do you have code after the brace which closes the main function?
    Yeah, figured that one out after looking harder for a second. :x


    I believe it's expecting:

    go == 'y'

    or

    go == 'Y'

    but not

    go == y

    Since you don't indent worth a diddly, I can't help you with the return error.
    Thaaaank you.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Oh great, now it's letting me know about more problems. I knew there had to be something wrong with my header/definitions.

    Source.obj : error LNK2001: unresolved external symbol _GES
    Source.obj : error LNK2001: unresolved external symbol _CONT
    Source.obj : error LNK2001: unresolved external symbol _print_head
    Debug/Source.exe : fatal error LNK1120: 3 unresolved externals
    Error executing link.exe.
    game.h:
    Code:
    #define NAME "Mitch Trahan"
    #define DATE "4/21/2009"
    #define TITLE "RANDOM NUMBER GAME"
    
    /*=================================================================================================*/
    
    /*This function will display the header*/
    void print_head(void);
    
    /*=================================================================================================*/
    
    /*This function will count the number of guesses and evaluate the user.*/
    void CONT(void);
    
    /*=================================================================================================*/
    
    /*This function will check the guess and instruct the user.*/
    void GES(void);
    
    /*=================================================================================================*/
    game.c
    Code:
    #include <stdio.h>
    #include "game.h"
    
    /*================================================================================================*/
    /*Define function print_head.
    print_head- Display header.
    */
    
    void print_head(void)
    {
    printf("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
    	printf("\nProgram by: ");
    
    	printf(NAME);
    
    	printf("\nDate: ");
    
    	printf(DATE);
    
    	printf("\n");
    	printf(TITLE);
    	printf("\n");
    
    printf("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
    	
    	printf("\n");
    return;
    }
    
    /*================================================================================================*/
    /*
    Define function CONT. 
    CONT- Gather the number of guesses and evaluate the user.
    */
    void CONT(void)
    {
    	++r; /*Count the number of guesses and store the tally at "r"*/
    
    if(z == v) /*Once the user has guessed the correct number, begin evaluation.*/
    {
    
    		printf("Excellent! You guessed the number!"); /*Inform user of success.*/
    
    		printf("\nIt took you %i tries to guess correctly.\n", r); /*Display counted attempts.*/
    
    if(r > 10)
    {
    		printf("You should be able to do better!\n");
    }
    if(r == 10)
    {
    		printf("Aha! You know the secret!\n");
    }
    if(r < 10)
    {
    		printf("Either you know the secret or you got lucky!\n");
    }
    
    } /*Close the evaluation.*/
    
    
    return;
    }
    
    /*================================================================================================*/
    /*Define function GES.
    GES- Run the user's input against the random number generated.
    */
    void GES(void)
    {
    
    if(v > z)
    {
    	printf("Too high, try again.\n");
    }
    if(v < z)
    {
    	printf("Too low, try again.\n");
    }
    
    return;
    }

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps you haven't compiled "game.c" and linked with your program (in IDE's that's what projects are for).

    There may also be problems with your use of global variables (shouldn't they be extern and declared in a header?). In any case, one should be able to program a simple number guessing game without relying on global variables.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you actually linking your files when you compile them?
    gcc -o someprog someprog.c game.c -Wall -pedantic
    You need to be including all of the C or object files for them to be linked correctly.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    I actually hadn't linked them...thanks.

    There's definitely a problem with my variables, as the error that I'm getting now is calling them all undeclared. I've no concept of "global variables" or "external variables" so I'm about to go through my book for that. Thanks for all of the help.

    All right! Got it running. Have a few things to iron out, but it works now so all is well. Thanks again.
    Last edited by StateofMind; 04-22-2009 at 05:55 PM.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Well, for some reason the program sticks to only one function at a time. I can't get it to run through the whole code, right now it runs to int CONT (void); , then loops back to the scanf(), and ends when the number is guessed.

    Before I included the redundant IF statement before int CONT (void), the program skipped it and went to int GES (void) and then looped directly to scanf(), exiting the entire program if the number is guessed. So I can get one or the other to work, but not both. Shouldn't the program move on down the line after one function is executed?

    Code:
    /*============================================================================================================*/
    #include <stdio.h>
    #include <stdlib.h>
    #include "game.h"
    
    int z;
    int v = 0;
    int r = 0;
    char go = 'y';
    
    
    /*============================================================================================================*/
    int main (void)
    {
    
    /*================================================*/
    /*Function 1- Display header.*/
    print_head(); /*Call header function from game.h*/
    
    /*================================================*/
    /*Function 2- Generate number and read guess.*/
    while(go == 'y' || go == 'Y')
    { /*Start the game.*/
    
    	z = 1 + rand() %1000; /*Generate random number between 1 and 1000.*/
    
    /*Read guess from the user and assign to v.*/
    	printf("I have a number between 1 and 1000.\n Can you guess my number?\n Please type your first guess:");
    	
    		go = 't'; /*Force start block to end loop.*/
    	
    } /*Close start of game.*/
    
    while(go == 't')
    { /*Open the game's main body.*/
    
    	scanf("%i", &v); /*Read the user's guess and store at v.*/
    
    /*================================================*/
    /*Function 3- Call CONT from game.h*/
    if(z != v || z == v)
    {
    	int CONT(void);
    }
    
    /*================================================*/
    /*Function 4- Call GES from game.h IF the guess is incorrect.*/
    if(v != z)
    {
    	int GES(void);
    }
    
    
    /*================================================*/
    /*Function 5- Ask the user to restart the game or end the program.*/
    if(v == z) /*Open restart block.*/
    {
    	printf("Would you like to play again?(y/n):");
    	scanf("%c", &go);
    
    if(go == 'y' || go == 'Y') /*Use input "Yes" to re-enter the start of the game.*/
    {
    	v = 0;
    	r = 0;
    }
    
    } /*Close restart block.*/
    
    /*================================================*/
    } /*Close the game's main body.*/
    
    /*================================================*/
    
    return 0;
    } /*Close the main function.*/
    /*============================================================================================================*/
    Definitions:
    Code:
    #include <stdio.h>
    #include "game.h"
    
    
    /*================================================================================================*/
    /*Define function print_head.
    print_head- Display header.
    */
    
    void print_head(void)
    {
    printf("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
    	printf("\nProgram by: ");
    
    	printf(NAME);
    
    	printf("\nDate: ");
    
    	printf(DATE);
    
    	printf("\n");
    	printf(TITLE);
    	printf("\n");
    
    printf("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
    	
    	printf("\n");
    return;
    }
    
    /*================================================================================================*/
    /*
    Define function CONT. 
    CONT- Gather the number of guesses and evaluate the user.
    */
    int CONT(void)
    {
    	++r; /*Count the number of guesses and store the tally at "r"*/
    
    if(v == z) /*Once the user has guessed the correct number, begin evaluation.*/
    {
    
    		printf("Excellent! You guessed the number!"); /*Inform user of success.*/
    
    		printf("\nIt took you %i tries to guess correctly.\n", r); /*Display counted attempts.*/
    
    if(r > 10)
    {
    		printf("You should be able to do better!\n");
    }
    if(r == 10)
    {
    		printf("Aha! You know the secret!\n");
    }
    if(r < 10)
    {
    		printf("Either you know the secret or you got lucky!\n");
    }
    
    } /*Close the evaluation.*/
    
    return 0;
    }
    
    /*================================================================================================*/
    /*Define function GES.
    GES- Run the user's input against the random number generated.
    */
    int GES(void)
    {
    
    if(v > z)
    {
    	printf("Too high, try again.\n");
    }
    if(v < z)
    {
    	printf("Too low, try again.\n");
    }
    
    return 0;
    }

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Code:
    /*================================================*/
    /*Function 3- Call CONT from game.h*/
    if(z != v || z == v)
    {
    	int CONT(void);
    }
    That is not how you call a function, that is how you declare a function. To call it, you would do:
    Code:
    CONT();

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Quote Originally Posted by bithub View Post
    Code:
    /*================================================*/
    /*Function 3- Call CONT from game.h*/
    if(z != v || z == v)
    {
    	int CONT(void);
    }
    That is not how you call a function, that is how you declare a function. To call it, you would do:
    Code:
    CONT();
    Ah, yeah I've been playing around with that trying to get it work. Wasn't sure.

    Still doesn't want to go through all of the functions though.

    Does return 0; close the entire program? Should I change the value for return in my functions?

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    return 0; just returns from a function. It will only end the program if you are returning from the main() function.

  13. #13
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by StateofMind View Post
    Ah, yeah I've been playing around with that trying to get it work. Wasn't sure.

    Still doesn't want to go through all of the functions though.

    Does return 0; close the entire program? Should I change the value for return in my functions?
    Your code is incredibly hard to read. Please, post it with proper indentation. If we can't read it without massacring brain cells, we can't help you out.

    But, to answer your question. When you 'call' return and some value, in any function(Except void functions, which don't return anything), it will close the function. If it's main you're returning from, you're effectively closing the program. If it's a function, then you're ending that function, returning control to the callee function, which will continue on with its own code.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Sorry about that. I think I've gone overboard with the /*comments*/. The color coding makes it a lot more readable, but pasting it somewhere else makes it look terrible. I'll try to fix it up.

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Ok I went through and tried to make it easier to read. I still can't figure out why the program isn't going through its sequence like normal. After the first value is entered for "v", it runs through GES (which it should, but I don't know if it's using CONT to count the attempts or not) but then the program closes as soon as the correct number is found. I don't understand how choosing the correct number is causing the program to shut down... It should enter CONT and display the information there, then run through the "restart block" at the bottom of the program. Shouldn't it?

    Definitions:
    Code:
    #include <stdio.h>
    #include "game.h"
    
    
    
    
    /*Define function print_head.
    print_head- Display header.
    */
    
    void print_head(void)
    {
    	printf("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
    	
    	printf("\nProgram by: ");
    
    	printf(NAME);
    
    	printf("\nDate: ");
    
    	printf(DATE);
    
    	printf("\n");
    	
    	printf(TITLE);
    	
    	printf("\n");
    
    	printf("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
    	
    	printf("\n");
    
    return;
    }
    
    
    
    
    /*
    Define function CONT. 
    CONT- Gather the number of guesses and evaluate the user.
    */
    
    int CONT(void)
    {
    	++r; /*Count the number of guesses and store the tally at "r"*/
    
    	if(v == z) /*Once the user has guessed the correct number, begin evaluation.*/
    	{
    		
    		printf("Excellent! You guessed the number!"); /*Inform user of success.*/
    
    		printf("\nIt took you %i tries to guess correctly.\n", r); /*Display counted attempts.*/
    
    
    		if(r > 10)
    		{
    			printf("You should be able to do better!\n");
    		}
    		if(r == 10)
    		{
    			printf("Aha! You know the secret!\n");
    		}
    		if(r < 10)
    		{
    			printf("Either you know the secret or you got lucky!\n");
    		}
    
    	} /*Close the evaluation.*/
    
    return 0;
    }
    
    
    
    
    /*Define function GES.
    GES- Run the user's input against the random number generated.
    */
    
    int GES(void)
    {
    
    	if(v > z)
    	{
    		printf("Too high, try again.\n");
    	}
    	if(v < z)
    	{
    		printf("Too low, try again.\n");
    	}
    
    return 0;
    }
    Body:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "game.h"
    
    
    int z;
    int v = 0;
    int r = 0;
    char go = 'y';
    
    
    
    int main (void)
    {
    
    
    /*FUNCTION 1- Display header.*/
    print_head(); /*Call header function from game.h*/
    
    
    
    /*FUNCTION 2- Generate number and ask for guess.*/
    while(go == 'y' || go == 'Y')
    {
    
    	z = 1 + rand() %1000; /*Generate random number between 1 and 1000.*/
    
    	/*Read guess from the user and assign to v.*/
    	printf("I have a number between 1 and 1000.\nCan you guess my number?\n\nPlease type your first guess:");
    	
    	go = 't'; /*Force block to stop loop.*/
    	
    } /*END F2*/
    
    
    
    while(go == 't') /*Begin main body of game.*/
    { 
    	
    	scanf("%i", &v); /*Read the user's guess and store at v.*/
    
    
    
    /*FUNCTION 3- Call CONT from game.h. CONT will count the attempts and check for accuracy.*/
    
    CONT();
    
    
    
    /*FUNCTION 4- Call GES from game.h IF the guess is incorrect. GES will direct the user to the correct number.*/
    
    if(v != z)
    {
    	GES();
    } /*END F4*/
    
    
    
    /*FUNCTION 5- Ask the user to restart the game or end the program.*/
    
    if(v == z) 
    {
    	printf("Would you like to play again?(y/n):");
    	scanf("%c", &go);
    
    	if(go == 'y' || go == 'Y')
    	{
    		v = 0;
    		r = 0;
    	}
    
    } /*End F5*/
    
    
    } /*Close the game's main body.*/
    
    
    
    return 0;
    } /*Close the main function.*/

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reply to crack code
    By quack in forum C++ Programming
    Replies: 10
    Last Post: 11-04-2003, 10:25 PM
  2. Crack This!
    By CumQuaT in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 09-10-2003, 11:02 AM

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