Help 'reading' some code.
Oct 30, 2012 at 8:36pm UTC
Here is some code, I was wondering if someone could explicitly describe what is going on throughout the code so I could better understand it. I have tried but I keep running into issues. Such as what do a and b refer to in the first, second, and third calls to swap(a,b)? Also I have a non-void error when trying to run this. Some clear explanation would help me solve further code.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <iomanip> // Required for setw() setprecision(), fixed.
#include <cmath>
using namespace std;
void swap(string& a, string& b)
{ string temp;
temp = a;
a = b;
b = temp;
}
int three_card_monte(string card1, string card2, string card3)
{ swap(card1, card2);
swap(card2, card3);
swap(card1, card3);
if (card1 == "queen" )
return 1;
else if (card2 == "queen" )
return 2;
else if (card3 == "queen" )
return 3;
}
int main()
{ string first = "queen" ;
string second = "king" ;
string third = "ace" ;
int guess;
int location;
location = three_card_monte(first, second, third);
cout << "Where's the Queen ( 1, 2 or 3 ) ? " ;
cin >> guess;
if (guess == location)
cout << "Congratulations!" << "\n" ;
else
cout << "Better luck next time, it was number " << location << "\n" ;
return 0;
}
Oct 30, 2012 at 8:56pm UTC
Such as what do a and b refer to in the first, second, and third calls to swap(a,b)?
Always the same. a
is the first parameter passed in, b
is the second.
Topic archived. No new replies allowed.