Trouble with Functions

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.
You declared bumChoice to take one argument but you are passing none when calling it from main
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.
The code should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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(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());.

Last edited on
Your int randNum() function does not return anything.

Also, move your srand(time(0)) to be the first line of main.
1
2
3
4
int main() {
    string endgame;
    srand(time(0));
    int therandNum = rand();

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.

//Bumfights Revisted (Using Functions)

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

string endgame;
int hp = 150;
int bumHP = 150;

char INTRO();

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;

srand(time(0));


if (yourchoice == 1)
{
atk = rand()%20+20;
def = rand()%15+10;
agi = rand()%5;
cout << "Blood spills out\n";
}

if (yourchoice == 2)
{
atk = rand()%10+10;
def = rand()%10+10;
agi = rand()%7;
cout << "You hit 'em right in the nuts\n";
}

if (yourchoice == 3)
{
atk = rand()%15+15;
def = rand()%15+15;
agi = rand()%10;
cout << "Your southpaw prevails\n";
}

if (yourchoice == 4)
{
atk = rand()%50+5;
def = rand()%5+5;
agi = rand()%3;
cout << "You bite him like Mike Tyson in a famine\n";
}

if (yourchoice == 5)
{
atk = rand()%20+5;
def = rand()%10+1;
agi = rand()%5;
cout << "Your breath surrounds your opponent\n";
}
bumhurt = (atk - bumAgi) - (bumDef/atk);
if (bumhurt < 0)
{ bumhurt = 0;}

return bumhurt;

}

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";
}




hurt = (bumAtk - agi) - (def/bumAtk);
if (hurt < 0)
{hurt = 0;}

return hurt;

}
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;
}

void pause()
{
cin.get();
}

Topic archived. No new replies allowed.