Originally Posted by
lloewe
Well it seems you're kind of missing one point of using functions, i.e. that you can call them repeatedly with different parameters.
So you would create a function that takes the 4 cards the user picked as parameters and generates the appropriate response - this usually requires if/switch statements.
Then you can call that function repeatedly (e.g. from a loop) and use the input the user provided as parameters.
A (contrived) simple example:
Code:
// this is a function that takes two int parameters multiplying and dividing them (if possible) and displays the result
void MulDivNumbers(int a, int b)
{
cout << a << " times " << b << " equals " << a*b << endl;
if (b == 0)
cout << "Cannot divide by zero" << endl;
else
cout << a << " divided by " << b << " yields " << a/b << " with " << a%b << " remaining" << endl;
cout << endl;
}
int main()
{
MulDivNumbers(14, 4); // call the function
MulDivNumbers(12, 0); // call it again
// and now in a loop
for (int i = 0; i < 5; i++)
MulDivNumbers(30, i);
// with user input
int input;
cout << "Enter a number :";
cin >> input;
MulDivNumbers(30, input);
return 0;
}
Originally Posted by
Pejo
That's a good reply above. I don't fully understand your constraints, so I ask forgiveness. When you say loops, are you restricted to just For loops or are you able to do While loops as well? When you get these problems, break them up to make it easier to manage.
Memory gäme with 4 cards, made up of two symbols. User guesses two cards, and you see if they match. Keep going until game is done. Flow diagram looks like this:
New Game - Generate new set of cards. I.e. assign values for card 1 - 4 with values.
Ask user for two guesses. Store in guess 1 and 2
Check if match. Use above code as a hint for this.
If match, mark cards as matched and check if game is over
Loop back until ask for guesses.
For me, if you can use While, set a boolean to gameFinished = false and do while gameFinished is false.
I'm sorry, I wasn't very clear with my description of the goal of the program there. Lemme try again:
-Program starts and prints out a welcome, rules of the game, and instructions
-User has 4 cards to choose from. There are 2 pairs of $ and 2 pairs of @. User must guess the pairs one at a time and keep guessing until they have all pairs.
-I cin the user's first guess and reflect that change by flipping the card they chose.
-I cin the user's second guess and check of it's a match:
-If it is, I let them keep guessing and keep their guessed cards flipped over.
-If it isn't, all cards are flipped to default positions and they begin guessing again.
-The game ends once all cards are guessed.
I've learned about if/else and switch statements, and have learned about While, Counter While, Flag While, Sentinel, Do/While and For loops. I don't think my understanding of loops is the issue, I feel pretty comfortable using them or knowing what to use them for.
What I'm having a tough time with is reflecting the user's choices. Right now I'm doing something like this:
Code:
//I have a terrible looking but functional void here to print out a nice looking group of cards. 2x2 grid.
void printCards (char card1, char card2, char card3, char card4)
#include <iostream>
#include <iomanip>
using namespace std;
//Just declaring these symbols as constants to expose myself to more constant usage.
const char front = '#';
const char symbol1 = '$';
const char symbol2 = '@';
const int oneMatch = 1;
const int twoMatch = 2;
int main()
{
//My Declarations
int guess1 = 0;
int guess2 = 0;
int matches = 0;
int guessCount = 0;
printCards(front, front, front, front);
cout << endl;
cout << "Total guesses: " << guessCount << "." << endl;
cin >> guess1;
system("CLS");
while (oneMatch > matches);
{
if (guess1 = 1) //If the user guesses card 1, flip the first card, increment our guess counter (just for a grand total at the end) and cin their second guess
{
printCards(symbol1, front, front, front); //I don't know why this is left-aligned in the code here but it's not in VS.
cout << endl;
guessCount++;
cout << "Total guesses: " << guessCount << "." << endl;
cout << "Let's see if you can get a match, pick another card." << endl << endl;
cin >> guess2;
system("CLS");
if (guess2 = 4) //If their second guess would be a pair based on the first one, increment our guess counter and increment our match counter, which would end the While loop.
{
printCards(symbol1, front, front, symbol1);
cout << endl;
cout << "Yooo you matched two $ cards. Noice. Let's see if you can match the other two cards." << endl;
guessCount++;
matches++;
}
else
{
printCards(front, front, front, front); //Otherwise I tell 'em they didn't get a match, matches isn't incremented, and the while loop repeats.
cout << endl;
cout << "Hoooo sorry, no match for you. Try again though." << endl;
guessCount++;
}
}
else if (guess1 = 2) //These would basically be the exact same as the code above. Just condensed them for this thread.
else if (guess1 = 3)
else if (guess1 = 4)
else //This is just a "Invalid entry, try again" thing.
}
system("pause");
return 0;
}
The rest of my code so far is "well what if guess 1 = 2? what is guess 1 = 3? = 4?" and continuing the thought process above to satisfy all answers.
I can't help but feel that this is the wrong way of doing this. My profs are unreachable atm and I can't really do much else except try more code or ask people. I can get this done this way, I did the entire thing without any loops to begin with, I'm jut looking for the best way.