need some advise with a program for a game

ok, so i need help mostly with making the decimals round to 2 places (ie.0.50)
i'm not the best at this, so if you wnt to change mroe of it, to make it easier to use / understand i'd be extremely thankful :)
heres the code:

//Undead Armory
//Made by
//Mulch
//property of Undead Nation / TEA.

// This is a program that will calculate the cost of the undeads armor, then
//Calculates the WA/WB/DA/Price of all of it...

#include <cstdlib>
#include <iostream>
#include <iomanip>



using namespace std;

int main()
{
int num,num1,num2,num3,num4,num5,num6,num7,Rsword,Lshield,Scythe,RMarmor,BCTouch,Shelm,Hofblur,Pblade; // all the different 'types' of armour
double WA, WB, DA,WA2, WB2, DA2,WA3, WB3, DA3,WA4, WB4, DA4,WA5, WB5, DA5,WA6, WB6, DA6,WA7, WB7, DA7, WA8, WB8, DA8; // the 'decimal' numbers.
float sum1, sum2,sum3, sum4, sum5,sum6,sum7, sum8,sum9,sum10, sum11,sum12,sum13, sum14,sum15,sum16, sum17,sum18,sum19, sum20,sum21,sum22, sum23,sum24; // the 'decimal' numbers.
int type,option,option2,display,calc ; //other things.





cout << "would you like to Display, or Calculate?" << endl;
cout << "1 = Display, 2 = Calculate" << endl;
cin >> option;

cout << endl;

if (option < 1 || option >= 3)
{
cout << "ERROR";
}
if (option == 1) // displaying section
{
cout << "would you like to see: RustySword, LargeShield, Scythe, RingMailArmor, BracersofChillingTouch,Spangenlm, HelmofBlur, or PoisonBlade?" << endl;
cout << "1 = rusty sword, 2 = large shield, 3 = scythe, 4 = ringmailarmor, 5 = bracersofchillingtouch, 6 = spangenhelm, 7 = helmofblur, 8 = poisonblade" << endl;
cin >> option2;



if (option2 == 1)
{
cout << "Rusty Sword" << endl;
cout << "WA = 0.45" << endl;
cout << "WB = 0.15" << endl;
cout << "DA = 0.00" << endl;
cout << "Cost = 500 gold" <<endl;
WA = 0.45;
WB = 0.15;
DA = 0.00;
}


if (option2 == 2)
{
cout << "Large Shield" << endl;
cout << "WA = 0.00" << endl;
cout << "WB = 0.00" << endl;
cout << "DA = 0.75" << endl;
cout << "Cost = 750 gold" <<endl;
WA2 =0.00 ;
WB2 =0.00;
DA2 =0.75;
}

if (option2 == 3)
{
cout << "Scythe" << endl;
cout << "WA = 1.05" << endl;
cout << "WB = 0.15" << endl;
cout << "DA = 0.30" << endl;
cout << "Cost = 1300 gold" <<endl;
WA3 = 1.05;
WB3 = 0.15;
DA3 = 0.30;
}

if (option2 == 4)
{
cout << "Ring Mail Armor" << endl;
cout << "WA = 0.00" << endl;
cout << "WB = 0.00" << endl;
cout << "DA = 1.35" << endl;
cout << "Cost = 1200 gold" <<endl;
WA4 = 0.00;
WB4 = 0.00;
DA4 = 1.35;
}

if (option2 == 5)
{
cout << "Bracers of Chilling Touch" << endl;
cout << "WA = 0.00" << endl;
cout << "WB = 1.80" << endl;
cout << "DA = 0.00" << endl;
cout << "Cost = 1400 gold" <<endl;
WA5 = 0.00;
WB5 = 1.80;
DA5 = 0.00;
}

if (option2 == 6)
{
cout << "Spangenhelm" << endl;
cout << "WA = 0.00" << endl;
cout << "WB = 0.00" << endl;
cout << "DA = 1.50" << endl;
cout << "Cost = 1500 gold" <<endl;
WA6 = 0.00;
WB6 = 0.00;
DA6 = 1.50;
}

if (option2 == 7)
{
cout << "Helm of Blur" << endl;
cout << "WA = 0.00" << endl;
cout << "WB = 0.00" << endl;
cout << "DA = 2.10" << endl;
cout << "Cost = 2100 gold" <<endl;
WA7 = 0.00;
WB7 = 0.00;
DA7 = 2.10;
}

if (option2 == 8)
{
cout << "Poison Blade" << endl;
cout << "WA = 1.20" << endl;
cout << "WB = 1.50" << endl;
cout << "DA = 0.00" << endl;
cout << "Cost = 2000 gold" <<endl;
WA8 = 1.20;
WB8 = 1.50;
DA8 = 0.00;
}



}


else




if ( option == 2) // the buying section
{
cout << "what type of armor are you buying?" << endl;
cout << " 1 = rusty sword, 2 = large shield, 3 = scythe, 4 = ringmailarmor" << endl;
cout << " 5 = bracersofchillingtouch, 6 = spangenhelm, 7 = helmofblur, 8 = poisonblade" << endl;
cin >> type;
cout.precision(2);
if (type == 1)
{
cout << "How many Rusty Swords do you wish to buy?" << endl;
cin >> num;
sum1 = WA * num;
cout << "you will gain " << sum1 << " WA"<< endl; // WA increase.
sum2 = WB * num;
cout << "You will gain " << sum2 << " WB" << endl; // WB increase.
sum3 = DA * num;
cout << "You will gain " << sum3 << " DA" << endl; // DA increase.
}


if (type == 2)
{
cout << "How many Large Shields do you wish to buy?" << endl;
cin >> num1;
sum4 = WA2 * num1;
cout << "you will gain " << sum4 << " WA"<< endl; // WA increase.
sum5 = WB2 * num1;
cout << "You will gain " << sum5 << " WB" << endl; // WB increase.
sum6 = DA2 * num1;
cout << "You will gain " << sum6 << " DA" << endl; // DA increase.
}

if (type == 3)
{
cout << "How many Scythes do you wish to buy?" << endl;
cin >> num2;
sum7 = WA3 * num2;
cout << "you will gain " << sum7 << " WA"<< endl; // WA increase.
sum8 = WB3 * num2;
cout << "You will gain " << sum8 << " WB" << endl; // WB increase.
sum9 = DA3 * num2;
cout << "You will gain " << sum9 << " DA" << endl; // DA increase.
}

if (type == 4)
{
cout << "How many Ring Mail Armor do you wish to buy?" << endl;
cin >> num3;

sum10 = WA4 * num3;
cout << "you will gain " << sum10 << " WA"<< endl; // WA increase.
sum11 = WB4 * num3;
cout << "You will gain " << sum11 << " WB" << endl; // WB increase.
sum12 = DA4 * num3;
cout << "You will gain " << sum12 << " DA" << endl; // DA increase.
}

if (type == 5)
{
cout << "How many Bracers Chilling Touch do you wish to buy?" << endl;
cin >> num4;

sum13 = WA4 * num4;
cout << "you will gain " << sum13 << " WA"<< endl; // WA increase.
sum14 = WB4 * num4;
cout << "You will gain " << sum14 << " WB" << endl; // WB increase.
sum15 = DA4 * num4;
cout << "You will gain " << sum15 << " DA" << endl; // DA increase.
}

if (type == 6)
{
cout << "How many Spangenhelm's do you wish to buy?" << endl;
cin >> num5;

sum16= WA4 * num5;
cout << "you will gain " << sum16 << " WA"<< endl; // WA increase.
sum17 = WB4 * num5;
cout << "You will gain " << sum17 << " WB" << endl; // WB increase.
sum18 = DA4 * num5;
cout << "You will gain " << sum18 << " DA" << endl; // DA increase.
}

if (type == 7)
{
cout << "How many Helm of Blur's do you wish to buy?" << endl;
cin >> num6;

sum19 = WA4 * num6;
cout << "you will gain " << sum19 << " WA"<< endl; // WA increase.
sum20= WB4 * num6;
cout << "You will gain " << sum20 << " WB" << endl; // WB increase.
sum21 = DA4 * num6;
cout << "You will gain " << sum21 << " DA" << endl; // DA increase.
}


if (type == 8)
{
cout << "How many Poison Blades do you wish to buy?" << endl;
cin >> num7;

sum22 = WA4 * num7;
cout << "you will gain " << sum22 << " WA"<< endl; // WA increase.
sum23 = WB4 * num7;
cout << "You will gain " << sum23 << " WB" << endl; // WB increase.
sum24 = DA4 * num7;
cout << "You will gain " << sum24 << " DA" << endl; // DA increase.
}

//possible upgrades: determine how much power you get.

}


system("PAUSE");
return EXIT_SUCCESS;
}
1
2
3
4
int num,num1,num2,num3,num4,num5,num6,num7,Rsword,Lshield,Scythe,RMarmor,BCTouch,Shelm,Hofblur,Pblade; // all the different 'types' of armour
double WA, WB, DA,WA2, WB2, DA2,WA3, WB3, DA3,WA4, WB4, DA4,WA5, WB5, DA5,WA6, WB6, DA6,WA7, WB7, DA7, WA8, WB8, DA8; // the 'decimal' numbers.
float sum1, sum2,sum3, sum4, sum5,sum6,sum7, sum8,sum9,sum10, sum11,sum12,sum13, sum14,sum15,sum16, sum17,sum18,sum19, sum20,sum21,sum22, sum23,sum24; // the 'decimal' numbers.
int type,option,option2,display,calc ; //other things. 


