First of all, sorry if this is a dumb question.
These are my 2 functions. I'm trying to make a repeated order in function listHardware as you can see, "Do you want to purchase more? Press y for yes." at the bottom of the function. But if user repeat order, only the new price will be displayed. Why does not it accumulate with the previous price of their previous order?
I thought I made it right by doing price+= in calcFunc.
void listHardware()
{
int i,j,type,m_type,c_type,r_type,s_type,g_type,quantity=0;
const int SIZE=3;
char shopmember,addorder = 'y';
double price=0;
while(addorder=='Y' || addorder=='y')
{
cout << endl << "Select which type of hardware that you want to purchase: ";
cin >> type;
if(type==1)
{
const char *monitor[SIZE][2]=
{
{"BenQ PD3200U", "(RM3000)"},
{"Acer Predator X34", "(RM4000)"},
{"Dell UltraSharp UP3218K", "(RM8000)"}
};
cout << "Monitors:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << monitor[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which Monitor you would like to purchase: ";
cin >> m_type; //monitor
cout << endl << "How many Monitor would you like to purchase?" << endl;
cin >> quantity;
}
if(type==2)
{
const char *cpu[SIZE][2]=
{
{"AMD Ryzen 7 2700X", "(RM1200)"},
{"Intel Core i5-8600K", "(RM1200)"},
{"Intel Core i9-7980XE", "(RM8000)"}
};
cout << "CPU:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << cpu[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which CPU would you like to purchase: ";
cin >> c_type; //cpu
cout << endl << "How many CPU would you like to purchase?" << endl;
cin >> quantity;
}
if(type==3)
{
const char *ram[SIZE][2]=
{
{"Patriot Viper Elite 8GB DDR4-2400MHz", "(RM400)"},
{"G.Skill Ripjaws V 16GB DDR4-2400MHz", "(RM1200)"},
{"Corsair Dominator Platinum 32GB DDR4-3333MHz", "(RM2000)"}
};
cout << "RAM:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << ram[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which RAM would you like to purchase: ";
cin >> r_type; //ram
cout << endl << "How many RAM would you like to purchase?" << endl;
cin >> quantity;
}
if(type==4)
{
const char *ssd[SIZE][2]=
{
{"Samsung 860 Pro 1TB", "(RM1250)"},
{"Crucial MX500 1TB", "(RM600)"},
{"WD Blue 2TB", "(RM1600)"}
};
cout << "SSD:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << ssd[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which SSD would you like to purchase: ";
cin >> s_type; //ssd
cout << endl << "How many SSD would you like to purchase?" << endl;
cin >> quantity;
}
if(type==5)
{
const char *gcard[SIZE][2]=
{
{"Nvidia GeForce RTX 2080 Ti", "(RM4000)"},
{"Nvidia GeForce GTX 1080 Ti", "(RM2900)"},
{"AMD Radeon RX 580 8GB", "(RM2100)"}
};
cout << "Graphic Card:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << gcard[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which Graphic Card would you like to purchase: ";
cin >> g_type; //gpu
cout << endl << "How many Graphic Card would you like to purchase?" << endl;
cin >> quantity;
}
cout << "Membership (Y/N): ";
cin >> shopmember;
cout << "Do you want to purchase more? Press y for yes." << endl;
cin >> addorder;
cin.ignore();
price=calcFunc(m_type,c_type,r_type,s_type,g_type,quantity,price,shopmember); //function call
}
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "Total Price: " << price << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
}
//
double calcFunc(int m_type,int c_type,int r_type, int s_type, int g_type,int quantity,double price, char shopmember)
{
const double discount=0.1;
if(m_type==1) //monitor
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(3000-(3000*discount))*quantity;
else
price+=3000*quantity;
}
else if(m_type==2) //monitor
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(4000-(4000*discount))*quantity;
else
price+=4000*quantity;
}
else if(m_type==3) //monitor
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(8000-(8000*discount))*quantity;
else
price+=8000*quantity;
}
if(c_type==1) //cpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1200-(1200*discount))*quantity;
else
price+=1200*quantity;
}
else if(c_type==2) //cpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1200-(1200*discount))*quantity;
else
price+=1200*quantity;
}
else if(c_type==3) //cpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(8000-(8000*discount))*quantity;
else
price+=8000*quantity;
}
if(r_type==1) //ram
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(400-(400*discount))*quantity;
else
price+=400*quantity;
}
else if(r_type==2) //ram
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1200-(1200*discount))*quantity;
else
price+=1200*quantity;
}
else if(r_type==3) //ram
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(2000-(2000*discount))*quantity;
else
price+=2000*quantity;
}
if(s_type==1) //ssd
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1250-(1250*discount))*quantity;
else
price+=1250*quantity;
}
else if(s_type==2) //ssd
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(600-(600*discount))*quantity;
else
price+=600*quantity;
}
else if(s_type==3) //ssd
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1600-(1600*discount))*quantity;
else
price+=1600*quantity;
}
if(g_type==1) //gpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(4000-(4000*discount))*quantity;
else
price+=4000*quantity;
}
else if(g_type==2) //gpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(2900-(2900*discount))*quantity;
else
price+=2900*quantity;
}
else if(g_type==3) //gpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(2100-(2100*discount))*quantity;
else
price+=2100*quantity;
}
}
double calcFunc(int m_type,int c_type,int r_type, int s_type, int g_type,int quantity,double price, char shopmember)
Is missing a return statement.
Add
return price;
at the end of the function and you should be good.
Related
So I've been getting: Warning C4244 '=': conversion from 'double' to 'long', possible loss of data line 158. From what I understand is that 'pow' and 'long int result' are somehow connected to this, I have been messing around with it and changed 'long int result' to 'double result' and got rid of the warning. I have a solution to get rid of the warning, but it won't matter since this program need long int to handle more data otherwise it will overflow if I use double.
If I decided to keep this warning in the program will there be potenial issues with it?
Can I somehow get rid of the warning and still be able to use 'long int result' or at least be able to handle more data some other way?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
char menu, choice;
int numberYears = 0;
long int deposit, withdraw;
long int result = 0;
long int annualDeposit = 0;
float interestRate = 0.0;
long int balance = 0;
cout << "------------------------------\n" << "Welcome to Bank Simulator 3000" << "\n------------------------------\n\n";
while (true)
{
cout << "XXXXXXX[MENU]XXXXXXXX\n";
cout << "[D]eposit\n";
cout << "[W]ithdrawal\n";
cout << "[B]alance\n";
cout << "[I]nterest payment\n";
cout << "[E]xit\n";
cout << "XXXXXXXXXXXXXXXXXXXXX\n";
cin >> menu;
switch (menu)
{
case'd':
case'D':
cout << "\n[DEPOSIT]\n";
cout << "Do you want to make a deposit?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y')
{
cout << "\nHow much do you want to deposit?\n" << "\n:";
}
else
{
cout << "\nReturning to menu.\n\n";
continue;
}
cin >> deposit;
balance += deposit;
cout << "\n" << deposit << " Kr has been added to balance.\n\n";
continue;
case'w':
case'W':
cout << "\n[WITHDRAWAL]\n" << "Do you want to make a withdrawal?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y')
{
cout << "\nHow much do you want to withdraw?\n" << "\n:";
}
else
{
cout << "\nReturning to menu.\n\n";
continue;
}
cin >> withdraw;
if (withdraw == 0)
{
cout << "\nYou withdrew 0 amount. No changes will apply.\n\n";
continue;
}
balance -= withdraw;
cout << "\n" << withdraw << " Kr has been drawn from balance.\n\n";
continue;
case'b':
case'B':
cout << "\n[BALANCE]\n";
cout << balance << " Kr\n\n";
continue;
case'i':
case'I':
cout << "\n[INTEREST PAYMENT]\n";
cout << "Do you want to calculate your interest payment?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y')
{
cout << "What's your annual deposit?\n" << ":";
}
else
{
cout << "\nReturning to menu.\n\n";
continue;
}
cin >> annualDeposit;
if (annualDeposit == 0)
{
cout << "You typed 0 in your annual deposits, this will give unwanted results.\n" << "Do you want to continue?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y');
else
{
cout << "Returning to menu.\n\n";
continue;
}
}
cout << "What's Your Interest Rate?\n" << ":";
cin >> interestRate;
if (interestRate == 0)
{
cout << "You typed 0 in your interest rate, this will give unwanted results.\n" << "Do you want to continue?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y');
else
{
cout << "Returning to menu.\n\n";
continue;
}
}
cout << "How many years do you want to save to?\n" << ":";
cin >> numberYears;
if (numberYears <= 0)
{
cout << "You typed 0 or less in number of years, this will give unwanted results.\n" << "Do you want to continue?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y');
else
{
cout << "Returning to menu.\n\n";
continue;
}
}
result = annualDeposit * pow(1 + interestRate / 100, numberYears);
cout << "Your Interest Payment Will Be: " << result << " Kr In " << numberYears << " Years\n\n";
continue;
default:
cout << "\nPlease use the following example\n" << "D = Deposit | W = Withdrawal | B = Balance | I = Interest payment | E = Exit\n\n";
continue;
case'e':
case'E':
cout << "Thanks for using Bank Simulator 3000!\n";
cout << "Press any key to close";
system("pause>0");
break;
}
break;
}
return(0);
}
In typical environment, a double variable can store a floating-point number upto about 10^300 (assuming IEEE754 64-bit is used).
On the other hand, a long int can store an integer only upto about 10^9 (32-bit) or 10^18 (64-bit).
Therefore, the maximum value to handle by long int is much smaller than one for double. This is why conversion from double to long int can cause loss of data.
You can add an explicit cast to suppress the warning.
result = static_cast<long int>(annualDeposit * pow(1 + interestRate / 100, numberYears));
I'm doing a school project that creates a blackjack game, but unfortunately, I'm stumped at a few errors I must have made earlier on. The errors appear on lines 165 and 173. It states that I'm missing several elements that I cannot piece together. Could someone help inform me of what's wrong with this program that won't compile?
#include <iostream>
#include <string>
#include <ctime>
#include <limits>
using namespace std;
int getCard()
{
return rand() % 10 + 1;
}
char readChar()
{
char c;
cin >> c;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return c;
}
int main()
{
//set srand to seed system clock
//to generate random number
srand((int)time(0));
char yesno = 'y';
do
{
int dealer = getCard();
int first = getCard(), second = getCard();
int total = first + second;
if (yesno == 'y' || yesno == 'Y')
{
cout << "The dealer starts with a " << dealer << endl;
cout << "Your first cards: " << first << ", " << second << endl;
cout << "Total: " << total << endl;
do
{
cout << "hit? (y/n): ";
yesno = readChar();
if (yesno == 'y' || yesno == 'Y')
{
int newCard = getCard();
total += newCard;
cout << "Card: " << newCard << endl;
cout << "Total: " << total << endl;
if (total > 21)
{
cout << "Bust" << endl;
yesno = 'n';
break;
}
else if(total == 21)
{
cout << "Blackjack" << endl;
}
else if (yesno == 'n' || yesno == 'N')
{
//Dealer must take more cards if less than 17
while (dealer < 17)
{
cout << "Dealer has a " << dealer << "..." << endl;
char c;
do {
cout << "(c to continue) ";
c = readChar();
} while (c != 'c' && c != 'C');
int newDealerCard = getCard();
cout << "Dealer gets a " << newDealerCard << endl;
dealer += newDealerCard;
cout << "Total: " << dealer << endl;
}
//determine winner
if (dealer == 21 && dealer != total)
cout << "Lose" << endl;
else if (dealer == total)
cout << "Push" << endl;
else if (dealer > 21 && total > dealer && total < 21)
cout << "Win" << endl;
else if (dealer > total && dealer <= 21)
cout << "Lose" << endl;
break;
}
} while (yesno != 'n' && yesno != 'N');
}
cout << "Would you like to play again? (y/n) : ";
yesno = readChar();
} while (yesno != 'n' && yesno != 'N');
return 0;
}
Since I was a bit lost at the end, I wasn't sure where to add my missing elements.
There are at least two do {...} while (...) loops in there that do not have while clauses.
I want to create a menu where there is both letter and number.
I tried to use string but now when user input 20, it will only take the first number, which is 2. How do I make so that when user put 20, it will be seen as 20 instead of 2?
#include <iostream>
using namespace std;
int main ()
{
string choice;
cout << "1. A" << endl;
cout << "2. B" << endl;
cout << "3. C" << endl;
cout << "4. D" << endl;
cout << "Q. Quit" << endl;
do
{
cout << "Please enter your choice: ";
cin >> choice;
if (choice[0] == '1')
{
cout << "1";
} else if (choice[0] == '2')
{
cout << "2";
} else if (choice[0] == '3')
{
cout << "3";
} else if (choice[0] == '4')
{
cout << "4";
} else if (choice[0] == 'q' || choice[0] == 'Q')
{
cout << "q";
} else {
cout << "Please choose one of the menu above. " << endl;
}
} while (choice[0] != 1 && choice[0] != 2 && choice[0] != 3 && choice[0] != 4 && choice[0] != 'q');
return 0;
}
You also can see my code at http://cpp.sh/7ipv
Your code treats 20 as 2 because it sees only the first character of the input.
Try this:
#include <iostream>
using namespace std;
int main ()
{
string choice;
cout << "1. A" << endl;
cout << "2. B" << endl;
cout << "3. C" << endl;
cout << "4. D" << endl;
cout << "Q. Quit" << endl;
do
{
cout << "Please enter your choice: ";
cin >> choice;
if (choice == "1")
{
cout << "1";
} else if (choice == "2")
{
cout << "2";
} else if (choice == "3")
{
cout << "3";
} else if (choice == "4")
{
cout << "4";
} else if (choice == "q" || choice == "Q")
{
cout << "q";
} else {
cout << "Please choose one of the menu above. " << endl;
}
} while (choice != "1" && choice != "2" && choice != "3" && choice != "4" && choice != "q" && choice != "Q");
return 0;
}
Im trying to figure out why the program runs in a loop. I just can't see how it starts the loop. would like to give you more info but just run the code and do everything right and the program just starts back over but not at the beginning. It will start from when I am selecting the payment option.
here is the code and the code is c++:
#include <iostream>
#include <functional>
using namespace std;
int option, pause, payment, selection, zipcode, error, pin, quantity_chips, quantity_milk, quantity_cola,
quantity_coffee, quantity_pennies, quantity_nickels, quantity_dimes, quantity_quarters, quantity_dollars,
quantity_fives, quantity_tens, quantity_twenties, quantity_total;
float chips, milk, cola, coffee, total, tax, final_cost, pennies, nickels, dimes, quarters,
dollars, fives, tens, twenties, cash_total, owed, change;
void checkout(), debitcard (), creditcard (), cash (), receipt ();
void menu ()
{
cout << "Welcome to Kenjin Xer0's Item Pooper.\n"
<< "10 or less items.\n";
do
{
quantity_total = (quantity_chips) + (quantity_milk) + (quantity_cola) + (quantity_coffee);
cout << "Select from our menu: Selected\n"
<< "\t1. Potato Chip.....$1.50......" << quantity_chips << "\n"
<< "\t2. 2% Milk.........$2.00......" << quantity_milk << "\n"
<< "\t3. Off Brand Cola..$1.00......" << quantity_cola << "\n"
<< "\t4. Dark Coffee.....$2.50......" << quantity_coffee << "\n"
<< "\t5. Check out Total:" << quantity_total << "\n";
cout << "Enter your option: ";
cin >> option;
if (option == 1)
{
cout << "Quantity: ";
cin >> quantity_chips;
chips = (1.5 * quantity_chips);
}
else if (option == 2)
{
cout << "Quantity: ";
cin >> quantity_milk;
milk = (2 * quantity_milk);
}
else if (option == 3)
{
cout << "Quantity: ";
cin >> quantity_cola;
cola = (1 * quantity_cola);
}
else if (option == 4)
{
cout << "Quantity: ";
cin >> quantity_coffee;
coffee = (2.5 * quantity_coffee);
}
else if (option == 5)
{
if (total > 10)
{
cout << "Problem! 10 or less line, Man!\n";
option = 0;
}
else
{
checkout();
}
}
else
{
cout << "Invalid option.\n";
}
}
while (option !=5);
}
void checkout ()
{
do
{
cout << "Select payment option (1:Debit 2:Credit 3:Cash): ";
cin >> payment;
if (payment == 1)
{
debitcard ();
}
else if (payment == 2)
{
creditcard ();
}
else if (payment == 3)
{
cash ();
}
else
{
cout << "Weird Choice, try again.\n";
}
}
while (payment != 1||2||3);
}
void debitcard ()
{
error = 3;
do
{
error--;
cout << "Enter PIN:\n";
cin >> pin;
if (error == 0)
{
cout << "We are sorry but does this card acually belong to you.\n"
<< "Now Leave.\n";
break;
}
else if (pin != 0000)
{
cout << "Wrong. Try Again\n"
<< "You now have " << error << " more tries\n";
}
else
{
receipt();
}
}
while (pin != 0000);
}
void creditcard ()
{
error = 3;
do
{
error--;
cout << "Enter Zip:\n";
cin >> zipcode;
if (error == 0)
{
cout << "We are sorry but does this card acually belong to you.\n"
<< "Now Leave.\n";
break;
}
else if (zipcode != 77523)
{
cout << "Wrong. Try Again\n"
<< "You now have " << error << " more tries\n";
}
else
{
receipt();
}
}
while (zipcode != 77523);
}
void cash ()
{
total = (chips) + (milk) + (cola) + (coffee);
tax = (total * .10);
final_cost = tax + total;
cout << "You owe $"<< final_cost <<".\n";
do
{
cash_total = (pennies) + (nickels) + (dimes) + (quarters) + (dollars) + (fives) + (tens) + (twenties);
owed = final_cost - cash_total;
cout << "Select the Amount:\n"
<< "\t1. Penny................$0.01......" << quantity_pennies << "\n"
<< "\t2. Nickel...............$0.05......" << quantity_nickels << "\n"
<< "\t3. Dime.................$0.10......" << quantity_dimes << "\n"
<< "\t4. Quarter..............$0.25......" << quantity_quarters << "\n"
<< "\t5. One Dollar Bill......$1.00......" << quantity_dollars << "\n"
<< "\t6. Five Dollars Bill....$5.00......" << quantity_fives << "\n"
<< "\t7. Ten Dollar Bill......$10.00....." << quantity_tens << "\n"
<< "\t8. Twenty Dollar Bill...$20.00....." << quantity_twenties << "\n"
<< "\t9. Cash Out\n"
<< "Cash you Have: $" << cash_total << " Still owe: $" << owed << "\n";
cout << "Enter your selection: ";
cin >> selection;
if (selection == 1)
{
cout << "Quantity: ";
cin >> quantity_pennies;
pennies = (0.01 * quantity_pennies);
}
else if (selection == 2)
{
cout << "Quantity: ";
cin >> quantity_nickels;
nickels = (0.05 * quantity_nickels);
}
else if (selection == 3)
{
cout << "Quantity: ";
cin >> quantity_dimes;
dimes = (0.10 * quantity_dimes);
}
else if (selection == 4)
{
cout << "Quantity: ";
cin >> quantity_quarters;
quarters = (0.25 * quantity_quarters);
}
else if (selection == 5)
{
cout << "Quantity: ";
cin >> quantity_dollars;
dollars = (1.00 * quantity_dollars);
}
else if (selection == 6)
{
cout << "Quantity: ";
cin >> quantity_fives;
fives = (5.00 * quantity_fives);
}
else if (selection == 7)
{
cout << "Quantity: ";
cin >> quantity_tens;
tens = (10.00 * quantity_tens);
}
else if (selection == 8)
{
cout << "Quantity: ";
cin >> quantity_twenties;
twenties = (20.00 * quantity_twenties);
}
else if (selection == 9)
{
receipt ();
}
else
{
cout << "Invalid option.\n";
}
}
while (selection != 9);
}
void receipt ()
{
total = (chips) + (milk) + (cola) + (coffee);
tax = (total * .10);
final_cost = tax + total;
change = owed * -1;
cout << "Receipt Take:\n";
if (quantity_chips > 0)
{
cout << "Potato Chips: $1.50 x " << quantity_chips << " = $" << chips << endl;
}
if (quantity_milk > 0)
{
cout << "2% Milk: $2.00 x " << quantity_milk << " = $" << milk << endl;
}
if (quantity_cola > 0)
{
cout << "Off Brand Cola: 1.00 x " << quantity_cola << " = $" << cola << endl;
}
if (quantity_coffee > 0)
{
cout << "Dark Coffee: $2.50 x " << quantity_coffee << " = $" << coffee << endl;
}
cout << "Tax (10.0%): $" << tax << endl;
cout << "Total: $" << final_cost << endl;
cout << "Change Returned: $" << change << endl;
}
int main ()
{
menu ();
return 0;
}
in ur checkout function
replace
while (payment != 1||2||3)
by
while (payment != 1 && payment != 2 && payment != 3)
The while loop that you have in checkout() is incorrect. You have:
while (payment != 1||2||3);
Which is not how you do mutiple or's. You would need to write it as
while (payment != 1 || payment != 2 || payment != 3);
To remove the confusing set of conditions that you've done to exit the do-while loop in the checkout function, this can be done instead:
void checkout ()
{
bool choice_made;
do
{
choice_made = true;
cout << "Select payment option (1:Debit 2:Credit 3:Cash): ";
cin >> payment;
if (payment == 1)
{
debitcard ();
}
else if (payment == 2)
{
creditcard ();
}
else if (payment == 3)
{
cash ();
}
else
{
cout << "Weird Choice, try again.\n";
choice_made = false;
}
}
while (!choice_made);
}
You only exit the loop until a valid choice is made. A simple boolean is all you need to control the logic.
As to your attempt, this line:
while (payment != 1||2||3)
applies the logical or to the values of 1, 2, and 3. The result is always true, which is 1. So what it all boils down to is
while (payment != 1)
I'm having a bit of an issue with my RPG stats code. I want people to use 6 basic stats (strength, dexterity, constitution, intelligence, wisdom, and charisma) that have a minimum value of 10. When creating the character, I'd like them to have 15 points to use, and in my code, it all works fine, unless you run out of points to place before you get to the last stat. Let's say you put all 15 points into strength. The display says that you'll have 25 strength, 32478493 dexterity, -42734627 constitution, -1 intelligence, etc. (these aren't exact number, just examples of what it looks like.) Here's the code.
CharCreate.h
#ifndef CharCreate_H
#define CharCreate_H
#include<fstream>
#include<string>
using namespace std;
int charcreate(){
fstream file;
char gender, choice;
string name, dummy;
int points;
int str, dex, con, intel, wis, cha;
float level;
double experience;
level = 1;
experience = 0;
ofstream myFile;
myFile.open ("T:\\character.txt");
system("color 2");
cout << "Welcome to the character creator." << endl;
genderchoice:cout << "First, are you male or female? (M/F)" << endl;
cin >> gender;
system("cls");
if (gender == 'M' || gender == 'm'){
cout << "You're male? (Y/N)" << endl;
cin >> choice;
system("cls");
if (choice == 'Y' || choice == 'y'){
cout << "Great!" << endl;
goto name;
} else if (choice == 'N' || choice == 'n'){
goto genderchoice;
}
} else if (gender == 'F' || gender == 'f'){
cout << "You're female? (Y/N)" << endl;
cin >> choice;
system("cls");
if (choice == 'Y' || choice == 'y'){
cout << "Great!" << endl;
goto name;
} else if (choice == 'N' || choice == 'n'){
goto genderchoice;
}
//------------------------------------------------------------------------------
name:system("cls");
system("color 3");
cout << "What is your name, traveler?" <<endl;
getline(cin,dummy);
getline(cin, name);
cout << "" << endl;
cout << "Your name is " << name << "? (Y/N)" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y'){
system("cls");
cout << "Greetings, " << name << "!" << endl;
} else if (choice == 'N' || choice == 'n'){
system("cls");
cout << "You must provide your name, stranger." << endl;
goto name;
}
//------------------------------------------------------------------------------
stats:system("cls");
system("color 4");
cout << "You have 6 stats to deal with in this game, and 15 points" << endl;
cout << "to allocate between them all." << endl;
cout << "These are: Strength (STR), Dexterity (DEX), Constitution (CON)," << endl;
cout << "Intelligence (INT), Wisdom (WIS), and Charisma (CHA)." << endl;
cout << "Continue: C" << endl;
cout << "Help: H" << endl;
cin >> choice;
if (choice == 'C' || choice == 'c'){
attrib:points = 15;
str:cout << "You have 10 Strength. How many more points do you wish to add?" << endl;
cin >> str;
points = points - str;
if (str > points > 15){
cout << "Not enough points!" << endl;
str = str - points;
goto str;
} else if (str == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto str;
} else if (str < points){
goto dex;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
str = str + 10;
cout << "You have " << str << " Strength" << endl;
system("pause");
system("cls");
dex:cout << "You have 10 Dexterity. How many more points do you wish to add?" << endl;
cin >> dex;
points = points - dex;
if (dex > points > 15){
cout << "Not enough points!" << endl;
dex = dex - points;
goto dex;
} else if (dex == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto dex;
} else if (dex < points){
goto con;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
dex = dex + 10;
cout << "You have " << dex << " Dexterity" << endl;
system("pause");
system("cls");
con:cout << "You have 10 Constitution. How many more points do you wish to add?" << endl;
cin >> con;
points = points - con;
if (con > points > 15){
cout << "Not enough points!" << endl;
con = con - points;
goto con;
} else if (con == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto con;
} else if (con < points){
goto intel;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
con = con + 10;
cout << "You have " << con << " Constitution" << endl;
system("pause");
system("cls");
intel:cout << "You have 10 Intelligence. How many more points do you wish to add?" << endl;
cin >> intel;
points = points - intel;
if (intel > points > 15){
cout << "Not enough points!" << endl;
intel = intel - points;
goto intel;
} else if (intel == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto intel;
} else if (intel < points){
goto wis;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
intel = intel + 10;
cout << "You have " << intel << " Intelligence" << endl;
system("pause");
system("cls");
wis:cout << "You have 10 Wisdom. How many more points do you wish to add?" << endl;
cin >> wis;
points = points - wis;
if (wis > points > 15){
cout << "Not enough points!" << endl;
wis = wis - points;
goto wis;
} else if (wis == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto wis;
} else if (con < points){
goto cha;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
wis = wis + 10;
cout << "You have " << wis << " Wisdom" << endl;
system("pause");
system("cls");
cha:cout << "You have 10 Charisma. How many more points do you wish to add?" << endl;
cin >> cha;
points = points - cha;
if (cha > points == 15){
cout << "Not enough points!" << endl;
cha = cha - points;
goto cha;
} else if (cha == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto cha;
} else if (con < points){
goto fin;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
cha = cha + 10;
cout << "You have " << cha << " Charisma." << endl;
system("pause");
system("cls");
fin:cout << "Your stats are:" << endl;
cout << "Strength: " << str << endl;
cout << "Dexterity: " << dex << endl;
cout << "Constitution: " << con << endl;
cout << "Intelligence: " << intel << endl;
cout << "Wisdom: " << wis << endl;
cout << "Charisma: " << cha << endl;
cout << "Are these correct? (Y/N)" << endl;
cin >> choice;
cout << "" << endl;
if (choice == 'Y' || choice == 'y'){
cout << "Congratulations, you have successfully finished your character." << endl;
} else if (choice == 'N' || choice == 'n')
goto attrib;
}
} else if (choice == 'H' || choice == 'h'){
cout << "Strength is how easily you can crush a tomato." << endl;
cout << "Dexterity is how easily you can handle a tomato with your hands." << endl;
cout << "Constitution is how easily you can eat a bad tomato." << endl;
cout << "Intelligence is knowing that tomato is a fruit." << endl;
cout << "Wisdom is not putting tomato in a fruit salad." << endl;
cout << "Charisma is selling a tomato-based fruit salad." << endl;
system("pause");
goto stats;
}
myFile << "Name: " << name << "\n";
myFile << "Gender: " << gender << "\n";
myFile << "\n";
myFile << "Level: " << level << "\n";
myFile << "Experience: " << experience << "\n";
myFile << "\n";
myFile << "Strength: " << str << "\n";
myFile << "Dexterity: " << dex << "\n";
myFile << "Constitution: " << con << "\n";
myFile << "Intelligence: " << intel << "\n";
myFile << "Wisdom: " << wis << "\n";
myFile << "Charisma: " << cha << "\n";
myFile.close();
}
#endif
main.cpp
#include <cstdlib>
#include <iostream>
#include "CharCreate.h"
using namespace std;
int main(int argc, char *argv[])
{
charcreate();
system("PAUSE");
return EXIT_SUCCESS;
}
How would I correct the problem of the numbers going haywire after running out of points? If it helps, I'm running Bloodshed Dev C++ as the compiler because that's the one we have to use at college.
In C++ if you don't set a value to a variable, it will contain whatever random data happens to be there at the time. You are going to want to initialize each stat to 10 (or some sane default value) at the start.