So I've never worked with percentages in c++ and I was wondering how to incorporate into a program I'm making for a game so see what the chances are that an item will reach a certain level depending on the chance of success for each different level.
Here's what I have so far, all I need is how to do the percentages.

``````#include <iostream>
#include <cmath>

int main () {

int itemlevel = 0;
int requiredlevel = 0;
bool itembroken = false;
double percentageofsuccess = 0;

cout >> "What Level is the item you want to upgrade?/n";
cin >> itemlevel;
cout >> "/nWhat Level do you want your item to be upgraded to?/n";
cin >> requiredlevel;

while (item != requiredlevel){
if (itemlevel == 0 || itemlevel == 1 || itemlevel == 2) {
itemlevel = itemlevel + 1; // 100% chance item will succeed
}

if (itemlevel == 3 && requiredlevel >= 4){
//80% chance item will succeed
if (upgradesuccess = true) item = item + 1;
else if (upgradefailed = true) item = item;
}

if (itemlevel == 4 && requiredlevel >= 5){
//60% chance item will succeed
if (upgradesuccess = true) item = item + 1;
else if (upgradefailed = true) item = item;
}

if (itemlevel == 5 && requiredlevel >= 6){
//%50 chance item will succeed
if (upgradesuccess = true) item = item + 1;
else if (upgradefailed = true) item = item;
}

if (itemlevel == 6 && requiredlevel >= 7){
//45% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess = true) item = item + 1;
else if (upgradefailed = true) item = item;
else if (itembroken = true) cout >> "The item has broken during upgrading./n";
}

if (itemlevel == 7 && requiredlevel >= 8){
//40% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess = true) item = item + 1;
else if (upgradefailed = true) item = item;
else if (itembroken = true) cout >> "The item has broken during upgrading./n";
}

if (itemlevel == 8 && requiredlevel >= 9){
//35% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess = true) item = item + 1;
else if (upgradefailed = true) item = item;
else if (itembroken = true) cout >> "The item has broken during upgrading./n";
}

if (itemlevel == 9 && requiredlevel >= 10){
//30% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess = true) item = item + 1;
else if (upgradefailed = true) item = item;
else if (itembroken = true) cout >> "The item has broken during upgrading./n";
}

if (itemlevel == 10 && requiredlevel >= 11){
//25% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess = true) item = item + 1;
else if (upgradefailed = true) item = item;
else if (itembroken = true) cout >> "The item has broken during upgrading./n";
}

if (itemlevel == 11 && requiredlevel == 12){
//20% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess = true) item = item + 1;
else if (upgradefailed = true) item = item;
else if (itembroken = true) cout >> "The item has broken during upgrading./n";
}

cout >> "There is a: " << percentageofsuccess << "Percent Chance your item will reach the desired level."
}
Cin.get();
return 0;
}``````

There's some syntax problems with your program that you should handle beforehand. In each of your condition statements, make sure you're using == signifying equals rather than = meaning assignment.

Also, cout uses the ostream operator << whereas cin uses the istream operator >>. In addition, it's "cin.get()" rather than "Cin.get()".

Oops on the == thing, I started to do that then I guess I just stopped, Thanks for that I'll fix those right away.

Ok sorry for the double post but I think I'm done with the programming:

``````#include <iostream>
#include <cmath>
#include <sstream>
#include <ctime>

using namespace std;

