'MAO card game in C++
So I wanted to program a card game that is called Mao. In this game players are given a first card with a letter A, B, C or D and a number from 1 to 9. They have to put another cards which they have but they must be the same letter or the same number. For example if the first card is A7, players can but A8, D7 and so on. But they can't put C3. In my program, the user inputs a first card that is called 'first' and then he inputs other cards named 'second'. If the user can use one of the cards followed by the rules of the game, program should return 'Yes'.If not, then 'No'. My code is:
using namespace std;
int main()
{
string first = "";
string second = "";
cout<<"enter first card: "<<endl;
cin>>first;
for (int i = 0 ;i < 5 ;i++)
{
cin>>second;
}
for (int i = 0; i < 5 ;i++)
{
if (second[0]==first[0] || second[1]==first[1])
{
cout<<"Yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
return 0;
}
The output should be like this:
enter first card:
A7
A9
C1
B7
D7
D5
Yes
no
Yes
Yes
no
But it only displays 'Yes'. Does anyone know how to fix this?
Solution 1:[1]
#include <iostream>
using namespace std;
int main()
{
string first="";
string second[5];
cout<<"Enter first card: "<<endl;
cin>>first;
for (int i=0; i<5;i++)
{
cin>>second[i];
}
cout<<"------------------------"<<endl;
for (int i=0; i<5; i++)
{
if (second[i][0]==first[0] || second[i][1]==first[1])
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pythonista |