Why the heck would you need this many variables...??
as i said, not very good.. and i this is stil a rough draft.. jsut making sure everythings going to work fine.
Round a float or double to a precision of 0.01:
(int)((floatValue + 0.005) * 100) / 100.0
Round a float or a double to a precision of 0.05:
(int)((floatValue + 0.025) * 20) / 20.0

If cou continue like this your game (even if it stays a small game) will need about 100k of lines of code ;)

You could let it automatically choose instead of if(type == 5)..., if(type == 6)... etc.
oh yea.. jsut to mention.. this isnt a game.. it's FOR a game.. fkor the game Lordsoflegend. im jsut making this.

and where would i put that line/lines? :( i wish i was better at this.. lmao
Last edited on
Ok, personally if you only ever need two decimal places then I'd store the numbers *100 and use integer arithmetic, then output

cout << ( num/100) << '.' << setw(2) << setfill('0') << (num%100)

because you can't exactly represent all numbers 0.01-0.99 in floating point format. You end up with roundoff errors.
On a slightly different area than asked for, maybe you should put all them if's into a switch statement?
how do i go about doing that now?
A switch wouldn't make much of a difference. Some kind of a lookup table or anything like this would be much more appropriate so you wouldn't have to write 8 orde more times the same thing.
Topic archived. No new replies allowed.