int main () {

int itemlevel = 0;
int requiredlevel = 0;
int counter = 0;
int successcounter = 0;
int failcounter = 0;
double percentage = 0;
double percentageofsuccess = 0;
char itembrokentext[40];
bool itembroken = false;
int itembrokencounter = 0;

cout << "What Level is the item you want to upgrade?/n";
cin >> itemlevel;
cout << "/nWhat Level do you want your item to be upgraded to?/n";
cin >> requiredlevel;

while (itemlevel != requiredlevel || itembroken != false && counter != 1000){
if (itemlevel == 0 || itemlevel == 1 || itemlevel == 2) {
itemlevel == itemlevel + 1; // 100% chance item will succeed
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

else if (itemlevel == 3 && requiredlevel >= 4){ //80% chance item will succeed
if (upgradesuccess == true) itemlevel == itemlevel + 1;
else if (upgradefailed == true) itemlevel == itemlevel;
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

else if (itemlevel == 4 && requiredlevel >= 5){//60% chance item will succeed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel == itemlevel;
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

else if (itemlevel == 5 && requiredlevel >= 6){//%50 chance item will succeed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel == itemlevel;
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

else if (itemlevel == 6 && requiredlevel >= 7){//45% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 <= 25) itembroken == true;
if (upgradesuccess == true) itemlevel == itemlevel + 1;
else if (upgradefailed == true) itemlevel == itemlevel;
else if (itembroken == true)itembrokencounter == 1;
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

else if (itemlevel == 7 && requiredlevel >= 8){//40% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 <= 25) itembroken = true;
if (upgradesuccess = true) itemlevel == itemlevel + 1;
else if (upgradefailed == true) itemlevel == itemlevel;
else if (itembroken == true)itembrokencounter == 2;
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

else if (itemlevel == 8 && requiredlevel >= 9){//35% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 <= 25) itembroken == true;
if (upgradesuccess == true) itemlevel == itemlevel + 1;
else if (upgradefailed == true) itemlevel == itemlevel;
else if (itembroken == true) itembrokencounter == 3;
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

else if (itemlevel == 9 && requiredlevel >= 10){//30% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 <= 25) itembroken == true;
if (upgradesuccess == true) itemlevel == itemlevel + 1;
else if (upgradefailed == true) itemlevel == itemlevel;
else if (itembroken == true) itembrokencounter = 4;
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

else if (itemlevel == 10 && requiredlevel >= 11){//25% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 <= 25) itembroken == true;
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel == itemlevel - 1;
else if (itembroken == true) itembrokencounter == 5;
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

else if (itemlevel == 11 && requiredlevel == 12){//20% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 <= 25) itembroken == true;
if (upgradesuccess == true) itemlevel == itemlevel + 1;
else if (upgradefailed == true) itemlevel == itemlevel - 1;
else if (itembroken == true)itembrokencounter == 6;
if (itemlevel == requiredlevel) successcounter == successcounter + 1;
}

counter == counter + 1;
if (itembroken = true) failcounter = failcounter + 1;
cout << "/nTrial number: " << counter;
}

/*if (itembrokencounter == 1) itembrokentext == "6 to 7";
else if (itembrokencounter == 2) itembrokentext == "7 to 8";
else if (itembrokencounter == 3) itembrokentext == "8 to 9";
else if (itembrokencounter == 4) itembrokentext == "9 to 10";
else if (itembrokencounter == 5) itembrokentext == "10 to 11";
else if (itembrokencounter == 6) itembrokentext == "11 to 12";
cout << "Congratulations the item you wanted to enhance reached the desired level!/n";
}
if (itembroken == true){
cout << "Sorry, the item you wanted to enhance broke trying to get from level:  " << itembrokentext;
}*/
percentage = successcounter / counter;
percentageofsuccess = percentage * 100;

cout << "The chance your item will reach the desired level is: " << percentageofsuccess << "%";

cin.get();
return 0;
}``````

Sorry, I think you misunderstood what I meant. In your if and else if statements, make sure that you're using the right form of =. == checks of equality whereas = is assignment.

I didn't mean for you to change mostly every statement with a = to ==.

As far as picking random percentage chances, try:

``````#include <stdlib.h>
[...]
double val = double(rand())/RAND_MAX;  // get a random number between 0 and 1
if (val < 0.35) {
// there's a 35% chance of this being true
}``````

or if you prefer percentages between 0 and 100:

``````double val = 100.0 * double(rand())/RAND_MAX;
if (val < 35.0) {
// there's a 35% chance of this being true
}``````

To do something like your combined percentages:

``````double val = 100.0 * double(rand())/RAND_MAX;
//30% chance item will succeed 25% chance the item will be destroyed
if (val < 30.0) {
// there's a 30% chance of this being true
}
else if (val < 55.0) {
// there's a 25% chance of the value being between 30 and 55
itembroken = true;
}``````

overlapping responses!

Your notation `if (upgrading >=0 <= 25)` is incorrect, instead:

``if (upgrading >= 0 && upgrading <= 25)``

Also, your rand()%1 isn't doing what you want, though rand()%100 will get you close. For an artificial example of "close", if rand() returned integers between 0 and 101 inclusive, then rand()%100 would get 0 or 1 twice as often as any other value.

Ok heres my new code, sorry I've been working on it by myself and just now got to look at the comments, I think I did some of what you guys are saying and I think I've for the most part got the code able to run, BUT the counter just keeps going past 10000 and wont stop.
how do I fix this?
Anyway heres the new code:

``````#include <iostream>
#include <cmath>
#include <sstream>
#include <ctime>
#include <time.h>
#include <cstdlib>

using namespace std;

int main () {

int itemlevel = 0;
int requiredlevel = 0;
int counter = 0;
int successcounter = 0;
int failcounter = 0;
double percentage = 0;
double percentageofsuccess = 0;
bool itembroken = false;
int itembrokencounter = 0;
bool counterfinished = false;

cout << "What Level is the item you want to upgrade?\n";
cin >> itemlevel;
cout << "What Level do you want your item to be upgraded to?\n";
cin >> requiredlevel;

while (itemlevel != requiredlevel || itembroken != false && counterfinished == false){
srand ( time(0) );
if (counter >= 10000) counterfinished = true;
if (itemlevel == 0 || itemlevel == 1 || itemlevel == 2) {
itemlevel = itemlevel + 1; // 100% chance item will succeed
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 3 && requiredlevel >= 4){ //80% chance item will succeed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 4 && requiredlevel >= 5){//60% chance item will succeed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 5 && requiredlevel >= 6){//%50 chance item will succeed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 6 && requiredlevel >= 7){//45% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 && <= 25) itembroken = true;
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
else if (itembroken == true)itembrokencounter = 1;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 7 && requiredlevel >= 8){//40% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 && <= 25) itembroken = true;
if (upgradesuccess = true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
else if (itembroken == true)itembrokencounter = 2;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 8 && requiredlevel >= 9){//35% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 && <= 25) itembroken = true;
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
else if (itembroken == true) itembrokencounter = 3;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 9 && requiredlevel >= 10){//30% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 && <= 25) itembroken = true;
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
else if (itembroken == true) itembrokencounter = 4;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 10 && requiredlevel >= 11){//25% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 && <= 25) itembroken = true;
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel - 1;
else if (itembroken == true) itembrokencounter = 5;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 11 && requiredlevel == 12){//20% chance item will succeed 25% chance the item will be destroyed
if (upgrading >=0 && <= 25) itembroken = true;
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel - 1;
else if (itembroken == true)itembrokencounter = 6;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

counter = counter + 1;
if (itembroken = true) failcounter = failcounter + 1;
cout << "\nTrial number: " << counter;
}

/*if (itembrokencounter == 1) itembrokentext == "6 to 7";
else if (itembrokencounter == 2) itembrokentext == "7 to 8";
else if (itembrokencounter == 3) itembrokentext == "8 to 9";
else if (itembrokencounter == 4) itembrokentext == "9 to 10";
else if (itembrokencounter == 5) itembrokentext == "10 to 11";
else if (itembrokencounter == 6) itembrokentext == "11 to 12";
cout << "Congratulations the item you wanted to enhance reached the desired level!/n";
}
if (itembroken == true){
cout << "Sorry, the item you wanted to enhance broke trying to get from level:  " << itembrokentext;
}*/
percentage = successcounter / counter;
percentageofsuccess = percentage * 100;

cout << "The chance your item will reach the desired level is: " << percentageofsuccess << "%";

cin.get();
return 0;
}``````

At line 32, you have:

``while (exprA || exprB && exprC) {``

Without regard to whether the (exprA || exprB) is correct, the && operator takes precedence over the || operator, your intent probably requires parenthesizing the ||-part:

``while ((exprA || exprB) && exprC) {``

Thanks for that couldn't find it anywhere... but now I get a problem where:
it always just says 0% no matter what.
why is this happening?
I think it has to do with the rand() number staying the same.

``````#include <iostream>
#include <cmath>
#include <sstream>
#include <ctime>
#include <time.h>
#include <cstdlib>

using namespace std;

int main () {

int itemlevel = 0;
int requiredlevel = 0;
int counter = 0;
int successcounter = 0;
int failcounter = 0;
double percentage = 0;
double percentageofsuccess = 0;
bool itembroken = false;
int itembrokencounter = 0;
bool counterfinished = false;

cout << "What Level is the item you want to upgrade?\n";
cin >> itemlevel;
cout << "What Level do you want your item to be upgraded to?\n";
cin >> requiredlevel;

while ((itemlevel != requiredlevel || itembroken != false) && counterfinished == false){
itembroken = false;

srand ( time(0) );
if (counter >= 1000) counterfinished = true;
if (itemlevel == 0 || itemlevel == 1 || itemlevel == 2) {
itemlevel = itemlevel + 1; // 100% chance item will succeed
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 3 && requiredlevel >= 4){ //80% chance item will succeed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 4 && requiredlevel >= 5){//60% chance item will succeed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 5 && requiredlevel >= 6){//%50 chance item will succeed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 6 && requiredlevel >= 7){//45% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
else if (itembroken == true)itembrokencounter = 1;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 7 && requiredlevel >= 8){//40% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
else if (itembroken == true)itembrokencounter = 2;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 8 && requiredlevel >= 9){//35% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
else if (itembroken == true) itembrokencounter = 3;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 9 && requiredlevel >= 10){//30% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel;
else if (itembroken == true) itembrokencounter = 4;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 10 && requiredlevel >= 11){//25% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel - 1;
else if (itembroken == true) itembrokencounter = 5;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

else if (itemlevel == 11 && requiredlevel == 12){//20% chance item will succeed 25% chance the item will be destroyed
if (upgradesuccess == true) itemlevel = itemlevel + 1;
else if (upgradefailed == true) itemlevel = itemlevel - 1;
else if (itembroken == true)itembrokencounter = 6;
if (itemlevel == requiredlevel) successcounter = successcounter + 1;
}

counter = counter + 1;
if (itembroken == true) failcounter = failcounter + 1;
cout << "\nTrial number: " << counter;
}

/*if (itembrokencounter == 1) itembrokentext == "6 to 7";
else if (itembrokencounter == 2) itembrokentext == "7 to 8";
else if (itembrokencounter == 3) itembrokentext == "8 to 9";
else if (itembrokencounter == 4) itembrokentext == "9 to 10";
else if (itembrokencounter == 5) itembrokentext == "10 to 11";
else if (itembrokencounter == 6) itembrokentext == "11 to 12";
cout << "Congratulations the item you wanted to enhance reached the desired level!/n";
}
if (itembroken == true){
cout << "Sorry, the item you wanted to enhance broke trying to get from level:  " << itembrokentext;
}*/
percentage = successcounter / counter;
percentageofsuccess = percentage * 100;

cout << "\nThe chance your item will reach the desired level is: " << percentageofsuccess << "%\n";

cin.get();
return 0;
}``````

Ah, integer division. successcounter is only ever zero (if you break your item before it levels up to the desired level) or one (if your item levels up to the desired level without breaking). (1 / howeverManyAttempts) will evaluate to 0 if (howeverManyAttemtps > 1). Instead do (at line 164):

``percentage = double(successcounter) / counter;``

to force it to do floating-point (or in this case double-precision) division, returning a reasonably accurate value between 0 and 1.

Also, now that I read more closely, I think you want your while expression at line 32 to read:

``while ((itemlevel != requiredlevel && itembroken != false) && counterfinished == false){``

(Alternatively think of it as "I want to quit when a) itemlevel == requiredlevel or b) itembroken == true or c) counterfinished = true, whichever happens first" ... that's the same as "I want to keep going as long as (not a and not b and not c)")

nevermind, but after you set itemlevel to 0 and ask it to go to 4 or more it will be 0% no matter what and asking it to go from 0 to 1,2, or 3 will always give off 100%, 50%, and 33.33%

I would suggest putting a LOT of cout's in your code, so you can see exactly what it's doing each time through the loop.

For instance, your upgradefailed case probably shouldn't reduce the itemlevel of the item. You may want to print something out in that case, but I would imagine the itemlevel should stay the same.

Also, since your code is nearly identical for each itemlevel, it's worth simplifying your program using some arrays (and maybe even structures). Pseudocode follows:

``````int chances[][2] = {{100, 0}, // chance of success, chance of failure
{100, 0}, // 100% chance of success for first three levels
{100, 0},
{80, 0},  // 80% chance of success, no chance of failure
...
};

...
int chanceofsuccess = chances[itemlevel][0];
int chanceoffailure = chances[itemlevel][1];
if (requiredlevel > itemlevel) {
upgrading = rand() % 99 + 1;  // this will always be an integer between 1 and 100, simplifying your if-expressions
else
if (upgradefailed == true and chanceoffailure > 0) {
upgrading = rand() % 99 + 1;
itembroken = true;
}
// do the right thing for the case of upgradesuccess or itembroken
}``````

I apologize if my C++ syntax is broken anywhere.

If you can use the same code with different data for each itemlevel, it's much less likely that one specific place in your code will be broken, and it's easier to print out all your values to see what's going on when you don't have to do it in a dozen places (and then clean it up when you're finished).

Ok I changed it and now its:

``````#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

int main () {

int itemlevel = 0;
int requiredlevel = 0;
int counter = 0;
double successcounter = 0;
int failcounter = 0;
double percentage = 0;
double percentageofsuccess = 0;
bool itembroken = false;
bool counterfinished = false;

cout << "What Level is the item you want to upgrade?\n";
cin >> itemlevel;
cout << "What Level do you want your item to be upgraded to?\n";
cin >> requiredlevel;
while ((itemlevel != requiredlevel || itembroken != false) && counterfinished == false){
itembroken = false;
srand ( time(0) );
if (counter >= 1000) counterfinished = true;
int chances[][2] = {{100, 0}, // chance of success, chance of failure
{100, 0}, // 100% chance of success for first three levels
{100, 0},
{80, 0},  // 80% chance of success, no chance of failure
{60, 0},
{50, 25},
{45, 25},
{40, 25},
{35, 25},
{30, 25},
{25, 25},
{20, 25},
};

int chanceofsuccess = chances[itemlevel][0];
int chanceoffailure = chances[itemlevel][1];
if (requiredlevel > itemlevel) {
upgrading = rand() % 99 + 1;  // this will always be an integer between 1 and 100, simplifying your if-expressions
else
if (upgradefailed == true && chanceoffailure > 0) {
upgrading = rand() % 99 + 1;
itembroken = true;
}
if (upgradesuccess == true) itemlevel ++;// do the right thing for the case of upgradesuccess or itembroken
if (itemlevel >= 10) itemlevel --;
if (itemlevel == requiredlevel) successcounter ++;
if (itembroken == true) failcounter ++;

}
cout << "\nTrial number: " << counter;
counter = counter + 1;
}
percentage = successcounter / counter;
percentageofsuccess = percentage * 100;

cout << "\nThe chance your item will reach the desired level is: " << percentageofsuccess << "%\n";

cin.get();
return 0;
}``````

but I'm still getting the same problem.

Hmmm, I think successcounter++ does the right thing if successcounter is a double (successcounter += 1.0), but it's not my favorite. Since it's a counter, I'd prefer to keep it an int, then cast the math to double to perform the division at line 72. Float values can be deceptively weird, simply because they can't exactly represent values in binary that are obvious in decimal. For fun, try the following:

``````float val = 0.0;
while (val < 100.0) {
std::cout << val << std::endl;
val += 0.2;
}``````

Of course, integers -can- be represented exactly, over the range you're looking at, so it shouldn't be a problem. I just cringe anyway. Sorry.

But now that your code is a manageable length, each time through the loop you can print out: starting itemlevel, chanceofsuccess, chanceoffailure, upgrading, upgradesuccess, upgradefailed, upgrading again (if rolling for itembroken), itembroken, resulting itemlevel, successcounter and failcounter. That should tell you exactly what your problem is! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.