If Statement "Your Code Will Never Be Executed" - c++

The if else at the bottom will not work; it says that pets do not really exist.
void checkIn ()
{
int roomNum = 0;
int party = 0;
char pets;
char smoke;
cout << "----------------------------" << endl;
cout << "Welcome to Shrek's Swamp Inn" << endl << endl;
cout << "How many people are In your party? " << endl;
cin >> party;
cout << "Do you have pets? (Y/N)" << endl;
cin >> pets;
switch (pets)
{
case 'Y' : case 'y':
cout << "GTLO" << endl;
break;
case 'N' : case 'n':
cout << "cool cool" << endl;
break;
default :
cout << "User error" << endl;
}
cout << "Do you want a smoke room? (Y/N) " << endl;
cin >> smoke;
switch (smoke)
{
case 'Y' : case 'y':
cout << "GTLO" << endl;
break;
case 'N' : case 'n':
cout << "cool cool" << endl;
break;
default :
cout << "User error" << endl;
}
if((pets == 'Y' || 'y') && (smoke == 'Y' || 'y')){
roomNum = rand() % 4 + 1;
cout << "Your room is " << roomNum << "S" << endl;
}
else if((pets == 'Y' || 'y') && (smoke == 'n' || 'N')){
roomNum = rand() % 8 + 5;
cout << "Your room is " << roomNum << "P" << endl;
}
else if((pets == 'n' || 'N') && (smoke == 'n' || 'N'));{
roomNum = rand() % 15 + 9;
cout << "Your room is " << roomNum << "R" << endl;
}
}

You should change the if condition
(pets == 'Y' || 'y')
to
(pets == 'Y' || pets == 'y')
Otherwise, 'y' (as a non-zero value) will always be evaluated as true for if condition, then (pets == 'Y' || 'y') will be always true too.
And it's the same for all other if conditions.

