#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <string>
#include <stdlib.h>
#include <time.h>
usingnamespace std;
int gnome = 10;
int bear = 60;
int randn = 1;
void lossfunc(int hhealth, int loss, string heroname, string monster, int monsterhealth, int monsterloss)
{
int monsterhealthremaining = monsterhealth - monsterloss;
int healthremaining = hhealth - loss;
cout << heroname << " has " << hhealth << " health, after the attack he has " << healthremaining << " health remaining!\n";
cout << monster << " has " <<monsterhealth << " health, after the attack he has " << monsterhealthremaining << " health remaining!\n";
}
void main()
{
string heroname;
std::cout << "Type in the name of the Hero = ";
getline(cin, heroname);
string monster; //*****THERE MIGHT BE THE PROBLEM, I DON'T KNOW*****
if (randn = 0)
{
string monster = "Gnome";
}
else
{
string monster = "Bear";
}
std::cout << "You will fight against a " << monster << "\n";
int hhealth;
std::cout << heroname <<" has 100 health points by default.\n";
hhealth = 100;
int loss;
std::cout << "Type in the amount of health " << heroname << " will lose = ";
std::cin >> loss;
int monsterhealth;
if (int randn = 0)
{
monsterhealth = 10;
}
else
{
monsterhealth = 60;
}
std::cout << monster << " has " << monsterhealth << " health points\n";
int monsterloss;
cout << "Type in the amount of health " << monster << " will lose = ";
cin >> monsterloss;
int mhr = monsterhealth - monsterloss;
int hhr = hhealth - loss;
if (mhr <= 0 && hhr <= 0)
cout << "Stalemate!\n";
elseif (mhr <= 0)
cout << heroname << " wins! ";
else cout << monster <<" wins! ";
lossfunc(hhealth, loss, heroname, monster, monsterhealth, monsterloss);
_getch();
}
Okay. I wanted to implement a monster-finding-feature-thing that will pick an opponent for the player. I started from creating a integer that I would manually switch through the compiler (from 1 to 0 to check if it works).
Unfortunately when the game is supposed to pick the monster name (string monster) after it's supposed to do that, there's nothing. It's like " [thenameissupposedtobehere] has 60 health".
What did I do wrong?
OBJECTS OBJECTS OBJECTS!!!! I'm sorry but this is the second post in a row where I see people doing this the hard way just because they don't know any better!
- First of all you are already declaring that you are using the std namespace in the global space so why do you type "std::cout" and "std::cin"? You don't have to answer me but this is annoying.
- Line 28 should be: if (randn == 0) remember that "=" is assignment and "==" is comparison.
Thanks for the quick reply.
I know I used namespace. I started writing this program about 6 monthsa go and took a break from programming and simply forgot about it.
Oh. Strange. I was putting the second = there before I made this thread and it had a red underline. Hm.
Thanks.
Wait, the name still doesn't appear.
1 2 3 4 5 6 7 8 9 10
string monster;
if (randn == 0)
{
string monster ="Gnome";
}
else
{
string monster = "Bear";
}
std::cout << "You will fight against a " << monster << "\n";
Also, I accidentally pressed a button in Visual C++ (2009) and whenever click within a parenthesis there's a grey bar. How do I turn it off?
string monster; //*****THERE MIGHT BE THE PROBLEM, I DON'T KNOW*****
if (randn == 0)
{
string monster = "Gnome";
}
else
{
string monster = "Bear";
}
You are declaring more than one string called monster at different scopes. The string you're setting to "Gnome" is not the same one you declared outside of the if(). It vanishes at the end of the scope, with the one you're interested in being left blank. Same for the other one being set to "Bear".
It should be
1 2 3 4 5 6 7 8 9
string monster; //*****THERE MIGHT BE THE PROBLEM, I DON'T KNOW*****
if (randn == 0)
{
monster = "Gnome";
}
else
{
monster = "Bear";
}
Line 27 declares the string monster using the default constructor and hence it is set to an empty string.
Line 30 declares another string called monster which hides the first declaration. This is then set to "Gnome" and immediately goes out of scope and is destroyed. The same thing happens in the else clause lines 32-35.
Consequently when line 36 is executed the only variable named monster that is visible is the one declared on line 27 which is still an empty string.
Change line 30 from:
string monster = "Gnome"; to monster = "Gnome";
and line 34 from string monster = "Bear"; to monster = "Bear";
Actually, main() is the one case where the return code is provided automatically if you don't provide it. If there is no explicit return, zero is returned from main()