In: Computer Science
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;
}
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;
}