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...
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...
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...
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.
I am creating a program in Python that allows the user to input a credit card...
I am creating a program in Python that allows the user to input a credit card number, and determine if the card is valid. This should be done by taking every other number, starting from the right, and adding them together. The doubling each of the other digits, and adding them together as single digits, and then adding the two sums together. For example, if the number 4833 1200 3412 3456 is used, the first sum should be 6+4+2+4+0+2+3+8=29, the...
I am trying to create a makefile for the following program in Unix. The C++ program...
I am trying to create a makefile for the following program in Unix. The C++ program I am trying to run is presented here. I was wondering if you could help me create a makefile for the following C++ file in Unix and show screenshots of the process? I am doing this all in blue on putty and not in Ubuntu, so i don't have the luxury of viewing the files on my computer, or I don't know how to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT