PRS Help

This is a discussion on PRS Help within the C Programming forums, part of the General Programming Boards category; I know ya'll have probably seen this one a thousand times but would appreciate some help. Here's the code: Header ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    Oklahoma City
    Posts
    55

    PRS Help

    I know ya'll have probably seen this one a thousand times but would appreciate some help. Here's the code:

    Header File:

    Code:
    /* The game of paper, rock, scissors. */
    
    #include <ctype.h>				/* for isspace() */
    #include <stdio.h>				/* for printf(), etc */
    #include <stdlib.h>				/* for rand() and srand() */
    #include <time.h>				/* for time() */
    
    enum p_r_s1	{paper, rock, scissors,
    			game, help, instructions, quit};
    
    enum outcome {win, lose, tie, error};
    
    typedef		enum p_r_s1		p_r_s1;
    typedef		enum outcome	outcome;
    
    outcome		compare(p_r_s1 player_choice, p_r_s1 machine_choice);
    void		prn_final_status(int win_cnt, int lose_cnt);
    void		prn_game_status(int win_cnt, int lose_cnt, int tie_cnt);
    
    void		prn_help(void);
    void		prn_instructions(void);
    void		report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr);
    void		report(outcome result, char *player_choice, char *machine_choice);
    p_r_s1		selection_by_machine(void);
    p_r_s1		selection_by_player(void);
    Main File:

    Code:
    #include "p_r_s1.h"
    
    int main(void)
    {
       int       win_cnt = 0, lose_cnt = 0, tie_cnt = 0;
       outcome   result;
       p_r_s1    player_choice, machine_choice;
    
       srand(time(NULL));              /* seed the random number generator*/
       prn_instructions();
       while ((player_choice = selection_by_player()) != quit)
          switch (player_choice) {
          case paper:
          case rock:
          case scissors:
             machine_choice = selection_by_machine();
             result = compare(player_choice, machine_choice);
             report (result, &win_cnt, &lose_cnt, &tie_cnt);
    		 break;
          case game:
             prn_game_status(win_cnt, lose_cnt, tie_cnt);
             break;
          case instructions:
             prn_instructions();
             break;
          case help:
             prn_help();
             break;
          default:
             printf("\nPROGRAMMER ERROR: Cannot get to here!\n\n");
             exit(1);
          }
       prn_game_status(win_cnt, lose_cnt, tie_cnt);
       prn_final_status(win_cnt, lose_cnt);
       return 0;
    }
    Compare File:

    Code:
    #include "p_r_s1.h"
    
    outcome compare(p_r_s1 player_choice, p_r_s1 machine_choice)
    {
    	outcome		result;
    
    	if (player_choice == machine_choice)
    		return tie;
    	switch (player_choice) {
    		case paper:
    			result = (machine_choice == rock) ? win : lose;
    			break;
    		case rock:
    			result = (machine_choice == scissors) ? win : lose;
    			break;
    		case scissors:
    			result = (machine_choice == paper) ? win : lose;
    			break;
    		default:
    			printf("PROGRAMMING ERROR: Unexpected choice!\n\n");
    			exit(1);
    	}
    	return result;
    }
    Print File:

    Code:
    #include "p_r_s1.h"
    
    void prn_final_status(int win_cnt, int lose_cnt)
    {
    	if (win_cnt > lose_cnt)
    		printf("CONGRATULATIONS - You won!\n\n");
    	else if (win_cnt == lose_cnt)
    		printf("A DRAW - You tied!\n\n");
    	else
    		printf("SORRY - You lost!\n\n");
    }
    
    void prn_game_status(int win_cnt, int lose_cnt, int tie_cnt)
    {
    	printf("\n%s\n%s%4d\n%s%4d\n%s%4d\n%s%4d\n\n",
    		"GAME STATUS:",
    		"	Win:	", win_cnt,
    		"	Lose:	", lose_cnt,
    		"	Tie:	", tie_cnt,
    		"	Total:	", win_cnt + lose_cnt + tie_cnt);
    }
    
    void prn_help(void)
    {
    	printf("\n%s\n",
    		"The following characters can be used for input:\n"
    		"	p	for paper\n"
    		"	r	for rock\n"
    		"	s	for scissors\n"
    		"	g	print the game status\n"
    		"	h	help, print this list\n"
    		"	i	Reprint the instructions\n"
    		"	q	quit this game\n");
    }
    
    void prn_instructions(void)
    {
    	printf("\n%s\n",
    		"Paper, Rock, Scissors:\n"
    		"\n"
    		"In this game\n"
    		"\n"
    		"	p is for \"paper\"\n"
    		"	r is for \"rock\"\n"
    		"	s is for \"scissors\"\n"
    		"\n"
    		"Both the player and the machine will choose one\n"
    		"of p, r, or s. If the two choices are the same,\n"
    		"then the game is a tie. Otherwise:\n"
    		"\n"
    		"	\"paper covers the rock\"		(a win for paper)\n"
    		"	\"rock crushes the scissors\"	(a win for rock)\n"
    		"	\"scissors cut through paper\"	(a win for scissors)\n"
    		"\n"
    		"There are other allowable inputs:\n"
    		"\n"
    		"	g	for game status		(print number of wins)\n"
    		"	h	for help			(print short instructions)\n"
    		"	i	for instructions	(print these instructions)\n"
    		"	q	for quit			(quit the game)\n"
    		"\n"
    		"This game is played repeatedly until q is entered.\n"
    		"\n"
    		"Good Luck!\n");
    }
    Report File:

    Code:
    #include "p_r_s1.h"
    
    void report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int
    *tie_cnt_ptr, p_r_s1 player_choice, p_r_s1 machine_choice)
    { 
    	switch (result) {
      case win:
       ++*win_cnt_ptr;
       if (player_choice == paper)
        printf("%27sYou chose paper I chose rock.  You win.\n", "");
       else if (player_choice == rock)
        printf("%27sYou chose rock I chose scissors.  You win.\n", "");
       else if (player_choice == scissors)
        printf("%27sYou chose scissors I chose paper.  You win.\n", "");
       break;
      case lose:
       ++*lose_cnt_ptr;
       if (player_choice == paper)
        printf("%27sYou chose paper I chose scissors.  You lose.\n", "");
       else if (player_choice == rock)
        printf("%27sYou chose rock I chose paper.  You lose.\n", "");
       else if (player_choice == scissors)
        printf("%27sYou chose scissors I chose rock.  You lose.\n", "");
       break;
      case tie:
       ++*tie_cnt_ptr;
       if (player_choice == paper)
        printf("%27sYou chose paper I chose paper.  We tie.\n", "");
       else if (player_choice == rock)
        printf("%27sYou chose rock I chose rock.  We tie.\n", "");
       else if (player_choice == scissors)
        printf("%27sYou chose scissors I chose scissors.  We tie.\n", "");
       break;
      default:
       printf("PROGRAMMER ERROR: Unexpected result!\n\n");
       exit(1);
     
    	}
    }
    Select File:

    Code:
    #include "p_r_s1.h"
    
    p_r_s1	selection_by_machine(void)
    {
    	return ((p_r_s1) (rand() % 3));
    }
    p_r_s1	selection_by_player(void)
    {
    	char	c;
    	p_r_s1	player_choice;
    
    	printf("Input p, r, or s:   ");
    	scanf(" %c", &c);
    	switch (c) {
    		case 'p':
    			player_choice = paper;
    			break;
    		case 'r':
    			player_choice = rock;
    			break;
    		case 's':
    			player_choice = scissors;
    			break;
    		case 'g':
    			player_choice = game;
    			break;
    		case 'i':
    			player_choice = instructions;
    			break;
    		case 'q':
    			player_choice = quit;
    			break;
    		default:
    			player_choice = help;
    			break;
    	}
    	return player_choice;
    }
    I keep getting the following errors:

    main.cpp(9) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

    Linking...

    main.obj : error LNK2019: unresolved external symbol "void __cdecl report(enum outcome,int *,int *,int *)" (?report@@YAXW4outcome@@PAH11@Z) referenced in function _main

    Debug/p_r_s1.exe : fatal error LNK1120: 1 unresolved externals

    I'm told that the function prototype and definition don't match number of items and correct types in the header and main files but I can't see where, my eyes hurt looking for it

    Thanks
    Last edited by SpudNuts; 12-01-2004 at 08:28 PM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    [edit] you edited agian [/edit]
    Woop?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    Oklahoma City
    Posts
    55
    Quote Originally Posted by prog-bman
    [edit] you edited agian [/edit]
    1. I've just started C about 3 weeks ago, so chill. I'm sure your first code was such a star studded performance.
    2. p_r_s1 is the name of the source file.

    If this is your way of constructive criticism, please don't reply I'd rather not have your help.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Actully i was trying to help you but you edited so i decided not to. cause I had already typed 2 different responses because you edited twice. Anyways time to slam you with neg rep for being a jerk to me for trying to help you out.
    Woop?

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    srand(time(NULL));
    main.cpp(9) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
    This is telling you that the time function returns a time_t while srand expects an unsigned int. In this case you can use a cast.
    main.obj : error LNK2019: unresolved external symbol "void __cdecl report(enum outcome,int *,int *,int *)" (?report@@YAXW4outcome@@PAH11@Z) referenced in function _main
    This is telling you that the report function with this prototype:
    Code:
    void		report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr);
    and called from the main function is missing. You could ask whoever wrote the code for it or you could try to write it yourself.

    The .cpp on the end of the files should tell you that this code is C++ and belongs in the C++ forum.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My goodness. That's got to be one of the most convoluted rock-paper-scissors games I've ever seen!

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

  7. #7
    Registered User
    Join Date
    Dec 2004
    Location
    Oklahoma City
    Posts
    55
    prog-bman, if you call saying "This code is crap" is your way of helping someone, you need some professional treatment. Not everyone on here is an expert, most come to forums to learn by those that can help, Not to be slammed on the first post they have ever left on this forum. No one has an idea of what you posted since you were so quick to edit it out, but if anyone is interested, the email I received says it all. This part of the discussion is closed, your dismissed...lol

    anonytmouse, I wrote the code. Trying to learn C, the only program I have is Visual Studio.net and doesn't have the option to compile C, just C++.

    I think the problem is getting the prototype and function definition to match, i'm just not getting how to do that. I keep thinking that it's conflicting with:

    report (result, &win_cnt, &lose_cnt, &tie_cnt);

    in main.

    Thanks for your response.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    1. Match the prototype to the definition (delete the other prototypes for report):
    Code:
    void report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int
    *tie_cnt_ptr, p_r_s1 player_choice, p_r_s1 machine_choice)
    2. Change the call to match the definition:
    Code:
             report (result, &win_cnt, &lose_cnt, &tie_cnt, player_choice, machine_choice);
    Trying to learn C, the only program I have is Visual Studio.net and doesn't have the option to compile C, just C++.
    Files with the .c extension will be compiled as C.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    Oklahoma City
    Posts
    55
    Now that is what I call constructive help, thanks anonytmouse.

    I was getting another "new" error, but you put my mind on the right track and now the program works great.

    Thanks again.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1

    Finished File???

    I have been working the exact same program.
    What was your final code that made it work?
    TIA
    Remo

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    I know there's an epidemic of people not reading the damn rules before they post.
    Read the one about bumping threads, then decide on something better than a "me too" vacuous response to a long dead thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read/Write linked lists from/to file
    By XorpiZ in forum C Programming
    Replies: 35
    Last Post: 01-29-2005, 07:13 PM
  2. Multi-Threading and ADO
    By chandhru in forum Windows Programming
    Replies: 19
    Last Post: 05-15-2004, 11:53 AM

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