I'm coding a game right now and everything works great that i want to have working right now, but there's a minor bug that i know of.
this is that when the player enters their name, and the name has spaces, the code just closes.
here's the part of code:
char name [40]; // lets the players name be up to 40 characters
int main (){
cout << "Hello, Welcome." << endl; // title screen
cout << "What do you want your name to be?" << endl;
cin >> name;
system ("cls");
cout << "Congratulations " << name << " What do you want your class to be?" << endl; // this will determine base stats
cout << "1. Warrior" << endl;
cout << "2. Mage" << endl;
cout << "3. Healer" << endl;
cin >> typeofclass;
if (typeofclass == 1){
system ("cls");
yourclass = 'Warrior';
cout << "You have chosen to be a Warrior.\n" << endl; // gives stats for this person
health = 500;
mana = 10;
str = 25;
def = 25;
}
else if (typeofclass == 2){
system ("cls");
yourclass = 'Mage';
cout << "You have chosen to be a Mage.\n" << endl;
health = 250;
mana = 50;
str = 10;
def = 10;
}
else if (typeofclass == 3){
system ("cls");
yourclass = 'Healer';
cout << "You have chosen to be a Healer.\n";
health = 250;
mana = 40;
str = 5;
def = 5;
}
else {
cout << "Sorry, thats not a right number, please try again." << endl;
}
cout << "Your Stats are:" << endl << endl;
cout << "Health " << health << endl << endl << "Mana " << mana << endl << endl;
cout << "Strength " << str << endl << endl << "Defense " << def << endl << endl;
cout << "Press Enter to continue.\n \n";
cin.ignore();
cin.get();
mainmenu();
cin.ignore();
cin.get();
}Use a string for the name instead of char and if you want to allow spaces use getline(cin, name) instead of cin.
This is what I usually use:
string name;
cout << "Enter name: ";
getline(cin,name);
But you migh also wanna try cin.getline(name, 40);
If you want to limit to 40 chars. Not 100% sure it works with a string though.
More: http://www.cplusplus.com/reference/iostream/istream/getline/
ok i get that now, thanks but theres one other problem ive been having with my program, its not really a error its more of a, i cant find any tutorials on how to do it.
random numbers, ive looked online on youtube on google etc, and i cant find one that actually works, is a random number.
Include these libraries:
#include <cstdlib>
#include <ctime>
This will initialize the use of rand function:
srand((unsigned)time(NULL));
Now to generate a random number use:
int i = rand() % n
This will generate a number between 0 and n-1
So let's say you use
int i = rand() % 3 i will be either 0, 1 or 2.
i was just looking on youtube and google for a good amount of time and i found this:
srand(static_cast<unsigned>(time(0)));
thats basically what you said and i also figured out i can use:
bob = rand() % 10 + 1; bob is just a example int.
and when i put the % 10 thats from 1- 10 but if i put it to:
% 10 + 1 that means it can do 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10.
is that right?
Thanks and I was working on my code while adding the random numbers and if you want to try it out here it is:
There's some bugs i know I'm still working on the code i just wanted to get feedback on what some people thought.
here's the code:
/*Coded by Josh*/
# include <iostream>
# include <cmath>
# include <ctime>
# include <conio.h>
using namespace std;
int points = 3;
int loot = 0;
int money;
int dmg;
int mdmg;
char yourclass;
string name; // lets the players name be up to 40 characters
int choice;
int goblin = 25; // monsters health.
int dwarf = 50;
int troll = 75;
int giant = 100;
int glvl = 1; // monsters level
int dlvl = 2;
int tlvl = 3;
int Glvl = 4;
int typeofclass;
int health;
int mana;
int str;
int def;
int expe;
int lvl = 1; // will require set ammount of exp.
void attpoints();
void mainmenu();
void shop ();
void rest ();
void arena ();
void fightg (){
srand(static_cast<unsigned>(time(0)));
goblin = 25;
// do loop here, until the monsters health is <= 0
system ("cls");
if (health <= 0){
cout << "You have died." << endl; // if you died from the fight then it will return to the main menu
cout << "Returning to Main menu." << endl; // if not it will ignore this
mainmenu();
}
else {
do {
system ("cls");
cout << "Your health is: " << health << endl << endl;
cout << "The Goblin's heath is: " << goblin << endl << endl << endl;
cout << "Do you want to: " << endl;
cout << "1. Fight." << endl;
cout << "2. Heal." << endl;
cout << "3. Go to Main menu." << endl;
cin >> choice;
if (choice == 1){
system ("cls");
mdmg = rand() % 10 + 1;
mdmg = mdmg * glvl;
dmg = rand() % 10 + 1;
dmg = dmg * str;
cout << "You hit your enemy for: " << dmg << endl << endl << endl;
health = health - mdmg;
goblin = goblin - dmg;
cout << "Your enemy hit you for:" << mdmg << endl << endl << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// press any key code to go back to the main fighting menu.
}
else if (choice == 2){
system ("cls") ;
cout << "You have chosen to heal." << endl << endl;
// random healing between 1 and 10
// mana devided by 10
// random healing multiplied by the 1/10th mana
// add that number to health
cout << "Your enemy hit you for:" << endl << endl;
// will do random between 1 and 10
// multiply this by the monsters level
// minus this from the health
cout << "Your health is now: " << health << endl << endl;
cout << endl << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// press any key code to go back to the main fighting menu.
}
else if (choice == 3) mainmenu ();
else {
cout << "That is not a choice. please try again." << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
}
} while (goblin > 0);
}
if (goblin <= 0){
expe = expe + rand() % 10 + 1;
expe = expe * glvl;
if (expe >= 50){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=100){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=150){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=200){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=300){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=400){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
}
system("cls");
cin.ignore();
loot = rand() % 100 + 1;
loot = loot * glvl;
cout << "You have killed a Goblin!\n";
cout << "\nYour loot is: ";
cout << loot << " Gold\n\n";
cout << "You also got: " << expe << " experience.\n\n";
cout << "\nPress Enter to continue.\n\n";
money = money + loot;
cin.get();
mainmenu();
}
void fightd () {
dwarf = 50;
// do loop here, until the monsters health is <= 0
system ("cls");
if (health <= 0){
cout << "You have died." << endl; // if you died from the fight then it will return to the main menu
cout << "Returning to Main menu." << endl; // if not it will ignore this
mainmenu();
}
else {
do {
system ("cls");
cout << "Your health is: " << health << endl << endl;
cout << "The Dwarf's heath is: " << dwarf << endl << endl << endl;
cout << "Do you want to: " << endl;
cout << "1. Fight." << endl;
cout << "2. Heal." << endl;
cout << "3. Go to Main menu." << endl;
cin >> choice;
if (choice == 1){
system ("cls");
mdmg = rand() % 10 + 1;
mdmg = mdmg * dlvl;
dmg = rand() % 10 + 1;
dmg = dmg * str;
cout << "You hit your enemy for: " << dmg << endl << endl << endl;
health = health - mdmg;
dwarf = dwarf - dmg;
cout << "Your enemy hit you for:" << mdmg << endl << endl << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// press any key code to go back to the main fighting menu.
}
else if (choice == 2){
system ("cls") ;
cout << "You have chosen to heal." << endl;
// random healing between 1 and 10
// mana devided by 10
// random healing multiplied by the 1/10th mana
// add that number to health
cout << "Your enemy hit you for:" << endl;
// will do random between 1 and 10
// multiply this by the monsters level
// minus this from the health
cout << "Your health is now: " << health << endl;
cout << endl << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// press any key code to go back to the main fighting menu.
}
else if (choice == 3) mainmenu ();
else {
cout << "That is not a choice. please try again." << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// will go back to the start of the loop again.
}
}
while (dwarf > 0);
}
if (dwarf <= 0){
expe = expe + rand() % 10 + 1;
expe = expe * glvl;
if (expe >= 50){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=100){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=150){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=200){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=300){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=400){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
}
system("cls");
cin.ignore();
loot = rand() % 100 + 1;
loot = loot * dlvl;
cout << "You have killed a Dwarf!\n";
cout << "\nYour loot is: ";
cout << loot << " Gold\n\n";
cout << "You also got: " << expe << " experience.\n\n";
cout << "\nPress Enter to continue.\n\n";
money = money + loot;
cin.get();
mainmenu();
}
void fightt () {
troll = 75;
// do loop here, until the monsters health is <= 0
system ("cls");
if (health <= 0){
cout << "You have died." << endl; // if you died from the fight then it will return to the main menu
cout << "Returning to Main menu." << endl; // if not it will ignore this
mainmenu();
}
else {
do {
system ("cls");
cout << "Your health is: " << health << endl << endl;
cout << "The Troll's heath is: " << troll << endl << endl << endl;
cout << "Do you want to: " << endl;
cout << "1. Fight." << endl;
cout << "2. Heal." << endl;
cout << "3. Go to Main menu." << endl;
cin >> choice;
if (choice == 1){
system ("cls");
mdmg = rand() % 10 + 1;
mdmg = mdmg * tlvl;
dmg = rand() % 10 + 1;
dmg = dmg * str;
cout << "You hit your enemy for: " << dmg << endl << endl << endl;
health = health - mdmg;
troll = troll - dmg;
cout << "Your enemy hit you for:" << mdmg << endl << endl << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// press any key code to go back to the main fighting menu.
}
else if (choice == 2){
system ("cls") ;
cout << "You have chosen to heal." << endl;
// random healing between 1 and 10
// mana devided by 10
// random healing multiplied by the 1/10th mana
// add that number to health
cout << "Your enemy hit you for:" << endl;
// will do random between 1 and 10
// multiply this by the monsters level
// minus this from the health
cout << "Your health is now: " << health << endl;
cout << endl << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// press any key code to go back to the main fighting menu.
}
else if (choice == 3) mainmenu ();
else {
cout << "That is not a choice. please try again." << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// will go back to the start of the loop again.
}
}
while (troll > 0);
}
if (troll <= 0){
expe = expe + rand() % 10 + 1;
expe = expe * glvl;
if (expe >= 50){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=100){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=150){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=200){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=300){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=400){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
}
system("cls");
cin.ignore();
loot = rand() % 100 + 1;
loot = loot * tlvl;
cout << "You have killed a Troll!\n";
cout << "\nYour loot is: ";
cout << loot << " Gold\n\n";
cout << "You also got: " << expe << " experience.\n\n";
cout << "\nPress Enter to continue.\n\n";
money = money + loot;
cin.get();
mainmenu();
}
void fightG () {
giant = 100;
// do loop here, until the monsters health is <= 0
system ("cls");
if (health <= 0){
cout << "You have died." << endl; // if you died from the fight then it will return to the main menu
cout << "Returning to Main menu." << endl; // if not it will ignore this
mainmenu();
}
else {
do {
system("cls");
cout << "Your health is: " << health << endl << endl;
cout << "The giants's heath is: " << giant << endl << endl << endl;
cout << "Do you want to: " << endl;
cout << "1. Fight." << endl;
cout << "2. Heal." << endl;
cout << "3. Go to Main menu." << endl;
cin >> choice;
if (choice == 1){
system ("cls");
mdmg = rand() % 10 + 1;
mdmg = mdmg * Glvl;
dmg = rand() % 10 + 1;
dmg = dmg * str;
cout << "You hit your enemy for: " << dmg << endl << endl << endl;
health = health - mdmg;
giant = giant - dmg;
cout << "Your enemy hit you for:" << mdmg << endl << endl << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// press any key code to go back to the main fighting menu.
}
else if (choice == 2){
system ("cls") ;
cout << "You have chosen to heal." << endl;
// random healing between 1 and 10
// mana devided by 10
// random healing multiplied by the 1/10th mana
// add that number to health
cout << "Your enemy hit you for:" << endl;
// will do random between 1 and 10
// multiply this by the monsters level
// minus this from the health
cout << "Your health is now: " << health << endl;
cout << endl << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// press any key code to go back to the main fighting menu.
}
else if (choice == 3) mainmenu ();
else {
cout << "That is not a choice. please try again." << endl;
cout << "To continue fighing press Enter." << endl;
cin.ignore();
cin.get();
// will go back to the start of the loop again.
}
}
while (giant > 0);
}
if (giant <= 0){
expe = expe + rand() % 10 + 1;
expe = expe * glvl;
if (expe >= 50){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=100){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=150){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=200){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=300){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
else if (expe >=400){
lvl = lvl + 1;
points = points + 3;
cout << "Congratulations" << name << "\n";
cout << "You have Level up!\n";
cout << "You are now level: " << lvl << "\n";
}
}
system("cls");
cin.ignore();
loot = rand() % 100 + 1;
loot = loot * Glvl;
cout << "You have killed a Giant!\n";
cout << "\nYour loot is: ";
cout << loot << " Gold\n\n";
cout << "You also got: " << expe << " experience.\n\n";
cout << "\nPress Enter to continue.\n\n";
money = money + loot;
cin.get();
mainmenu();
}
void mainmenu (){
int choice;
int goblin = 250; // monsters health.
int dwarf = 500;
int troll = 750;
int giant = 1000;
int glvl = 1; // monsters level
int dlvl = 2;
int tlvl = 3;
int Glvl = 4;
system ("cls");
cout << "Main menu." << endl << endl; // Main menu
cout << "You have: " << money << " Gold.\n\n";
cout << "You have: " << points << " Attribute points.\n\n";
cout << "What would you like to do " << name << "?" << endl << endl;
cout << "1. Fight." << endl << endl;
cout << "2. Go to the Shop." << endl << endl;
cout << "3. Rest." << endl << endl;
cout << "4. Go to the Arena." << endl << endl;
if (points > 0)
cout << "5. Spent Attribute Points.\n";
cin >> choice;
if (choice == 1){
do {
system ("cls");
cout << "You have chosen to fight." << endl; // who they choose to fight
cout << "Who would you like to fight?" << endl;
cout << "1. Goblin." << endl;
cout << "2. Dwarf." << endl;
cout << "3. Troll." << endl;
cout << "4. Giant." << endl;
cin >> choice;
if (choice == 1){
fightg();
}
else if (choice == 2){
fightd();
}
else if (choice == 3){
fightt();
}
else if (choice == 4){
fightG();
}
else {
cout << "That is not a choice. please try again." << endl;
cout << "\nPress Enter to retry.\n";
cin.ignore();
cin.get();
// will go back to the start of the loop again.
}
} while (choice != 1 || 2 || 3 || 4);
}
else if (choice == 2) {
shop();
}
else if (choice == 3) {
rest();
}
else if (choice == 4) {
arena();
}
else if (choice == 5){
attpoints();
}
else {
cout << "That is not a choice. please try again." << endl;
// will go back to the start of the loop again.
}
}
int main (){
cout << "Hello, Welcome." << endl; // title screen
cout << "What do you want your name to be?" << endl;
getline(cin,name);
system ("cls");
cout << "Congratulations " << name << " What do you want your class to be?" << endl; // this will determine base stats
cout << "1. Warrior" << endl;
cout << "2. Mage" << endl;
cout << "3. Healer" << endl;
cin >> typeofclass;
if (typeofclass == 1){
system ("cls");
yourclass = 'Warrior';
cout << "You have chosen to be a Warrior.\n" << endl; // gives stats for this person
health = 50;
mana = 1;
str = 3;
def = 3;
}
else if (typeofclass == 2){
system ("cls");
yourclass = 'Mage';
cout << "You have chosen to be a Mage.\n" << endl;
health = 25;
mana = 5;
str = 1;
def = 1;
}
else if (typeofclass == 3){
system ("cls");
yourclass = 'Healer';
cout << "You have chosen to be a Healer.\n";
health = 25;
mana = 4;
str = 1;
def = 1;
}
else {
cout << "Sorry, thats not a right number, please try again." << endl;
}
cout << "Your Stats are:" << endl << endl;
cout << "Health " << health << endl << endl << "Mana " << mana << endl << endl;
cout << "Strength " << str << endl << endl << "Defense " << def << endl << endl;
cout << "Press Enter to continue.\n \n";
cin.ignore();
cin.get();
mainmenu();
cin.ignore();
cin.get();
}
void shop(){
system ("cls");
cout << "A work in progress" << endl;
cout << "\nPress Enter to Go to the main menu \n";
cin.ignore();
cin.get();
mainmenu();
}
void rest() {
system ("cls");
cout << "A work in progress" << endl;
cout << "\nPress Enter to Go to the main menu \n";
cin.ignore();
cin.get();
mainmenu();
}
void arena() {
system ("cls");
cout << "A work in progress" << endl;
cout << "\nPress Enter to Go to the main menu \n";
cin.ignore();
cin.get();
mainmenu();
}
void attpoints(){
bool done = false;
while (done != true){
if (points <= 0){
system ("cls");
cout << "You have no more Points.\n";
cout << "Press Enter to return to the Main Menu.\n";
cin.ignore();
cin.get();
mainmenu();
}
system ("cls");
cout << "You have: " << points << " Points left.\n\n";
cout << "Your Stats are:" << endl << endl;
cout << "Health " << health << endl << endl << "Mana " << mana << endl << endl;
cout << "Strength " << str << endl << endl << "Defense " << def << endl << endl;
cout << "Would you like to:" << endl;
cout << "\n1. Add 10 to Your Health.";
cout << "\n2. Add 1 to str.";
cout << "\n3. Add 1 to def.";
cout << "\n4. Add 1 to mana str.\n";
cout << "5. Go to main menu.\n\n";
cin >> choice;
cout << "\n";
switch (choice){
case 1:
system ("cls");
points = points - 1;
health = health + 10;
cout << "You added 10 to your health, Your health is now: " << health << ".\n";
cout << "\nPress Enter to add more.\n";
cin.ignore();
cin.get();
break;
case 2:
system ("cls");
points = points - 1;
str = str + 1;
cout << "You added 1 to your Strength, Your Strength is now: " << str << ".\n";
cout << "\nPress Enter to add more.\n";
cin.ignore();
cin.get();
break;
case 3:
system ("cls");
points = points - 1;
def = def + 1;
cout << "You added 1 to your Defense, Your Defense is now: " << def << ".\n";
cout << "\nPress Enter to add more.\n";
cin.ignore();
cin.get();
break;
case 4:
system ("cls");
points = points - 1;
mana = mana + 1;
cout << "You added 1 to your Mana, Your Mana is now: " << mana << ".\n";
cout << "\nPress Enter to add more.\n";
cin.ignore();
cin.get();
break;
case 5:
done = true;
break;
}
}
cout << "\nPress Enter to Go to the main menu \n";
cin.ignore();
cin.get();
mainmenu();
}