Hey, I'm new to c++ and I was in the midst of creating a fighting game with functions. For some reason (probably stupid) I generate a compile error (too few arguments for bumChoice();). Here's the code:
#include <iostream>
#include <string>
using namespace std;
int randNum()
{
srand(time(0));
}
int bumChoice (int therandNum)
{ int bumchoice = therandNum%4+1;
return bumchoice;
}
int main()
{ string endgame;
int therandNum = randNum();
int bum_choice = bumChoice();
cout << bum_choice ;
cin >> endgame;
return 0;
}
If anyone could could tell me what I'm doing wrong I would appreciate it, since I can't seem to find out what I'm doing wrong. By the way I'm using a Bloodshed Dev compiler.
Also, in randNum() you're setting a rand seed and not returning anything. You should call srand(time(0)); within main() once, then to get a random number you would use rand().
Bazzy, could you show me how the code would look? Thanks for your help by the way. Also, pabloist thanks usually I seed it and then use rand() but I'm going to use a lot of random numbers, different each time a while loop is executed.
#include <iostream>
#include <string>
usingnamespace std;
int randNum() {
srand(time(0));
}
int bumChoice (int therandNum) {
int bumchoice = therandNum%4+1;
return bumchoice;
}
int main() {
string endgame;
int therandNum = randNum();
int bum_choice = bumChoice(therandNum);
cout << bum_choice ;
cin >> endgame;
return 0;
}
When you pass a variable to a function, you do NOT need to have that variable have the same name, or even have it be a variable. For instance, where you have:
1 2
int therandNum = randNum();
int bum_choice = bumChoice(therandNum);
,
this would also work:
1 2
int anotherVariableName = randNum();
int bum_choice = bumChoice(anotherVariableName);
.
Even better, you could just use: int bum_choice = bumChoice(randNum());.
Hey thanks everybody for your help! Here's the finished code for my game (albeit ugly and immature). I really just wanted practice with functions. Anyways, the one function that doesn't work is pause(). I tried both cin.ignore and cin.get to wait for the user to press enter but neither worked. If anyone can figure it out I would sure appreciate it.
int CHOICE();
int BUMHURT(int yourchoice);
int HURT();
int HP(int hurt);
int BUMHP(int bumhurt);
void pause();
int main()
{
char meanstreets = INTRO();
if (meanstreets == 'y')
{
while (hp >= 1 && bumHP >= 1)
{
int yourchoice = CHOICE();
int bumhurt = BUMHURT(yourchoice);
bumHP = BUMHP(bumhurt);
if (bumHP < 1)
break;
pause();
int hurt = HURT();
hp = HP(hurt);
if (hp < 1)
break;
pause();
}
if (hp < 1)
{ cout << "\nYou have died the lonely death of a BUM!\n\n"
"GAME OVER...";
}
if (bumHP < 1)
{ cout << "\nYou have earned the right to smoke that rock.\n\n"
"FLYING HIGH NOW!";
}
cin >> endgame;
}
else if (meanstreets == 'n')
{
cout << "\nI always knew you were a COWARD!!";
cin >> endgame;
}
return 0;
}
char INTRO()
{
char dec;
cout << "Bum Fights, A Wagner Production\n\n"
"There's only enough spare change for\n"
"one vagabond to get his crack rock.\n"
"Will you fight for your right to beg?\n\n"
"Fight (y / n): "
;
cin >> dec;
return dec;
}
int CHOICE()
{
int choice;
cout << "\nWhat to do:\n"
"1)Shank a Nigga\n"
"2)Kick\n"
"3)Punch\n"
"4)Bite\n"
"5)Stanky Breath\n\n"
"(1 - 5)? ";
cin >> choice;
return choice;
}
int BUMHURT(int yourchoice)
{
int atk, def, agi, bumAgi, bumDef, bumhurt;
bumAgi = 5;
bumDef = 20;
int HURT()
{
int bumAtk, bumDef, bumAgi, def, agi, hurt;
def = 20;
agi = 5;
srand(time(0));
int bumChoice = rand()%4+1;
if (bumChoice == 1)
{
bumAtk = rand()%20+20;
bumDef = rand()%15+10;
bumAgi = rand()%5;
cout << "The bum's sharpened plastic knife cuts into you\n";
}
if (bumChoice == 2)
{
bumAtk = rand()%10+10;
bumDef = rand()%10+10;
bumAgi = rand()%7;
cout << "The bum's stanky leg knocks you over\n";
}
if (bumChoice == 3)
{
bumAtk = rand()%15+15;
bumDef = rand()%15+15;
bumAgi = rand()%10;
cout << "The bum hits you with a stiff uppercut\n";
}
if (bumChoice == 4)
{
bumAtk = rand()%30+10;
bumDef = rand()%25+5;
bumAgi = rand()%7+7;
cout << "The bum grabs a rock and smashes you in the head\n";
}
}
int BUMHP(int bumhurt)
{
bumHP = bumHP - bumhurt;
if(bumHP >= 1)
{cout << "\nYou did " <<bumhurt<< " damage.\n"
<< "The bum has " <<bumHP<< " hp left.\n\n";
return bumHP;}
else
return bumHP;
}
int HP(int hurt)
{
hp = hp - hurt;
if(hp >= 1)
{cout << "\nThe bum did " <<hurt<< " damage.\n"
<< "You have " <<hp<< " hp left.\n";
return hp;}
else
return hp;
}