Help with my text game?

closed account (iNU7ko23)
Hi I am writing a adventure game and will soon at classes and things but can you please tell me why I am getting these errors. The intro works but when it says you come across a monster(). The monster() function is supposed to output a random monster. And then I will soon at in a fighting system.
// The Adventure.cpp : Defines the entry point for the console application.
#include <cstdlib>
#include <ctime>
#include "stdafx.h"
#include <iostream>
#include <string>

using std::srand;
using std::rand;
using namespace std;
std::srand(std::time(0));




string name;
void monster();
int level;
bool fighting(level);
bool intro();
string name = " ";


int main( )
{
intro();
level =
cout << "You exit your kingdom to begin your long journey when you come across a " << monster();

return 0;
}

bool intro()
{
using std::cout;
using std::cin;
cout << "Hi! What is your name? \n";
cin >> name;
cout << "Hello! "<< name;
cout << ". We need you to defeat the evil dragon.\n";
cout << "To do this you will need to make a great journey.\n";
cout << "And then defeat the dragon at its source!\n";
cout << "Will you accept the challenge?\n";
cout << "1) Yes. \n";
cout << "2) No. \n\n";
int reply;
cin >> reply;
return !(reply == 1);
}


void monster()
{
int num = rand() % 3 + 1;
if(num = 1)
cout << "Ogre!";
name = "Ogre";
break;
else if (num = 2)
cout << "Dwarf!";
name = "Dwarf";
else if (num = 3)
cout << "Hippo!";
name = "Hippo";
}

closed account (zb0S216C)
In monster(), why are you using break when there's no loop? Also, if 2 or more statements belong to an if statement, enclose them in braces.

Wazzak
closed account (iNU7ko23)
@Framework

Thanks I changed it to this!

void monster()
{
int num = rand() % 3 + 1;
if(num = 1)
{
cout << "Ogre!";
name = "Ogre";
}
else if (num = 2)
{
cout << "Dwarf!";
name = "Dwarf";
}
else if (num = 3)
{
cout << "Hippo!";
name = "Hippo";
}

but there's still 4 errors...
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(1) : warning C4627: '#include <cstdlib>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(2) : warning C4627: '#include <ctime>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(10) : error C2039: 'time' : is not a member of 'std'
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(10) : error C3861: 'time': identifier not found
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(10) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Users\User\Documents\Visual Studio 2008\Projects\The Adventure\The Adventure\Debug\BuildLog.htm"
closed account (zb0S216C)
It's that pre-compiled header, stdafx. I can only assume you're using Visual C++. Here's what you need to do:

1) Take your source & header files (except stdafx) from the project directory and store them somewhere temporarily.
2) Create a new project, but this time, de-select pre-compiled headers. Then, add all of your headers and sources.
3) In your project options, navigate to the tree on the left-hand side.
4) Under advanced, in both release and debug, make sure the Use pre-compiled headers field is set to No.

Wazzak
You got lots of other mistakes in the code though once you fix the precompiled header thing.
You need to review basics by reading the tutorial on this site.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void monster()
{
int num = rand() % 3 + 1;
if(num = 1) // use == here and in the other comparisons in this function
{
cout << "Ogre!";
name = "Ogre";
}
else if (num = 2)
{
cout << "Dwarf!";
name = "Dwarf";
}
else if (num = 3)
{
cout << "Hippo!";
name = "Hippo";
}
} // 



1
2
level = // this statement is incomplete
cout << "You exit your kingdom to begin your long journey when you come across a " << monster(); // my compiler doesn't like void being passed to cout 
Last edited on
Topic archived. No new replies allowed.