![]() |
|
#1
|
|||
|
|||
Functions, loops, counters, help please.CPP / C++ / C Code:
Okay, thanks for your time to begin with. I am currently trying to make a Rock paper scissors program, that will loop through until told to exit (4), will keep a win counter (which I realize that I do not have included yet). I also have to validate the user input. I thought a do while might work but I am not positive. I know I am currently missing something simple but can not come up with the solution. Again thanks for any and all help. |
|||
|
#2
|
|||
|
|||
Re: Functions, loops, counters, help please.Quote:
So: What happened when you tried to compile? Didn't work did it? Your executable statements must all reside inside function blocks. The blocks can not be nested. That is, each function block must be complete and can not contain other function blocks. I hate to repeat myself, but all of your executable code must reside in a function block. You always have a main() function, and you have a good start for a couple of other functions that can be useful. Here's my suggestion: First, ya gotta have a plan! Really. I strongly recommend that before you even think of writing code or figuring out what kind of loop you need, make a plan and write it down! Scribble out a few lines that show how the program will play some games by getting user input and computer input values. One plan might go something like the following. You have a main program that consists of some kind of loop that gets the user play and the machine's play. It calculates the winner and increments the counter for the winner. The loop terminates when the user says so. So, here's a plan: 1. Make a function that gets the user's choice (return an integer value 1, 2, 3, 4, where the values represent Rock, Paper, Scissors, and Exit). 2. Make a function that gets the machine's choice (return an integer value 1, 2, or 3 where the values represent Rock, Paper, and Scissors). 3. Make a function that takes user choice and machine choice as inputs and returns a value of 1 if the user wins and a value of 2 if the machine wins. (Or whatever values you would like: maybe a 0 for machine win and 1 for user win. Anything you want. See Footnote) You already have a couple of good functions for welcoming the user and showing the user what choices are possible. Here's an example of how you might make a main() function that plays the game along the lines of the above plan: Code:
The "Loop Forever" thing can be simply CPP / C++ / C Code:
Some programmers don't like "break" statements, so they might prefer something like the following. In particular, experienced C and C++ programmers like to show off the feature of the language that lets statements do more than one thing: The while condition gets the user input and uses that value to decide whether to go into the loop: Code:
Now, your plan doesn't have to be like my suggestions. For example my so-called "ShowMenu()" function could be called by "GetUserInput()" rather than by a separate call from main() since they always go together. I would never name the function "ShowMenu" if, in fact its purpose was to show the menu and to get the user input. I might very well make my function "GetUserInput" show the menu (probably by coding the menu inside the GetUserInput function rather than calling a void function that merely prints out the choices. Unless, that is, I wanted to internationalize the game by having a selection of "ShowMenu" functions for various languages that I could compile into the program. Anyhow, I think that the important thing is to have a plan in your head (and on paper) before the code comes pouring out. Regards, Dave Footnote: When it comes to making and naming functions, try to create something that makes sense to you and something that is easy to remember for future programmers who might want to debug and/or enhance the code. For example, I might create the grading function like this: CPP / C++ / C Code:
Then in the main() function, after getting the players' choices, you would have something that is self-documenting: CPP / C++ / C Code:
Now, anyone can tell at a glance what you have in mind with those statements. (Or so it seems to me.) Last edited by davekw7x : 22-Oct-2009 at 18:12.
|
|
#3
|
|||
|
|||
Re: Functions, loops, counters, help please.I would once again like to say many thanks, I currently do not have time to go over all aspects of ur advice. I will however do so tomorrow after work, thank you so much for your time and help!
|
|
#4
|
|||
|
|||
Re: Functions, loops, counters, help please.CPP / C++ / C Code:
Thanks again Dave for your help, this was all the further I was able to take the program with what you gave me. I am still struggling on figuring out how to get that win counter implanted in there, I realize that it still is not declared for useage anywhere, but that is because I am not sure where to declare it. Also I seem to be having a problem with the continual looping, it executes once then stops. |
|
#5
|
|||
|
|||
Re: Functions, loops, counters, help please.Quote:
I'll take this one first. Your program doesn't have the big loop that continues playing until the user says to exit. Using one of my previous examples without changing much in your program, it could go something like this: CPP / C++ / C Code:
Quote:
Set counters to zero before you begin the play loop. At each play, after you calculate the winner, increment the appropriate counter. Since you have the calculations spread out over the entire loop, it could go something like this: CPP / C++ / C Code:
Regards, Dave |
|
#6
|
|||
|
|||
Re: Functions, loops, counters, help please.Once again dave thank you. Combined with what I've learned today and the assistance you've provided I believe I have all I need to complete this program. I will post the completed program when finished. I learned the proper way to document the counter today which I see that you appear to have done it the same way.
Thanks Score_Reaper |
|
#7
|
|||
|
|||
Re: Functions, loops, counters, help please.Well I couldn't get it to work in that form dave even after I spent all that time on it. So I decided to try the switch method and see if I would fair any better but to no avail, This is what I am currently stuck with. I either get and infinite loop, or just the display of the letter I insert.
The goal of this program is to allow the user to play as many times as they want while keeping a counter. When the user Presses "q or Q" to quit, then I am wanting it to display the number of, wins for player, computer, and ties. As well as the overall winner. But I am stumped on something easy I am sure. *Side note* functions must be used and nothing can be placed in main except the callers (prototypes I think they are called). CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
Re: Functions, loops, counters, help please.CPP / C++ / C Code:
Finally finished it and got it working, this was writting and compiled in Dev-C++ so anyone who has questions feel free to ask. |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Display the graphs of functions | reedz | C++ Forum | 1 | 25-Feb-2008 21:30 |
| Help with some functions | baka_yaro | C++ Forum | 3 | 14-Mar-2007 06:56 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| Functions and Classes - Where did I go wrong? | redmage | C++ Forum | 5 | 10-Apr-2005 19:31 |
| conflict between printf and stdarg.h va functions | mirizar | C Programming Language | 3 | 12-Jul-2004 09:11 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The