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

Did pretty much all of it but just needs few things. Not sure how to make it so

ID: 669776 • Letter: D

Question

Did pretty much all of it but just needs few things. Not sure how to make it so that it says if the rank and suit are correct. It compiles like this but something is just wrong I don't know what exactly.

C++: 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 should look like this:

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

CODE:

#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 g_prev_guess;

int delta;

int endchk; //conv. loop test

void respond(), cleanup(), listen(), prompt(), Hello(), setup();

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() {

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;

}

g_secret_rank = rank;

g_prev_delta = g_secret_rank;

g_prev_guess = suit;

}

int card_to_num(int rank, char suit)

{

int card_num = 1;

if ((2 == rank) && ('C' == suit))

{ card_num = 1;}

else if ((3 == rank) && ('C' == suit))

{card_num = 2;}

else if ((4 == rank) && ('C' == suit))

{card_num = 3;}

else if ((5 == rank) && ('C' == suit))

{card_num = 4;}

else if ((6 == rank) && ('C' == suit))

{card_num = 5;}

else if ((7 == rank) && ('C' == suit))

{card_num = 6;}

else if ((8 == rank) && ('C' == suit))

{card_num = 7;}

else if ((9 == rank) && ('C' == suit))

{card_num = 8;}

else if ((10 == rank) && ('C' == suit))

{card_num = 9;}

else if ((11 == rank) && ('C' == suit))

{card_num = 10;}

else if ((12 == rank) && ('C' == suit))

{card_num = 11;}

else if ((13 == rank) && ('C' == suit))

{card_num = 12;}

else if ((1 == rank) && ('C' == suit))

{card_num = 13;}

else if ((2 == rank) && ('D' == suit))

{card_num = 14;}

else if ((3 == rank) && ('D' == suit))

{card_num = 15;}

else if ((4 == rank) && ('D' == suit))

{card_num = 16;}

else if ((5 == rank) && ('D' == suit))

{card_num = 17;}

else if ((6 == rank) && ('D' == suit))

{card_num = 18;}

else if ((7 == rank) && ('D' == suit))

{card_num = 19;}

else if ((8 == rank) && ('D' == suit))

{card_num = 20;}

else if ((9 == rank) && ('D' == suit))

{card_num = 21;}

else if ((10 == rank) && ('D' == suit))

{card_num = 22;}

else if ((11 == rank) && ('D' == suit))

{card_num = 23;}

else if ((12 == rank) && ('D' == suit))

{card_num = 24;}

else if ((13 == rank) && ('D' == suit))

{card_num = 25;}

else if ((14 == rank) && ('D' == suit))

{card_num = 26;}

else if ((2 == rank) && ('H' == suit))

{card_num = 27;}

else if ((3 == rank) && ('H' == suit))

{card_num = 28;}

else if ((4 == rank) && ('H' == suit))

{card_num = 29;}

else if ((5 == rank) && ('H' == suit))

{card_num = 30;}

else if ((6 == rank) && ('H' == suit))

{card_num = 31;}

else if ((7 == rank) && ('H' == suit))

{card_num = 32;}

else if ((8 == rank) && ('H' == suit))

{card_num = 33;}

else if ((9 == rank) && ('H' == suit))

{card_num = 34;}

else if ((10 == rank) && ('H' == suit))

{card_num = 35;}

else if ((11 == rank) && ('H' == suit))

{card_num = 36;}

else if ((12 == rank) && ('H' == suit))

{card_num = 37;}

else if ((13 == rank) && ('H' == suit))

{card_num = 38;}

else if ((1 == rank) && ('H' == suit))

{card_num = 39;}

else if ((2 == rank) && ('S' == suit))

{card_num = 40;}

else if ((3 == rank) && ('S' == suit))

{card_num = 41;}

else if ((4 == rank) && ('S' == suit))

{card_num = 42;}

else if ((5 == rank) && ('S' == suit))

{card_num = 43;}

else if ((6 == rank) && ('S' == suit))

{card_num = 44;}

else if ((7 == rank) && ('S' == suit))

{card_num = 45;}

else if ((8 == rank) && ('S' == suit))

{card_num = 46;}

else if ((9 == rank) && ('S' == suit))

{card_num = 47;}

else if ((10 == rank) && ('S' == suit))

{card_num = 48;}

else if ((11 == rank) && ('S' == suit))

{card_num = 49;}

else if ((12 == rank) && ('S' == suit))

{card_num = 50;}

else if ((13 == rank) && ('S' == suit))

{card_num = 51;}

else if ((1 == rank) && ('S' == suit))

{card_num = 52;}

return card_num;

}

