Help with my text game?

closed account (iNU7ko23)
Here is my code please help me with the errors!?
/ 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 std::time;

srand(time(0));
using namespace std;
srand(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";
}

and here are the errors...

1>------ Build started: Project: The Adventure, Configuration: Debug Win32 ------
1>Compiling...
1>The Adventure.cpp
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(2) : 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(3) : 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 C2873: 'time' : symbol cannot be used in a using-declaration
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(12) : error C3861: 'time': identifier not found
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(12) : 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(12) : error C2365: 'srand' : redefinition; previous definition was 'function'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdlib.h(512) : see declaration of 'srand'
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(14) : error C3861: 'time': identifier not found
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(14) : 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(14) : error C2365: 'srand' : redefinition; previous definition was 'function'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdlib.h(512) : see declaration of 'srand'
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(19) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(21) : error C2374: 'name' : redefinition; multiple initialization
1> c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(16) : see declaration of 'name'
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(28) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
lots of writing...
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(38) : error C2088: '>>' : illegal for class
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(39) : error C2088: '<<' : illegal for class
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(58) : error C2043: illegal break
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(59) : error C2181: illegal else without matching if
1>c:\users\user\documents\visual studio 2008\projects\the adventure\the adventure\the adventure.cpp(62) : error C2181: illegal else without matching if
1>Build log was saved at "file://c:\Users\User\Documents\Visual Studio 2008\Projects\The Adventure\The Adventure\Debug\BuildLog.htm"
1>The Adventure - 15 error(s), 3 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Please help me!
Last edited on
I think the error is on this line srand(time(0));. If you are calling srand and time before using namespace std; you need to write std::srand(std::time(0));. You can't have function calls outside a functions or declarations so move that line to the start of main.

Please use code tags [code] [/code] next time to make it easier to read the code.
Topic archived. No new replies allowed.