halflife Posted January 27, 2009 Share Posted January 27, 2009 Why isn't it exitting my while loop? while( choice != 2 ) { cout << "Hit or hold?" << endl; cout << "1 = hit, 2 = hold" << endl; cin >> choice; if(choice == 1) { cout << pcard3 << endl; playerscore = playerscore + pcard3; cout << "Your current score: " << playerscore << endl; if(playerscore == 21) { cout << "BLACKJACK!!! YOU WIN!!!" << endl; playerwin++; comploss++; choice == 2; } if(playerscore > 21) { cout << "PLAYER BUST!" << endl; compwin++; playerloss++; choice == 2; } } cout << "Hit or hold?" << endl; cin >> choice; if(choice == 1) { cout << pcard4 << endl; playerscore = playerscore + pcard4; cout << "Your current score: " << playerscore << endl; if(playerscore == 21) { cout << "BLACKJACK!!! YOU WIN!!!" << endl; compwin++; playerloss++; choice == 2; } if(playerscore > 21) { cout << "PLAYER BUST!" << endl; compwin++; playerloss++; choice == 2; } } cout << "Hit or hold?" << endl; cin >> choice; if(choice == 1) { cout << pcard5 << endl; playerscore = playerscore + pcard5; cout << "Your current score: " << playerscore << endl; if(playerscore == 21) { cout << "BLACKJACK!!! YOU WIN!!!" << endl; compwin++; playerloss++; choice == 2; } if(playerscore > 21) { cout << "PLAYER BUST!" << endl; compwin++; playerloss++; choice == 2; } } cout << "Hit or hold?" << endl; cin >> choice; if(choice == 1) { cout << pcard6 << endl; playerscore = playerscore + pcard6; cout << "Your current score: " << playerscore << endl; if(playerscore == 21) { cout << "BLACKJACK!!! YOU WIN!!!" << endl; compwin++; playerloss++; choice == 2; } if(playerscore > 21) { cout << "PLAYER BUST!" << endl; compwin++; playerloss++; choice == 2; } } } Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted January 27, 2009 Veteran Share Posted January 27, 2009 (edited) Because to set choice equal to 2 you need to write choice = 2; instead of choice == 2; The first is assignment, the second is comparison for equality. If you're working with Visual C++, the compiler should report this as a level 1 warning. http://msdn.microsoft.com/en-us/library/e11e1wz2.aspx Edited January 27, 2009 by Dr_Asik Link to comment Share on other sites More sharing options...
0 halflife Posted January 27, 2009 Author Share Posted January 27, 2009 your my savior man. i cant thank you enough. i feel so stupid now because i know that, and spent a half hour trying to figure out what i was doing wrong....ugh im such a noob Link to comment Share on other sites More sharing options...
Question
halflife
Why isn't it exitting my while loop?
while( choice != 2 )
{
cout << "Hit or hold?" << endl;
cout << "1 = hit, 2 = hold" << endl;
cin >> choice;
if(choice == 1)
{
cout << pcard3 << endl;
playerscore = playerscore + pcard3;
cout << "Your current score: " << playerscore << endl;
if(playerscore == 21)
{
cout << "BLACKJACK!!! YOU WIN!!!" << endl;
playerwin++;
comploss++;
choice == 2;
}
if(playerscore > 21)
{
cout << "PLAYER BUST!" << endl;
compwin++;
playerloss++;
choice == 2;
}
}
cout << "Hit or hold?" << endl;
cin >> choice;
if(choice == 1)
{
cout << pcard4 << endl;
playerscore = playerscore + pcard4;
cout << "Your current score: " << playerscore << endl;
if(playerscore == 21)
{
cout << "BLACKJACK!!! YOU WIN!!!" << endl;
compwin++;
playerloss++;
choice == 2;
}
if(playerscore > 21)
{
cout << "PLAYER BUST!" << endl;
compwin++;
playerloss++;
choice == 2;
}
}
cout << "Hit or hold?" << endl;
cin >> choice;
if(choice == 1)
{
cout << pcard5 << endl;
playerscore = playerscore + pcard5;
cout << "Your current score: " << playerscore << endl;
if(playerscore == 21)
{
cout << "BLACKJACK!!! YOU WIN!!!" << endl;
compwin++;
playerloss++;
choice == 2;
}
if(playerscore > 21)
{
cout << "PLAYER BUST!" << endl;
compwin++;
playerloss++;
choice == 2;
}
}
cout << "Hit or hold?" << endl;
cin >> choice;
if(choice == 1)
{
cout << pcard6 << endl;
playerscore = playerscore + pcard6;
cout << "Your current score: " << playerscore << endl;
if(playerscore == 21)
{
cout << "BLACKJACK!!! YOU WIN!!!" << endl;
compwin++;
playerloss++;
choice == 2;
}
if(playerscore > 21)
{
cout << "PLAYER BUST!" << endl;
compwin++;
playerloss++;
choice == 2;
}
}
}
Link to comment
Share on other sites
2 answers to this question
Recommended Posts