// prompt first guess

void Hello() {

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

prompt();

}

void prompt() {

}

// get user's guess

void listen() {

cout << "Guess rank & suit: ";

char gx;

cin >> delta;

cin >> gx;

g_guess = gx;

}

//respond to user's guess

void respond() {

if (delta == g_secret_rank && g_guess == suit) {

cout << "Win ";

endchk = 0;

}

else if (g_guess < g_prev_guess || delta < g_prev_delta) {

cout << "Warmer. ";

endchk = 1;

}

else if (g_guess > g_prev_guess || delta > g_prev_delta) {

cout << "Colder. ";

endchk = 1;

}

prompt();

}

void cleanup()

{

}

Explanation / Answer

Answer:

The code that is highlighted in bold letters is the modified code.

Program code to copy:

// GuessACardGame.cpp : Defines the entry point for the console application.

#include "stdafx.h"

#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 g_prev_guess;

int delta;

int endchk; //conv. loop test

void respond(), listen(), prompt(), Hello(), setup();

int cleanup();

int main()

{   

     do

     {

          setup();

          Hello();

          do

          {

              //conv. loop

              listen();

              respond();

          } while (endchk);

     }

     while(cleanup());

     system("pause");

     return 0;

}

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

void 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;

     }

     g_secret_rank = rank;

     g_prev_delta = g_secret_rank;

     g_prev_guess = suit;

}

int card_to_num(int rank, char suit)

{

     int card_num = 1;

     if ((2 == rank) && ('C' == suit))

     { card_num = 1;}

     else if ((3 == rank) && ('C' == suit))

     {card_num = 2;}

     else if ((4 == rank) && ('C' == suit))

     {card_num = 3;}

     else if ((5 == rank) && ('C' == suit))

     {card_num = 4;}

     else if ((6 == rank) && ('C' == suit))

     {card_num = 5;}

     else if ((7 == rank) && ('C' == suit))

     {card_num = 6;}

     else if ((8 == rank) && ('C' == suit))

     {card_num = 7;}

     else if ((9 == rank) && ('C' == suit))

     {card_num = 8;}

     else if ((10 == rank) && ('C' == suit))

     {card_num = 9;}

     else if ((11 == rank) && ('C' == suit))

     {card_num = 10;}

     else if ((12 == rank) && ('C' == suit))

     {card_num = 11;}

     else if ((13 == rank) && ('C' == suit))

     {card_num = 12;}

     else if ((1 == rank) && ('C' == suit))

     {card_num = 13;}

     else if ((2 == rank) && ('D' == suit))

     {card_num = 14;}

     else if ((3 == rank) && ('D' == suit))

     {card_num = 15;}

     else if ((4 == rank) && ('D' == suit))

     {card_num = 16;}

     else if ((5 == rank) && ('D' == suit))

     {card_num = 17;}

     else if ((6 == rank) && ('D' == suit))

     {card_num = 18;}

     else if ((7 == rank) && ('D' == suit))

     {card_num = 19;}

     else if ((8 == rank) && ('D' == suit))

     {card_num = 20;}

     else if ((9 == rank) && ('D' == suit))

     {card_num = 21;}

     else if ((10 == rank) && ('D' == suit))

     {card_num = 22;}

     else if ((11 == rank) && ('D' == suit))

     {card_num = 23;}

     else if ((12 == rank) && ('D' == suit))

     {card_num = 24;}

     else if ((13 == rank) && ('D' == suit))

     {card_num = 25;}

     else if ((14 == rank) && ('D' == suit))

     {card_num = 26;}

     else if ((2 == rank) && ('H' == suit))

     {card_num = 27;}

     else if ((3 == rank) && ('H' == suit))

     {card_num = 28;}

     else if ((4 == rank) && ('H' == suit))

     {card_num = 29;}

     else if ((5 == rank) && ('H' == suit))

     {card_num = 30;}

     else if ((6 == rank) && ('H' == suit))

     {card_num = 31;}

     else if ((7 == rank) && ('H' == suit))

     {card_num = 32;}

     else if ((8 == rank) && ('H' == suit))

     {card_num = 33;}

     else if ((9 == rank) && ('H' == suit))

     {card_num = 34;}

     else if ((10 == rank) && ('H' == suit))

     {card_num = 35;}

     else if ((11 == rank) && ('H' == suit))

     {card_num = 36;}

     else if ((12 == rank) && ('H' == suit))

     {card_num = 37;}

     else if ((13 == rank) && ('H' == suit))

     {card_num = 38;}

     else if ((1 == rank) && ('H' == suit))

     {card_num = 39;}

     else if ((2 == rank) && ('S' == suit))

     {card_num = 40;}

     else if ((3 == rank) && ('S' == suit))

     {card_num = 41;}

     else if ((4 == rank) && ('S' == suit))

     {card_num = 42;}

     else if ((5 == rank) && ('S' == suit))

     {card_num = 43;}

     else if ((6 == rank) && ('S' == suit))

     {card_num = 44;}

     else if ((7 == rank) && ('S' == suit))

     {card_num = 45;}

     else if ((8 == rank) && ('S' == suit))

     {card_num = 46;}

     else if ((9 == rank) && ('S' == suit))

     {card_num = 47;}

     else if ((10 == rank) && ('S' == suit))

     {card_num = 48;}

     else if ((11 == rank) && ('S' == suit))

     {card_num = 49;}

     else if ((12 == rank) && ('S' == suit))

     {card_num = 50;}

     else if ((13 == rank) && ('S' == suit))

     {card_num = 51;}

     else if ((1 == rank) && ('S' == suit))

     {card_num = 52;}

     return card_num;

}