Adding to songyuanyao's answer, you have another syntax error
else if((pets == 'n' || 'N') && (smoke == 'n' || 'N'));{
Note the semicolon ; towards the end of the statement, you need to remove this

Related

Error Compiling C++ Blackjack Program (Missing Elements)

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.

Price does not accumulate if user repeat order

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.

Nothing happening when I call my function?

I'm trying to make a function for a rock, paper, scissors game with two char parameters where the first one represents the user's choice of rock, paper, or scissors. The second parameter represents the result of the game, either win, loss, or tie. When I tried to call the function, however, nothing is happening. I'm lost on what exactly I need to do next. All help is greatly appreciated!
#include <iostream>
#include <cstdlib>
using namespace std;
double playRPS (char a, char b);
int main() {
char letter;
char result = 0;
cout << "Welcome to COP3014 ROCK PAPER SCISSORS!\n\n";
cout << "Please select: " << endl
<< "Rock(r), Paper(p), or Scissors(s)? " << endl
<< "Or enter q to quit --> ";
cin >> letter;
if (letter == 'r' || letter == 'R' || letter == 'p' || letter == 'P' || letter == 's' || letter == 'S') {
playRPS(letter, result);
}
else {
cout << "Please enter r, p, or s" << endl;
}
return 0;
}
double playRPS (char x, char y) {
int choice1 = 0, choice2 = 0, choice3 = 0;
int user2 = rand() % 3 + 1;
if (( x == 'r' || x == 'R') && (user2 == '2')) {
cout << "The computer chose... PAPER!";
cout << "You chose ROCK!";
cout << "You LOSE!";
y = choice2;
return choice2;
}
else if ((x == 'r' || x == 'R') && (user2 == '1')) {
cout << "The computer chose... ROCK!";
cout << "You chose ROCK!";
cout << "You TIED!";
y = choice3;
return choice3;
}
else if ((x == 'r' || x == 'R') && (user2 == '3')) {
cout << "The computer chose... SCISSORS!";
cout << "You chose ROCK!";
cout << "You WIN!";
y = choice1;
return choice1;
}
else if (( x == 'p' || x == 'P') && (user2 == '2')) {
cout << "The computer chose... PAPER!";
cout << "You chose PAPER!";
cout << "You TIED!";
y = choice3;
return choice3;
}
else if (( x == 'p' || x == 'P') && (user2 == '1')) {
cout << "The computer chose... ROCK!";
cout << "You chose PAPER!";
cout << "You WIN!";
y = choice1;
return choice1;
}
else if (( x == 'p' || x == 'P') && (user2 == '3')) {
cout << "The computer chose... SCISSORS!";
cout << "You chose PAPER!";
cout << "You LOSE!";
y = choice2;
return choice2;
}
else if (( x == 's' || x == 'S') && (user2 == '2')) {
cout << "The computer chose... PAPER!";
cout << "You chose SCISSORS!";
cout << "You WIN!";
y = choice1;
return choice1;
}
else if (( x == 's' || x == 'S') && (user2 == '1')) {
cout << "The computer chose... ROCK!";
cout << "You chose SCISSORS!";
cout << "You LOSE!";
y = choice2;
return choice2;
}
else if (( x == 's' || x == 'S') && (user2 == '3')) {
cout << "The computer chose... SCISSORS!";
cout << "You chose SCISSORS!";
cout << "You TIED!";
y = choice3;
return choice3;
}
else{
return main();
}
General remarks
using namespace std;
Avoid using namespace std.
return main();
You are not allowed to call main in your code. This will result in Undefined Behavior. Plus, what is your intention here?
rand()
rand() should be avoided. Here is an interesting video on why you should not use it, and instead use C++11 random.
y = choice2;
You are passing y by value, which means assigning it won't modify the y from the outside. You should pass y by reference when doing this (i.e. char& y in the declaration).
Why is the function not doing anything?
... Actually, it does!
user2 == '2'
Your comparisons are broken. '2' is actually not 2, but 50. The reason is that '2' is a character, so you actually are reading the associated character code.
This means all of your conditions are false in playRPS, so the only thing the function does it to call main() (in return main();).
What about shortening your code?
Your test cases are quite redundant and heavy. You could change it to drastically cut down your code size.
Let's print what the choice selected by the player...
if (x == 'r' || x == 'R')
cout << "You chose ROCK!" << endl;
else if (x == 'p' || x == 'P')
cout << "You chose PAPER!" << endl;
else if (x == 's' || x == 'S')
cout << "You chose SCISSORS!" << endl;
All good! Let's do the same with the computer's choice!
if (user2 == 1)
cout << "The computer chose... ROCK!" << endl;
else if (user2 == 2)
cout << "The computer chose... PAPER!" << endl;
else if (user2 == 3)
cout << "The computer chose... SCISSORS!" << endl;
Then you should compare what the player chose to what the computer chose, and tell who is the winner. Unfortunately, we can't compare x to user2 without doing many cases again...
What if we decided to have x's choice being saved the same way as user2? We also can use tolower to avoid checking for the caps variant of the letter.
int user1 = 0;
x = tolower(x); // we force x to lower case
if (x == 'r')
user1 = 1;
else if (x == 'p')
user1 = 2;
else if (x == 's')
user1 = 3;
Good! Now we can also improve conditions in our first if/else if block:
if (user1 == 1)
cout << "You chose ROCK!" << endl;
else if (user1 == 2)
cout << "You chose PAPER!" << endl;
else if (user1 == 3)
cout << "You chose SCISSORS!" << endl;
Which means we also can compare user1 to user2 so we know who won.
if (user1 == user2) {
cout << "It's a TIE!" << endl;
}
else if ((user1 == 1 && user2 == 2) ||
(user1 == 2 && user2 == 3) ||
(user1 == 3 && user2 == 1)) {
cout << "You LOSE!" << endl;
}
else {
cout << "You WIN!" << endl;
}
However, using 1, 2 and 3 does not make things very clear. What if you used an enum to represent these values?
enum RPSChoice
{
ROCK = 1,
PAPER = 2,
SCISSORS = 3
};
For example, the first block now looks like:
if (user1 == ROCK)
cout << "You chose ROCK!" << endl;
else if (user1 == PAPER)
cout << "You chose PAPER!" << endl;
else if (user1 == SCISSORS)
cout << "You chose SCISSORS!" << endl;
What if we wrapped our new two first blocks into a function so we avoid repeating ourselves?
void printDecision(string who, int choice) {
cout << who; // no matter what, we will tell who took a decision
if (choice == ROCK)
cout << " chose ROCK!" << endl;
else if (choice == PAPER)
cout << " chose PAPER!" << endl;
else if (choice == SCISSORS)
cout << " chose SCISSORS!" << endl;
}
This way, we can make playRPS even more clear, by replacing the two large blocks into simple, short function calls:
printDecision("You", user1);
printDecision("The computer", user2);
Let's do another simple function that decides who won:
int winner(int user1, int user2) {
if (user1 == user2) {
return 0; // tie
}
else if ((user1 == ROCK && user2 == PAPER) ||
(user1 == PAPER && user2 == SCISSORS) ||
(user1 == SCISSORS && user2 == ROCK)) {
return 2; // user2 is the winner
}
else {
return 1; // user1 is the winner
}
}
And a final one that returns the value we give according to a given character:
int characterToChoice(char c)
{
c = tolower(c);
if (c == 'r')
return ROCK;
else if (c == 's')
return SCISSORS;
else if (c == 'p')
return PAPER;
else
return 0; // Not a proper choice!
}
Done! This is the final program with all improvements in (nothing done to replace rand() in), and here is an online prompt to try it out.
Note that there are more ways you can improve the code, to simplify it even more and to make it more clear. I am most notably thinking about std::unordered_map to bind a RPSChoice value to a string, and a char to a RPSChoice. You may also prefer switch to if in some cases.
As stated by the comments to your question, you could have diagnosed this issue using a debugger. πάντα ῥεῖ's comment for reference:
The right tool to solve such problems is your debugger. You should step through your code line-by-line before asking on Stack Overflow. For more help, please read How to debug small programs (by Eric Lippert).

How to make input string taken as it is instead of only the first character?

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;
}

RPG Stats Issue

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.