Small program output not correct?
Mar 23, 2013 at 3:21am UTC
This is a 4 hand poker program. I just hit a roadblock which I can't solve. When I run the program and enter values in between 1-10 it's supposed to tell me whether I have a pair, 3 of a kind ,4 of a kind or nothing. I feel like the problem is in the switch command but I haven't been able to get it to work. Please help me thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <iostream>
using namespace std;
bool die (const string & msg);
int main () {
unsigned w,x,y,z;
cout<<"Input 4 card values: " ;
cin>>w>>x>>y>>z || die ("invalid input" );
// Suppose we've discarded the face cards, so legal values are [1, 10]
if (w<1 || w>10 || x<1 || x>10 || y<1 || y>10 || z<1 || z>10)
die ("Out of range: [1,10]" );
switch ( (w==x) + (x==y ) + (y==z) + (z==w)) {
case 0: cout<<"nothing" <<endl; break ;
case 1: cout<<" pair" <<endl; break ;
case 3: cout<<"3 of a kind" <<endl; break ;
case 4: cout<<"4 of a kind" <<endl; break ;
default : assert (false );
} // end switch
} // main
bool die (const string & msg) {
cerr<<endl<<"Fatal error: " <<msg<<endl;
exit (EXIT_FAILURE);
} // end die
Last edited on Mar 23, 2013 at 7:32am UTC
Mar 23, 2013 at 9:43am UTC
It works great for me. What's the problem you are getting?
Mar 23, 2013 at 6:43pm UTC
Try this input 2 9 9 2. And you will get a "3 of a kind". I have fixed it.
Topic archived. No new replies allowed.