// prompt first guess

void Hello()

{

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

     prompt();

}

void prompt()

{

     cout<<endl;

}

// get user's guess

void listen()

{

     cout << "Guess rank & suit: ";

     char gx;

     cin >> delta;

     cin >> gx;

     g_guess = gx;

}

//respond to user's guess

void respond()

{

     if (delta == g_secret_rank && g_guess == suit)

     {

          cout << "Win ";

          endchk = 0;

     }

     else if (g_guess < g_prev_guess || delta < g_prev_delta)

     {

          cout << "Warmer. ";

          endchk = 1;

     }

     else if (g_guess > g_prev_guess || delta > g_prev_delta)

     {

          cout << "Colder. ";

          endchk = 1;

     }

     prompt();

}

int cleanup()

{

     char choice;

     cout<<"Are we done? (Y or N): ";

     cin>>choice;

     if (choice == 'Y' || choice == 'y')

          return 1;

     else

          return 0;

}

Sample Output:

Sample output:

I've chosen a secret card that you have to guess.

Guess rank & suit: 13 D

Warmer.

Guess rank & suit: 13 S

Colder.

Guess rank & suit: 12 S

Colder.

Guess rank & suit: 6

S

Colder.

Guess rank & suit: 5 S

Colder.

Guess rank & suit: 2 S

Win

Are we done? (Y or N): y

I've chosen a secret card that you have to guess.

Guess rank & suit: 2 S

Warmer.

Guess rank & suit: 5 S

Warmer.

Guess rank & suit: 13 S

Colder.

Guess rank & suit: 10 S

Colder.

Guess rank & suit: 11 S

Colder.

Guess rank & suit: 7 S

Warmer.

Guess rank & suit: 8 S

Colder.

Guess rank & suit: 7 D

Warmer.

Guess rank & suit: 7 C

Warmer.

Guess rank & suit: 6 S

Warmer.

Guess rank & suit: 9 S

Colder.

Guess rank & suit: 9 D

Colder.

Guess rank & suit: 9 S

Colder.

Guess rank & suit: 9 H

Colder.

Guess rank & suit: 9 C

Warmer.

Guess rank & suit: 10 S

Colder.

Guess rank & suit: 8 S

Colder.

Guess rank & suit: 7 S

Warmer.

Guess rank & suit: 8 C

Warmer.

Guess rank & suit: 8 S

Colder.

Guess rank & suit: 9 S

Colder.

Guess rank & suit: 7 S

Warmer.

Guess rank & suit: 8 D

Win

Are we done? (Y or N): n

Press any key to continue . . .

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