Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Question: calling functions, using func. arguments, locals & globals, funcs.

ID: 669537 • Letter: C

Question

C++ Question: calling functions, using func. arguments, locals & globals, funcs. in "Menu driven" programs

Have to make a game that asks user to guess a secret card (from a standard 52 playing card deck). The game then repeatedly prompts the user for a guess. For each user's guess after the first one, the game tells the user if the guess was Warmer (i.e., closer than the previous guess) or Colder before prompting for the next guess. (For the first guess, it is permissible to tell the user Warmer.) When the user correctly guesses the number (where the game outputs Win), the game asks the user if we are done. Regardless of how the user answers, the program ends.

Need to make & test a program called guess-card. The program will include a main body consisting of the conversation loop with it's six function calls. This will require six function definitions, one for each function called in the conv. loop. Cleanup will merely ask if the user is finished, and return 0 regardless of the answer, which allows the program to keep a doxbox open until the user answers.

It is permissible to define and call other function from the six conv. functions, if desired (e.g., a prompt function).

OUTPUT:

I've chosen a secret card that you have to guess. Guess rank & suit: 4 C

Warmer

Guess rank & suit: 8 H

Colder

Guess rank & suit: 10 D

Warmer

Guess rank & suit: 2 D

Warmer

Guess rank & suit: 8 C

Colder Guess rank & suit: 14 C

Win

Are we done? (Y or N): Y

#include <cstdlib> //srand

#include <ctime> //time

#include <cmath>

#include <iostream>

using namespace std;

int suit;

int rank;

int g_secret_rank;

int g_guess;

int g_prev_delta = 1000;

int endchk; //conv. loop test

int main()

{

setup();

Hello();

do{ //conv. loop

listen();

respond();

} while (endchk);

cleanup();

system("pause");

return 0;

}

// guess secret number [1...13] & suit: 'S', 'H', 'D', 'C'

void setup()

{

cout << "Setup. ";

srand(static_cast<unsigned int>(time(0)));

int num = rand() % 52;

int rank;

rank = 1 + (rand() % 13); // get rank num [1..13]

int letter= rand() % 4;

switch (letter)

{

case 0: suit = 'C'; break;

case 1: suit = 'D'; break;

case 2: suit = 'H'; break;

case 3: suit = 'S'; break;

}

if ('C' == suit)

{

if (2 == rank)

{ g_secret_rank = 0; }

else if (3 == rank)

{ g_secret_rank = 1; }

else if (4 == rank)

{ g_secret_rank = 2; }

else if (5 == rank)

{ g_secret_rank = 3; }

}

}

// prompt first guess

void Hello()

{

cout << "I've chosen a secret card that you have to guess. ";

cout << "Guess rank & suit> ";

prompt();

}

void prompt()

{

}

// get user's guess

void listen()

{

int gx;

cin >> gx;

g_guess = gx;

}

//respond to user's guess

void respond()

{

cout << "Respond. ";

cout << "Secret = " << g_secret_rank << endl;

if (delta < g_prev_delta)

{ cout << "getting warmer. ";}

if (delta > g_prev_delta)

{ cout << "getting colder. ";}

g_prev_delta = delta;

prompt();

endchk = delta;

}

void cleanup()

{

}

Need help fixing it up and cleaning it up. Not sure whether I'm doing the number part right or the suit properly.
Also how do I add another loop around the conv. loop, which uses the Cleanup answer to run the conv. loop more than one time, picking a new secret, etc.

Explanation / Answer

here is the code:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

// ****creating class****
class Card
{

private:
int value; // to hold value of
char suit; // to hold value of
public:
void setValue(int v); // inline function to
{value= v;}
void setSuit(char s); // inline function to
{suit = s;}


int getValue(); // inline function to
{return value}
char getSuit(); // inline function to
{return suit;}

void display();

};




void Card::display()
{



switch(getSuit())
{
case'h':
case'H': cout << " The suit of your card is hearts ";
break;
case's':
case'S': cout << " The suite of your cars is spades ";
break;
case'd':
case'D': cout << " The suite of your card is diamonds ";
break;
case'c':
case'C': cout << " The suite of your card is clubs ";
break;
}




cout << "The value of your card is: " << getValue() << " ";

}


//****************** MAIN ***************

int main()
{
Card idk;
int num;
char letter;

cout << "Hello... What is the first letter of the suit of your card? ";
cin >> letter;


//while ( char != 'h' || char != 'H' || char != 'c' || char != 'C' || char != 'd' || char != 'D' || char != 's' || char 'S' )
//{
//"You have entered an invalid letter... Please try again. ";
//cin >> letter;
//}


idk.setSuit(letter);


cout << "Now please enter the value of your card... ";
cout << "Remember, Ace is 1, Jack is 11, Queen is 12, and King is 13: ";
cin >> num;



while (num < 1 || num > 13)
{
cout << "You can't follow instructions... ";
cout << "Please enter a correct number: ";
cin >> num;
}




idk.setValue(num);

idk.display();


system("pause");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote