ok so my problem is when a user enters a number from choices well it works fine the problem is if they enter a character it gets stuck in a continues loop of what was last outputed on the screen
// Text Adventure.cpp : main project file.
#include "stdafx.h"
#include <cstdlib>
#include <cstdio>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
int choice = 0;
int monster_crit;
int attack = 1;
int level = 1;
int abp = 1;
int real_damage;
int just_leveled = 1;
int strength = 1;
int exp = 0;
int store1_potions = 10;
int store1_Fire_Swords = 1;
int store1_strength_ups = 1;
int in_shop = 0;
int sword = 0;
int looked = 0;
int health = 100;
int damage = 10;
int world = 1;
int gold = 15;
int torch_lit = 0;
int world_1_swords = 1;
int trout_of_Auzur_hp = 2000;
int trout_of_Auzur = 1;
int potions = 3;
int Dead_Trout_of_Auzur_hp = 999999;
int Dead_Trout_of_Auzur = 1;
int just_killed = 1;
for(;;)
{
void leveling(int choice, int attack, int level, int abp, int just_leveled, int strength, int exp);
if(world == 1)
{
printf("you are in a dark room what do you do \n 1.Look around \n");
if(looked == 1)
{
if(torch_lit == 0)
{
printf(" 2.Light the torch \n");
}
if(world_1_swords == 1)
{
printf(" 3.Pickup the sword \n");
}
}
if(torch_lit == 1)
{
printf(" 4.Leave the room \n");
}
cin >> choice;
if (choice == 1)
{
}
if(choice == 1)
{
if(sword == 0)
{
printf("You see a sword\n");
}
if(sword == 1)
{
}
if(torch_lit == 0)
{
printf("You see a torch\n\n");
}
if(torch_lit == 1)
{
printf("You see a lit torch\n\n");
}
looked = 1;
}
else if(choice == 2)
{
printf("You light the torch you now see a door would you like to enter it 1.Yes 2.No \n");
torch_lit = 1;
scanf("%d", &choice);
if(choice == 1)
{
world = 2;
}
else if(choice == 2)
{
printf("You stay in the room \n\n");
world = 1;
}
else
{
printf("invalid command\n");
}
}
else if(choice == 3)
{
if(world_1_swords == 1)
{
printf("You have obtained a sword\n\n");
sword = 1;
damage = 100;
world_1_swords = 0;
}
}
else if(choice == 4)
{
if(torch_lit == 1)
{
world = 2;
}
}
}
if(world == 2)
{
if(trout_of_Auzur == 1)
{
printf("Old Man: Werdo your there I have a favor to ask will you go to the lake and kill the Trout of Auzur please\n1.Yes\n2.No\n");
scanf("%d", &choice);
if(choice == 1)
{
while(trout_of_Auzur == 1)
{
if(trout_of_Auzur_hp <=0)
{
trout_of_Auzur = 0;
exp += 100;
}
if(trout_of_Auzur == 1)
{
printf("1.Attack\n2.use potion\n");
scanf("%d", &choice);
if(choice == 1)
{
srand ( time(NULL) );
real_damage = rand() % (damage *2) + damage + (strength*2) + (strength*2);
monster_crit = rand() % 2 + 1;
if(monster_crit == 1)
{
health -= rand() % (10 + 8) *2;
}
else if(monster_crit == 2)
{
health -= rand() % 10 + 8;
}
else
{
}
trout_of_Auzur_hp -= real_damage;
}
if(choice == 2)
{
if(potions >= 1)
{
health += 75;
monster_crit = rand() % 2 + 1;
if(monster_crit == 1)
{
health -= rand() % (10 + 8) *2;
}
else if(monster_crit == 2)
{
health -= rand() % 10 + 8;
}
else
{
}
potions --;
}
if(potions <= 0)
{
}
}
else
{
}
cout << "Trout of Auzur hp: " <<trout_of_Auzur_hp << endl;
cout << "Your hp: " << health << endl;
if(health <= 0)
{
return 0;
}
}
}
}
else if(choice == 2)
{
}
}
if(trout_of_Auzur == 0 && Dead_Trout_of_Auzur == 1)
{
if(just_killed == 1)
{
potions += 10;
printf("Old Man: GREAT JOB WERDO!\n");
printf("Trout of Azuar: HISSSSS!\n");
printf("You have been ATTACKED!\n");
just_killed ++;
}
if(Dead_Trout_of_Auzur == 1)
{
if(Dead_Trout_of_Auzur_hp >= 1)
{
if(health >= 1)
{
cout << "Dead Trout of Auzur health: " << Dead_Trout_of_Auzur_hp << endl << "Your Health: " << health << endl;
printf("Press 1. to attack\nPress 2. to use a potion\n");
scanf("%d", &choice);
if(choice == 1)
{
Dead_Trout_of_Auzur_hp -= 0;
health -= 500;
}
if(choice == 2)
{
printf("The Trout stops you from using the potion\n");
Dead_Trout_of_Auzur_hp -= 0;
health -= 500;
}
}
if(health <= 0)
{
printf("Dead Trout of Auzur: you fool you should have never come here now you die\n");
printf("The old man jumps in front of the trouts magic and saves your life\n at the cost of his own ");
printf("After the Dead Trout of Auzur casts his last magic\nhe explodes and dies ");
printf("Right before the old man dies he casts a heal spell on you\n\n");
health += 461;
Dead_Trout_of_Auzur_hp -= 9999999;
}
}
if(Dead_Trout_of_Auzur_hp <= 0)
{
Dead_Trout_of_Auzur --;
world = 3;
}
}
while(world == 3)
{
if(in_shop == 0)
{
printf("You have managed to find your way to the town square what would you like to do\n 1. shop\n 2. leave\n");
scanf("%d", &choice);
if(choice == 1)
{
in_shop = 1;
while(in_shop == 1)
{
printf("press 4 to exit the store\n");
cout << "You have " << gold << " gold" << endl;
if(store1_potions >= 1)
{
cout << "I have " << store1_potions << " potions press 1 to buy a Potion for 15 gold\n";
}
if(store1_potions <= 0)
{
printf("I am very sorry to inform you that i have no more Potions\n");
}
if(store1_Fire_Swords >= 1)
{
cout << "I have " << store1_Fire_Swords << " Fire Swords press 2 to buy 1 for 500 gold\n";
}
if(store1_Fire_Swords <= 0)
{
printf("I am very sorry to inform you that i have no more Fire Swords\n");
}
if(store1_strength_ups >= 1)
{
cout << "I have " << store1_strength_ups << " Strength ups in stock press 3 to buy 1 for 1000 gold\n";
}
if(store1_strength_ups <= 0)
{
printf("I am very sorry to inform you that i have no more Strength ups\n");
}
scanf("%d", &choice);
if(choice == 4)
{
in_shop = 0;
world = 3;
}
else if(choice == 1)
{
if(gold >= 15)
{
store1_potions --;
potions ++;
gold -= 15;
}
else if(gold <= 14)
{
printf("Not enough gold\n");
potions += 0;
gold -= 0;
}
else
{
printf("HOW");
}
}
else
{
printf("Invalid command\n");
}
}
}
else if(choice == 2)
{
world = 4;
}
else
{
printf("Invalid Command");
}
}
}
if(world == 4)
{
printf("how?");
}
}
}
}
return 0;
}
so lets say you enter w at the first cin/scanf it goes you are in a dark room what do you do press 1. to look around FOREVER
// Text Adventure.cpp : main project file.
#include "stdafx.h"
#include <cstdlib>
#include <cstdio>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
int choice = 0;
int monster_crit;
int attack = 1;
int level = 1;
int abp = 1;
int real_damage;
int just_leveled = 1;
int strength = 1;
int exp = 0;
int store1_potions = 10;
int store1_Fire_Swords = 1;
int store1_strength_ups = 1;
int in_shop = 0;
int sword = 0;
int looked = 0;
int health = 100;
int damage = 10;
int world = 1;
int gold = 15;
int torch_lit = 0;
int world_1_swords = 1;
int trout_of_Auzur_hp = 2000;
int trout_of_Auzur = 1;
int potions = 3;
int Dead_Trout_of_Auzur_hp = 999999;
int Dead_Trout_of_Auzur = 1;
int just_killed = 1;
for(;;)
{
void leveling(int choice, int attack, int level, int abp, int just_leveled, int strength, int exp);
if(world == 1)
{
printf("you are in a dark room what do you do \n 1.Look around \n");
if(looked == 1)
{
if(torch_lit == 0)
{
printf(" 2.Light the torch \n");
}
if(world_1_swords == 1)
{
printf(" 3.Pickup the sword \n");
}
}
if(torch_lit == 1)
{
printf(" 4.Leave the room \n");
}
cin >> choice;
if (choice == 1)
{
}
if(choice == 1)
{
if(sword == 0)
{
printf("You see a sword\n");
}
if(sword == 1)
{
}
if(torch_lit == 0)
{
printf("You see a torch\n\n");
}
if(torch_lit == 1)
{
printf("You see a lit torch\n\n");
}
looked = 1;
}
else if(choice == 2)
{
printf("You light the torch you now see a door would you like to enter it 1.Yes 2.No \n");
torch_lit = 1;
scanf("%d", &choice);
if(choice == 1)
{
world = 2;
}
else if(choice == 2)
{
printf("You stay in the room \n\n");
world = 1;
}
else
{
printf("invalid command\n");
}
}
else if(choice == 3)
{
if(world_1_swords == 1)
{
printf("You have obtained a sword\n\n");
sword = 1;
damage = 100;
world_1_swords = 0;
}
}
else if(choice == 4)
{
if(torch_lit == 1)
{
world = 2;
}
}
}
if(world == 2)
{
if(trout_of_Auzur == 1)
{
printf("Old Man: Werdo your there I have a favor to ask will you go to the lake and kill the Trout of Auzur please\n1.Yes\n2.No\n");
scanf("%d", &choice);
if(choice == 1)
{
while(trout_of_Auzur == 1)
{
if(trout_of_Auzur_hp <=0)
{
trout_of_Auzur = 0;
exp += 100;
}
if(trout_of_Auzur == 1)
{
printf("1.Attack\n2.use potion\n");
scanf("%d", &choice);
if(choice == 1)
{
srand ( time(NULL) );
real_damage = rand() % (damage *2) + damage + (strength*2) + (strength*2);
monster_crit = rand() % 2 + 1;
if(monster_crit == 1)
{
health -= rand() % (10 + 8) *2;
}
else if(monster_crit == 2)
{
health -= rand() % 10 + 8;
}
else
{
}
trout_of_Auzur_hp -= real_damage;
}
if(choice == 2)
{
if(potions >= 1)
{
health += 75;
monster_crit = rand() % 2 + 1;
if(monster_crit == 1)
{
health -= rand() % (10 + 8) *2;
}
else if(monster_crit == 2)
{
health -= rand() % 10 + 8;
}
else
{
}
potions --;
}
if(potions <= 0)
{
}
}
else
{
}
cout << "Trout of Auzur hp: " <<trout_of_Auzur_hp << endl;
cout << "Your hp: " << health << endl;
if(health <= 0)
{
return 0;
}
}
}
}
else if(choice == 2)
{
}
}
if(trout_of_Auzur == 0 && Dead_Trout_of_Auzur == 1)
{
if(just_killed == 1)
{
potions += 10;
printf("Old Man: GREAT JOB WERDO!\n");
printf("Trout of Azuar: HISSSSS!\n");
printf("You have been ATTACKED!\n");
just_killed ++;
}
if(Dead_Trout_of_Auzur == 1)
{
if(Dead_Trout_of_Auzur_hp >= 1)
{
if(health >= 1)
{
cout << "Dead Trout of Auzur health: " << Dead_Trout_of_Auzur_hp << endl << "Your Health: " << health << endl;
printf("Press 1. to attack\nPress 2. to use a potion\n");
scanf("%d", &choice);
if(choice == 1)
{
Dead_Trout_of_Auzur_hp -= 0;
health -= 500;
}
if(choice == 2)
{
printf("The Trout stops you from using the potion\n");
Dead_Trout_of_Auzur_hp -= 0;
health -= 500;
}
}
if(health <= 0)
{
printf("Dead Trout of Auzur: you fool you should have never come here now you die\n");
printf("The old man jumps in front of the trouts magic and saves your life\n at the cost of his own ");
printf("After the Dead Trout of Auzur casts his last magic\nhe explodes and dies ");
printf("Right before the old man dies he casts a heal spell on you\n\n");
health += 461;
Dead_Trout_of_Auzur_hp -= 9999999;
}
}
if(Dead_Trout_of_Auzur_hp <= 0)
{
Dead_Trout_of_Auzur --;
world = 3;
}
}
while(world == 3)
{
if(in_shop == 0)
{
printf("You have managed to find your way to the town square what would you like to do\n 1. shop\n 2. leave\n");
scanf("%d", &choice);
if(choice == 1)
{
in_shop = 1;
while(in_shop == 1)
{
printf("press 4 to exit the store\n");
cout << "You have " << gold << " gold" << endl;
if(store1_potions >= 1)
{
cout << "I have " << store1_potions << " potions press 1 to buy a Potion for 15 gold\n";
}
if(store1_potions <= 0)
{
printf("I am very sorry to inform you that i have no more Potions\n");
}
if(store1_Fire_Swords >= 1)
{
cout << "I have " << store1_Fire_Swords << " Fire Swords press 2 to buy 1 for 500 gold\n";
}
if(store1_Fire_Swords <= 0)
{
printf("I am very sorry to inform you that i have no more Fire Swords\n");
}
if(store1_strength_ups >= 1)
{
cout << "I have " << store1_strength_ups << " Strength ups in stock press 3 to buy 1 for 1000 gold\n";
}
if(store1_strength_ups <= 0)
{
printf("I am very sorry to inform you that i have no more Strength ups\n");
}
scanf("%d", &choice);
if(choice == 4)
{
in_shop = 0;
world = 3;
}
else if(choice == 1)
{
if(gold >= 15)
{
store1_potions --;
potions ++;
gold -= 15;
}
else if(gold <= 14)
{
printf("Not enough gold\n");
potions += 0;
gold -= 0;
}
else
{
printf("HOW");
}
}
else
{
printf("Invalid command\n");
}
}
}
else if(choice == 2)
{
world = 4;
}
else
{
printf("Invalid Command");
}
}
}
if(world == 4)
{
printf("how?");
}
}
}
}
return 0;
}
}
Reading the value of std::cin straight into an integer is generally a bad idea (as various jumbles of text looping for an eternity has taught me.) A much safer, less error prone way to go about this is to input the values into a stringstream before hand and then stream into the variable like so.
#include <iostream>
#include <string>
#include <sstream>
int main()
{
int choice = 0;
std::string buffer;
std::stringstream ss;
std::cin >> buffer;
ss << buffer;
ss >> choice;
ss.str(""); //This clears the streams contents. You'll need to do this every time you want to reuse it.
if(ss.fail() ) {
//Do something about it
}
//And so on..
return 0;
}
And please add some indentation to your code. It takes a tad more effort ( namely pressing tab and space... ) but if I can give a solution with correct spelling after just waking up then you can press those damn keys
Heck. Most IDEs let you highlight a block of text and indent it all at once.
I hope this post helps ( and improves the style of code.)
EDIT: Okay. So it seems being half awake led me to right some potentially non-working code so here's a quick edit. Consider looking into a better way of swapping data between the streams than a std::string acting as a buffer.
What really is happening (in your original code) is that when the user enters a character, characters are really integers. You can see each characters corresponding value on the ascii table http://www.asciitable.com/
Once the user enters a character, it never satisfies the if statements you have an loops forever because of your for(;;) statements.
So sorry bout poor format i switch houses alot so i need to email my self the code and then it kinda gets rid of all my tabs and messes up how everything is placed
werdo659 wrote:So sorry bout poor format i switch houses alot so i need to email my self the code and then it kinda gets rid of all my tabs and messes up how everything is placed
get yourself a login for dropbox. you get 2gb free and its a nice way for quick access to your files, and more importantly as a backup
My friend had a problem with something similar as well. To fix it, just set it's value to NULL after you're done using it. I believe that's how we fixed it