if else statements and library functions used properly?

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;

youwonpercentage = (youwonnumber / totalbattlesnumber) * 100; // calculate your winning percentage

cout<<" You won: "<<right<<setw(6)<<youwonnumber<<" ("<<right<<fixed<<setw(6)<<setprecision(2)<<youwonpercentage<<" %)"<<endl;

opponentwonpercentage = (opponentwonnumber / totalbattlesnumber) * 100; // claculate opponents winning percentage

cout<<" Opponent won: "<<right<<setw(6)<<opponentwonnumber<<" ("<<right<<fixed<<setw(6)<<setprecision(2)<<opponentwonpercentage<<" %)"<<endl;

tiepercentage = (tienumber / totalbattlesnumber) * 100; // calculate tie games percentage

cout<<" Tie: "<<right<<setw(6)<<tienumber<<" ("<<right<<fixed<<setw(6)<<setprecision(2)<<tiepercentage<<" %)\n"<<endl;

/*interaction */
{

char choice;

if(choice==Y)
{
cout<<"\n Another battle (enter: Y/N) ?\n"<<endl;
cin.get(choice);
system("cls"); // to clear the screen
}

if(choice==N)
{
exit(0);
}

else
{
cout<<" Invalid input. Enter only 'Y' or 'N' please."<<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.

{ unsigned int sequence, suit1 = 1, suit2 = 0, set = 0, face1 = 0, face2 = 0,
totalbattlesnumber = 0, youwonnumber = 0, opponentwonnumber = 0, tienumber = 0;
char pic1, pic2, heart = 3, club = 5, lefttopcorner = 201, dhb = 205,
toprightcorner = 187,vhb = 186,leftbottomcorner = 200, rightbottomcorner = 188,
singlelefttopcorner = 218, shb = 196, singlerighttopcorner =191, svb = 179,
singleleftbottomcorner = 192, singlerightbottomcorner = 217;
string card1, card2;
unsigned short count;
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++; //adds one more number to the opponent won display
}

if(card1 == card2)
{
cout<<" This is a tie game.\n"<<endl;
tienumber++; //adds one more number to the tie display
}

if(face1 > face2)
{
cout<<" Congratulations. You win this battle!\n"<<endl;
youwonnumber++; //adds one more number to the you won display
}

totalbattlesnumber++; //adds one more number to total battles

/* Display to show results for card came */

cout<<" Total battles: "<<right<<setw(3)<<totalbattlesnumber<<endl;

youwonpercentage = (youwonnumber / totalbattlesnumber) * 100.0; // calculate your winning percentage

cout<<" You won: "<<setw(3)<<youwonnumber<<" ("<<setw(6)<<fixed<<setprecision(2)<<youwonpercentage<<" %)"<<endl;

opponentwonpercentage = (opponentwonnumber / totalbattlesnumber) * 100.0; // calculate opponents winning percentage

cout<<" Opponent won: "<<setw(3)<<opponentwonnumber<<" ("<<setw(6)<<setprecision(2)<<opponentwonpercentage<<" %)"<<endl;

tiepercentage = (tienumber / totalbattlesnumber) * 100.0; // calculate tie games percentage

cout<<" Tie: "<<setw(3)<<tienumber<<" ("<<setw(6)<<setprecision(2)<<tiepercentage<<" %)\n"<<endl;

/*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");
}
}
masterrick wrote:
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
Last edited on
Topic archived. No new replies allowed.