Question

In: Computer Science

I am working on a C++ program, where a user puts in a notation a playing...

I am working on a C++ program, where a user puts in a notation a playing card and the output is the full name of the card.(ex: KH = King of Hearts)

I have most of it working but I want to have an error come up when the user puts in the wrong info and then loop back to the original question. Am I setting it up wrong?

Pasted below is my code

#include<iostream>

#include<string>

using namespace std;

int main()

{

string suite;

string rank;

string card;

//Prompt user for abbreviation name card

cout << endl;

cout << "Please enter a playing card notations: ";

cin >> card;

cout << endl;

//length of card spot

if(card.length() == 2)

{

rank = card.substr(0,1);

suite = card.substr(1,1);

}

else if(card.length() == 3)

{

rank = card.substr(0,2);

suite = card.substr(2,1);

}

else

cout << "There is an Human input error" << endl;

return 1;

//if statement of suite and output of suites

if(rank == "1") {cout << "Ace ";}

if(rank == "2") {cout << "Two ";}

if(rank == "3") {cout << "Three ";}

if(rank == "4") {cout << "Four ";}

if(rank == "5") {cout << "Five ";}

if(rank == "6") {cout << "Six ";}

if(rank == "7") {cout << "Seven ";}

if(rank == "8") {cout << "Eight ";}

if(rank == "9") {cout << "Nine ";}

if(rank == "10") {cout << "Ten ";}

if(rank == "J") {cout << "Jack ";}

if(rank == "Q") {cout << "Queen ";}

if(rank == "K") {cout << "King ";}

if(rank == "A") {cout << "Ace ";}


//if stament of rank and output of rank

if(suite == "H") {cout << "of Hearts" << endl;}

if(suite == "D") {cout << "of Diamonds" << endl;}

if(suite == "S") {cout << "of Spades" << endl;}

if(suite == "C") {cout << "of Clubs " << endl;}

cout << endl;


return 0;

}

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string suite;

    string rank;

    string card;

    //Prompt user for abbreviation name card

    cout << endl;

    cout << "Please enter a playing card notations: ";

    cin >> card;

    cout << endl;

    //length of card spot

    if (card.length() == 2)

    {

        rank = card.substr(0, 1);

        suite = card.substr(1, 1);

    }

    else if (card.length() == 3)

    {

        rank = card.substr(0, 2);

        suite = card.substr(2, 1);

    }

    else

    {

        cout << "There is an Human input error" << endl;

        return 1;

    }

    //if statement of suite and output of suites

    if (rank == "1")

    {

        cout << "Ace ";

    }

    if (rank == "2")

    {

        cout << "Two ";

    }

    if (rank == "3")

    {

        cout << "Three ";

    }

    if (rank == "4")

    {

        cout << "Four ";

    }

    if (rank == "5")

    {

        cout << "Five ";

    }

    if (rank == "6")

    {

        cout << "Six ";

    }

    if (rank == "7")

    {

        cout << "Seven ";

    }

    if (rank == "8")

    {

        cout << "Eight ";

    }

    if (rank == "9")

    {

        cout << "Nine ";

    }

    if (rank == "10")

    {

        cout << "Ten ";

    }

    if (rank == "J")

    {

        cout << "Jack ";

    }

    if (rank == "Q")

    {

        cout << "Queen ";

    }

    if (rank == "K")

    {

        cout << "King ";

    }

    if (rank == "A")

    {

        cout << "Ace ";

    }

    //if stament of rank and output of rank

    if (suite == "H")

    {

        cout << "of Hearts" << endl;

    }

    if (suite == "D")

    {

        cout << "of Diamonds" << endl;

    }

    if (suite == "S")

    {

        cout << "of Spades" << endl;

    }

    if (suite == "C")

    {

        cout << "of Clubs " << endl;

    }

    cout << endl;

    return 0;

}


Related Solutions

C# Yahtzee Program I am creating a Yahtzee program where you have the option to choose...
C# Yahtzee Program I am creating a Yahtzee program where you have the option to choose 5 or more dice that will be rolled. I just need help trying to create a random dice roll and seeding it. Could you give me some code examples of getting a dice roll of a six sided die that won't display similar results.
I have to create a program in C++ where a user can enter as many numbers...
I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds,...
I'm working in Java and am working on a project where I need to find an...
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying...
A variance of Monty Hall Problem. Let's say I am playing a game where there are...
A variance of Monty Hall Problem. Let's say I am playing a game where there are 6 doors, there is a car behind 2 doors and there are goats behind 4 doors. I don't know what is behind the doors but I want a car. Let's say I pick a door at random, so Initially, my chances of winning a car are 1/3. But before I open my door to see if I won, the host of the game opens...
Hello, I am working on a C-program that deals with memory mapping, Please show all code...
Hello, I am working on a C-program that deals with memory mapping, Please show all code (code must be in the C) and outputs. The instructions are as follows INSTRUCTIONS: You have two sample c codes, one for the client and one for the server. IF the client makes a change in the shared memory, those changes will show on the server side. Your task is to adapt the change strategy to develop two applications so they can send messages...
I am working on making a simple gradebook. How it works is it asks user for...
I am working on making a simple gradebook. How it works is it asks user for name and grade. It does this for multiple instances until the person types quit to end the program. The code I have here works. My question is how do I print the output in alphabetical order? Output from the input I put into my program: David 98 Annabelle 87 Josh 91 Ben 95 What I want my code to do (in alphabetical order): Annabelle87...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user decide the size of a 2d array then fill said array. #include <iostream> using namespace std; typedef int* IntArrayPtr; int main(void) { int column, row, i, j; IntArrayPtr *arr_num = new IntArrayPtr[row]; cout << " " << endl << " This program creates a 2D array of dynamic memory, fills it with user entered numbers, " << endl << " Then prints the array...
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
I am working on a project where I need to close the solenoid valve (24DCV) at...
I am working on a project where I need to close the solenoid valve (24DCV) at a different rate of time (variable closer time). It needs close very fast (at least within 50ms). I am using NI DAQ: USB-6211. Which relay should I use? Will digital relay timer work? Looking forward to your suggestions.
Write a working C++ program to ask the user for a set of grades which are...
Write a working C++ program to ask the user for a set of grades which are to be stored in an array in increasing sequence. The user must first provide the number which represents the count of all grades to be provided. For example, if there will be 10 grades, the user is first prompted to provide the number 10. Subsequently, each grade is provided, one at a time. Allow for a maximum of 30 grades in defining the array....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT