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.
Related
When i build and run the code, i get the next error: "terminate called after throwing an instance of 'std::out_of_range'
what<>: vector::_M_range_check: __n <which is 0> >= this->size<> <which is 0>
I don't know how to fix this problem, I am new to coding!
What I'm trying to do is using a collection (list) of integers and allowing the user to select options from a menu to perform operations on the list.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector <int>numbers{};
int nr_to_add{};
char selection{};
double average{};
int min = numbers.at(0);
int max = numbers.at(0);
do{
cout << "P - Print numbers!" << endl;
cout << "A - Add numbers!" << endl;
cout << "M - Display mean of the numbers!" << endl;
cout << "S - Display the smallest number!" << endl;
cout << "L - Display the largest number!" << endl;
cout << "Q - Quit" << endl;
cout << "Enter your selection: ";
cin >> selection;
if(selection == 'p' || selection == 'P'){
for(int i = 0; i < numbers.size(); i++)
cout << "The numbers are: " << numbers[i];
if(numbers.empty())
cout << "[] - the list is empty! " << "\nYou should add some numbers!" << endl;
cout << "Add some numbers: ";
cin >> nr_to_add;
numbers.push_back(nr_to_add);
}else if(selection == 'a' || selection == 'A'){
cout << "Add the number you want: ";
cin >> nr_to_add;
cout << "\n" << nr_to_add << " has been added successfully!" << endl;
numbers.push_back(nr_to_add);
}else if(selection == 'm' || selection == 'M'){
if(numbers.empty())
cout << "Unable to calculate de average - no data introduced!" << endl;
if(numbers.size() != 0)
average = accumulate(numbers.begin(),numbers.end(), 0.0)/numbers.size();
cout << "The average is: " << average << endl;
}else if(selection == 's' || selection == 'S'){
if(numbers.empty())
cout << "Unable to determine the smallest number - list is empty!";
if(numbers.size() != 0)
for(int i = 0,n = 0; i <= n; i++){
if(numbers.at(i) < min)
min = numbers[i];
cout << "The smallest number is: " << min << endl;
}
}else if(selection == 'l' || selection == 'L'){
if(numbers.empty())
cout << "Unable to determine the largest number - list is empty!";
if(numbers.size() != 0)
for(int i = 0,n = 0; i <= n; i++){
if(numbers.at(i) > max)
max = numbers[i];
cout << "The largest number is: " << max << endl;
}
}else if(selection == 'q' || selection == 'Q'){
cout << "Goodbye..." << endl;
} else {
cout << "Unknown selection, please try again" << endl;
}
} while(selection != 'q' && selection != 'Q');
return 0;
}
The problem is that you are calling at(0) on an empty vector. That is what the out_of_range error is telling you. You need to move the calls into your if statements where the values are actually being used, after you perform your empty() checks.
Also, your min and max loops are coded wrong. And you don't need to use loops at all, you can use the standard std::min_element() and std::max_element() algorithms instead (you are already using the std::accumulate() algorithm).
Try something more like this:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <limits>
using namespace std;
int main()
{
vector<int> numbers{};
int nr_to_add;
char selection;
double average_nr;
int min_nr, max_nr;
do {
cout << "P - Print numbers!" << endl;
cout << "A - Add numbers!" << endl;
cout << "M - Display mean of the numbers!" << endl;
cout << "S - Display the smallest number!" << endl;
cout << "L - Display the largest number!" << endl;
cout << "Q - Quit" << endl;
cout << "Enter your selection: ";
if (!(cin >> selection)) {
selection = '\0';
cin.clear();
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (selection == 'p' || selection == 'P') {
if (numbers.empty()) {
cout << "The list is empty! " << "\nYou should add some numbers!" << endl;
} else {
cout << "The numbers are:"
for(size_t i = 0; i < numbers.size(); ++i) {
cout << " " << numbers[i];
}
cout << endl;
}
}
else if (selection == 'a' || selection == 'A') {
cout << "Add the number you want: ";
if (cin >> nr_to_add) {
numbers.push_back(nr_to_add);
cout << "\n" << nr_to_add << " has been added successfully!" << endl;
} else {
cout << "Bad input, please try again" << endl;
cin.clear();
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else if (selection == 'm' || selection == 'M'){
if (numbers.empty()) {
cout << "Unable to calculate the average - list is empty!" << endl;
} else {
average_nr = accumulate(numbers.begin(), numbers.end(), 0.0) / numbers.size();
cout << "The average is: " << average_nr << endl;
}
}
else if (selection == 's' || selection == 'S') {
if (numbers.empty()) {
cout << "Unable to determine the smallest number - list is empty!";
} else {
/*
min_nr = numbers[0];
for(size_t i = 1; i < numbers.size(); ++i){
if (numbers[i] < min_nr) {
min_nr = numbers[i];
}
}
*/
min_nr = *min_element(numbers.begin(), numbers.end());
cout << "The smallest number is: " << min_nr << endl;
}
}
else if (selection == 'l' || selection == 'L') {
if (numbers.empty()) {
cout << "Unable to determine the largest number - list is empty!";
} else {
/*
max_nr = numbers[0];
for(size_t i = 1; i < numbers.size(); ++i){
if (numbers[i] > max_nr) {
max_nr = numbers[i];
}
}
*/
max_nr = *max_element(numbers.begin(), numbers.end());
cout << "The largest number is: " << max_nr << endl;
}
}
else if (selection == 'q' || selection == 'Q') {
cout << "Goodbye..." << endl;
}
else {
cout << "Unknown selection, please try again" << endl;
}
}
while (selection != 'q' && selection != 'Q');
return 0;
}
Try this
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector <int>numbers{};
int nr_to_add{};
char selection{};
double average{};
int min = 0;
int max = 0;
do {
cout << "P - Print numbers!" << endl;
cout << "A - Add numbers!" << endl;
cout << "M - Display mean of the numbers!" << endl;
cout << "S - Display the smallest number!" << endl;
cout << "L - Display the largest number!" << endl;
cout << "Q - Quit" << endl;
cout << "Enter your selection: ";
cin >> selection;
if (selection == 'p' || selection == 'P') {
for (int i = 0; i < numbers.size(); i++)
cout << "The numbers are: " << numbers[i];
if (numbers.empty()) {
cout << "[] - the list is empty! " << "\nYou should add some numbers!" << endl;
cout << "Add some numbers: ";
cin >> nr_to_add;
numbers.push_back(nr_to_add);
min = nr_to_add;
max = nr_to_add;
}
}
else if (selection == 'a' || selection == 'A') {
cout << "Add the number you want: ";
cin >> nr_to_add;
cout << "\n" << nr_to_add << " has been added successfully!" << endl;
if (numbers.empty) {
min = nr_to_add;
max = nr_to_add;
}
else {
if (min > nr_to_add) min = nr_to_add;
if (max < nr_to_add) max = nr_to_add;
}
numbers.push_back(nr_to_add);
}
else if (selection == 'm' || selection == 'M') {
if (numbers.empty())
cout << "Unable to calculate de average - no data introduced!" << endl;
if (numbers.size() != 0)
average = accumulate(numbers.begin(), numbers.end(), 0.0) / numbers.size();
cout << "The average is: " << average << endl;
}
else if (selection == 's' || selection == 'S') {
if (numbers.empty())
cout << "Unable to determine the smallest number - list is empty!";
else
cout << "The smallest number is: " << min << endl;
}
else if (selection == 'l' || selection == 'L') {
if (numbers.empty())
cout << "Unable to determine the largest number - list is empty!";
else
cout << "The largest number is: " << max << endl;
}
else if (selection == 'q' || selection == 'Q') {
cout << "Goodbye..." << endl;
}
else {
cout << "Unknown selection, please try again" << endl;
}
} while (selection != 'q' && selection != 'Q');
return 0;
I'm currently in a class that wants me to make a craps game.
The problem is in int main on the second while statement comparing the point with the roll. It ignores the if statement and does the loop again, even though it hits the point or 7. Sometimes it works like it should and other times it repeats the loop a few times.
#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int diceRoll() {
int x = 0, y = 0;
x = rand() % 6 + 1;
y = rand() % 6 + 1;
cout << "You rolled a " << x << " and a " << y << " which comes out to ------> " << x + y << " <-------" << endl;
return x + y;
}
bool playAgain() {
char ans;
cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
cin >> ans;
while (ans != 'Y' || ans != 'y' || ans != 'n' || ans != 'N') {
if (ans == 'Y' || ans == 'y')
{
return true;
}
if (ans == 'N' || ans == 'n')
{
return false;
}
cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
cin >> ans;
}
}
int main()
{
srand(time(NULL));
int dices, bid, point = 0;
int money = 50;
bool gameRunning = true;
bool didTheyWin;
while (gameRunning == true) {
if (money == 0) {
cout << "You have no money, ending game." << endl;
break;
}
cout << "Please enter a bid. You currently have $" << money << endl;
cout << "$";
cin >> bid;
while (bid > money) {
cout << "Please bet below your current balance: $" << money << endl;
cout << "$";
cin >> bid;
}
dices = diceRoll();
didTheyWin = false;
if ((dices == 7) || (dices == 11)) {
cout << "You won $" << bid << " !" << endl;
money = money + bid;
}
else if ((dices == 2) || (dices == 3) || (dices == 12)) {
cout << "You LOSE! You lost $" << bid << " !" << endl;
money = money - bid;
}
else {
point = dices;
cout << "The target number is > " << point << " <" << endl;
cout << "If you hit a 7 you lose, if you hit the target you win. \nYou automatically roll until one of these two things happen.\n";
while (didTheyWin == false) {
diceRoll();
dices = diceRoll();
if (dices == point) {
cout << "You won $" << bid << " !" << endl;
money = money + bid;
cout << "You now have $" << money << endl;
didTheyWin = true;
}
else if (dices == 7) {
cout << "You LOSE! You lost $" << bid << " !" << endl;
money = money - bid;
cout << "You now have $" << money << endl;
didTheyWin = true;
}
}
}
gameRunning = playAgain();
}
cout << "Thanks for playing. _END_" << endl;
return 0;
}
You call diceRoll twice, and ignore what you get back from the first call. You'll see the results of that first roll displayed, but they'll be ignored and you'll roll again.
I have a pig dice game where there are two modes (1 or 2 dice are rolled). It's played with 2 human players. When I run my program with 1 die selected it runs fine, but when I roll 2 dice it gets thrown into an infinite loop. I'm looking for a hint on where the problem lies and why it was thrown into a loop as in my mind both programs should be almost identical. Sorry in advance if the code looks strange.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int winningScore = 100;
int turn = PLAYER1;
void printIntro()
{
cout << " Welcome to the dice game: Pig! "<< endl;
cout << "The goal is to be the first player to reach 100. If playing with one die the rules are that each player can rol as many times as they choose. just dont roll a 1 or else you'll lose your turn AND all points accumulated in that round. If you're playing with 2 dies the same rules applies, but if you roll snake eyes (double 1's) you'll not only lose your turn but you'll also loose all your points. good luck and may the best player win!"<< endl;
}
int game1(string playerName, int playerScore)
{
int roll = rand() % 6 + 1;
cout << playerName << " You rolled: " << roll <<endl;
if(roll == 1)
{
cout << " OH NO! You rolled a 1. "<< endl;
cout << " Your turn is over. " << endl;
playerScore = 0;
}
else
{
playerScore +=roll;
cout << playerName << " Your score: " << playerScore <<endl;
}
if(roll == 1)
{
if(turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else
{
char choice;
cout << " Would you like to roll again? (y/n): ";
cin >> choice;
if(choice != 'y')
{
if (turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
}
return playerScore;
}
int game2(string playerName, int playerScore)
{
int roll1 = rand() % 6 + 1;
int roll2 = rand() % 6 + 1;
cout << playerName << " You rolled: " << roll1 << " and " << roll2 <<endl;
if(roll1 || roll2 == 1)
{
cout << " OH NO! You rolled a 1. " << endl;
cout << " Your turn is over. " << endl;
playerScore = 0;
}
else if (roll1 && roll2 == 1)
{
cout << "OH CRAP! You rolled snake eyes!" << endl;
cout << " Your turn is over. " << endl;
playerScore == 0;
}
else
{
playerScore += roll1 + roll2 ;
cout << playerName << " Your score: " << playerScore <<endl;
}
if(roll1 || roll2 == 1)
{
if(turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else if (roll1 && roll2 == 1)
{
if(turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else
{
char choice;
cout << "Would you like to roll again? (y/n): ";
cin >> choice;
if(choice != 'y')
{
if (turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
}
return playerScore;
}
int main()
{
srand(time(0));
int player1score = 0;
int player2score = 0;
string player1name;
string player2name;
int dieRoll;
printIntro();
cout << " Player 1, Enter your name: ";
cin >> player1name;
cout << " Player 2, Enter your name: ";
cin >> player2name;
cout << "Wouild you like to roll with 1 or 2 dice?" << endl;
cin >> dieRoll;
if (dieRoll == 1)
{
while (player1score < winningScore && player2score < winningScore)
{
if (turn == PLAYER1)
{
player1score = game1(player1name, player1score);
}
else
{
player2score = game1(player2name, player2score);
}
}
if(player1score >= winningScore)
{
cout << player1name <<endl;
cout << " Your score is : " << player1score<<endl;
cout << player1name << " WINS! " << endl;
}
else
{
cout << player2name << endl;
cout <<" Your score: "<< player2score << endl;
cout << player2name << " WINS!" << endl;
}
}
else
{
while (player1score < winningScore && player2score < winningScore)
{
if (turn == PLAYER1)
{
player1score = game2(player1name, player1score);
}
else
{
player2score = game2(player2name, player2score);
}
}
if(player1score >= winningScore)
{
cout << player1name <<endl;
cout << " Your score is : " << player1score<<endl;
cout << player1name << " WINS! " << endl;
}
else
{
cout << player2name << endl;
cout <<" Your score: "<< player2score << endl;
cout << player2name << " WINS!" << endl;
}
}
return 0;
}
There are a couple issues in the following block which may be causing problems:
else if (roll1 && roll2 == 1)
{
cout << "OH CRAP! You rolled snake eyes!" << endl;
cout << " Your turn is over. " << endl;
playerScore == 0;
}
The way you have the conditional written, it just checks to see if roll1 is anything besides zero (that's the part before &&) and then it checks if roll2 is equal to 1. The conditional should probably read: else if (roll1 == 1 && roll2 == 1).
And I think you want to assign (=) playerScore at the bottom rather than evaluate for equality (==). You already know this, but this is what it should look like: playerScore = 0.
I have created a Prompt User to Loop to restart the program however when I type anything including "y" it just goes prompts me with "Press any key to continue . . . " and then closes. Here's my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int Multiple1 = 0;
int Multiple2 = 0;
char again = 'Y';
while (again == 'y' || again == 'Y')
{
int Multiple1 = 0;
int Multiple2 = 0;
cout << "Input your first Multiple: " << endl;
cin >> Multiple1;
cout << "Your First Multiple is: " << Multiple1 << endl;
cout << "Input your second Multiple: " << endl;
cin >> Multiple2;
cout << "Your Second Multiple is: " << Multiple2 << endl;
for (int i = 1; i <= 100; i++)
{
if (i % Multiple1 == 0 && i % Multiple2 == 0)
cout << "FizzBuzz" << endl;
else
if (i % Multiple1 == 0)
cout << "Fizz" << endl;
else
if (i % Multiple2 == 0)
cout << "Buzz" << endl;
else cout << i << endl;
}
{
cout << "Restart? (y/n) ";
cin >> again;
}
system("pause");
return 0;
}
}
Thank you.
This is my first project and I am completely new to coding so I am not sure what to do.
Your return 0 is misplaced, it should be outside the while loop.
And you shouldn't put a bracket {} around your cin >> again.
{ // this bracket doesn't needed
cout << "Restart? (y/n) ";
cin >> again;
}
Just use bracket when it needed e.g. in for, if, function, etc. It will not make a compile error. But I think it makes you misread and think the while loop was closed.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int Multiple1 = 0;
int Multiple2 = 0;
char again = 'Y';
while (again == 'y' || again == 'Y')
{
int Multiple1 = 0;
int Multiple2 = 0;
cout << "Input your first Multiple: " << endl;
cin >> Multiple1;
cout << "Your First Multiple is: " << Multiple1 << endl;
cout << "Input your second Multiple: " << endl;
cin >> Multiple2;
cout << "Your Second Multiple is: " << Multiple2 << endl;
for (int i = 1; i <= 100; i++)
{
if (i % Multiple1 == 0 && i % Multiple2 == 0)
cout << "FizzBuzz" << endl;
else
if (i % Multiple1 == 0)
cout << "Fizz" << endl;
else
if (i % Multiple2 == 0)
cout << "Buzz" << endl;
else cout << i << endl;
}
{ //This bracket doesn't need.
cout << "Restart? (y/n) ";
cin >> again;
continue;
} // This bracket doesn't need.
system("pause");
return 0;
}
}
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.