this is part of my program. can anyone tell me if it will work correctly. It is suppose to show a battle of a card game war and display the correct message, and also how many times teach won with the percentages.
{ unsigned int sequence, suit1 = 1, suit2 = 0, set = 0, face1 = 0, face2 = 0;
char pic1, pic2, heart = 3, club = 5, lefttopcorner = 201, dhb = 205,
toprightcorner=187,vhb=186,leftbottomcorner=200,rightbottomcorner=188,singleleft topcorner=218,shb=196,singlerighttopcorner =191, svb = 179, singleleftbottomcorner = 192, singlerightbottomcorner = 217;
unsigned int Y, N;
string card1, card2;
unsigned short count;
unsigned int totalbattlesnumber = 0, youwonnumber = 0, opponentwonnumber =0, tienumber = 0;
float youwonpercentage = 0.0, opponentwonpercentage = 0.0, tiepercentage = 0.0;
.
.
.
.
..(skipped main body)
.
.
.
.
.
/* DISPLAY GAME RESULT */
if(face1 < face2)
{
cout<<" Sorry. You lost this battle!\n"<<endl;
opponentwonnumber++;
totalbattlesnumber++;
}
if(card1 == card2)
{
cout<<" This is a tie game.\n"<<endl;
tienumber++;
totalbattlesnumber++;
}
if(face1 > face2)
{
cout<<" Congratulations. You win this battle!\n"<<endl;
youwonnumber++;
totalbattlesnumber++;
}
cout<<" Total battles: "<<right<<setw(6)<<totalbattlesnumber<<endl;
so i fixed some of this program , but cant figure out why it is giving me 0 on the percentage value after the second run and so on. the first time the program runs, it shows the correct percentage, but after it loops , it does not.
/*interaction */
string choice;
do {
cout<<"\n Another battle (enter: Y/N) ? ";
cin >> choice;
cout << choice << endl;
if (choice == "Y" || choice == "y") //user can enter Y or y.
{
system("cls");
}
else if (choice == "N" || choice == "n") //user can enter N or n.
{
exit(0);
}
else
{ // error message if user does not enter Y or N.
cout<<" Invalid input. Enter only 'Y' or 'N' please."<<endl;
}
} // loops to check for invalid input
while(choice != "Y" && choice != "y" && choice != "N" && choice != "n");
}
}
but cant figure out why it is giving me 0 on the percentage value after the second run and so on. the first time the program runs, it shows the correct percentage,
Probably because you are doing integer arithmetic (your codes as posted is hard to read)
For example: tiepercentage = (tienumber / totalbattlesnumber) * 100.0; // calculate tie games percentage
both tienumber and totalbattlesnumber are integers.
so tienumber/totalbattlesnumber will be always be 0 as long as tienumber is less than totalbattlesnumber - INTEGERS DO NOT HAVE DECIMAL POINTS
You if you want to keep them as integers, then when you do the calulation you will have to cast
one of the numbers to a float or double
Example: tiepercentage = ( (float) tienumber / totalbattlesnumber) * 100.0; // calculate tie games percentage