problem with sradom instance in our game

We have built a randomized area of our game. We used a swtich, but it still is looking for input instead of choosing one on its own
#include <time.h>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bool riddle()
{
srand (time(NULL));
int riddle = rand() %10 +1;

While(riddle != 11)
{
cout<<"To Pass You Must Answer These Riddles Three!!!\n\n";

cin>>riddle;
cin.ignore(11,'\n');

cout<< endl;

swtich(riddle)
{
case 1;
{
if(!rdl1complete)
{
riddle1();
}
break
}
ect...
You're only supposed to call srand() once. There's a lot of typos and bugs in your code.

EDIT: also, cin>>riddle; is always going to ask for user input. That's what it's for.
Last edited on
srand is only called once...

As for bugs in the code I only posted a portion of it other wise it would be very, very long.

The issue is, instead of it making the choice, the game still looks for input from the user

my question is if the switch is causing this error
So riddle() is only called once? What's riddle1()? Why are there two identifiers named riddle for a function and a variable? And this:

case 1;

should be

case 1:
Last edited on
First i apologuise for tiny mistakes i was just typing what we have saved in my partners lap top

we are in class so his laptop isn't connected to the internet.

Bool riddle() is a cpp declared in the Global.h we have.

riddle1(); is referrring to one of ten cpp files attributed to a specific riddle.

the int riddle is one declared specifically for this cpp and not a global like the riddle and riddle1 cpps
As I said, if you don't want user input, don't ask for it:

cin>>riddle;
Thanks it got working, one other question...

Is there a way to remove a specific string from a vector, i have tried pop_back, erase and clear but there have been errors called.
Those should work. pop_back() removes the last element, while erase() takes an iterator and removes the element pointed to by it.

If you're going to remove elements often, it would be better to use a list or a deque.
We use inventory.push_back("metal bar") to insert it into the vector

but when we use inventory.erase("metal bar") i get this error

error C2664 'std::_Vector_iterator<_Ty,_Alloc> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Ty,_Alloc>)' : cannot convert parameter 1 from 'const char [10] to std::_Vector_const_iterator<_Ty,_Alloc>'
"metal bar" is a string, not an iterator. You can use std::find() to get an iterator pointing to the string you want:

1
2
std::vector<std::string>::iterator it = std::find(inventory.begin(), inventory.end(), "metal bar");
if(it != inventory.end()) inventory.erase(it);

Remember to #include <algorithm> .
the iterator is teh number location of it within the vector?
No, that's the index.

You can think of an iterator as a pointer that always points to some element in some container. Incrementing it will make it point to the next element. Every container in the STL has a begin() and an end() member functions, which return iterators to the first element and to one past the last element, respectively. Looping like this:

1
2
3
4
5
6
// foo is a container of type std::some_container

for(std::some_container::iterator it = foo.begin(); it != foo.end(); ++it)
{
    // whatever
}

will cause the loop to iterate over every element in that container. Note that not every container allows accessing elements using an index, so iterators are the only way to iterate over every element in a way that will work with any STL container.
Last edited on
how would that work with this header set up

1
2
3
4
5
6
7
#include "..\..\std_lib_facilities.h"

using namespace std;

#include "Player.h"

#include "Globals.h" 


Player.h and Globals.h are the other two header files used in the game
also i have about 18 or 19 times that end up used so i guess i am hoping there is a more stream lined way of removeing the "item" from the "inventory"
when i tried what you told me to put i got over 100 errors

mostly saying i can't do what your saying for me to
Let's see. Can I see the declaration of "inventory"?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Global.h
 
extern vector<string> inventory;    // to allow ay cpp to access it at any time
//________________________________________________________________________

Cabin.cpp

vector<string> inventory;    //to initiallize

inventory.push_back("Metal Bar");    // to add to the inventory 
//________________________________________________________________________

as well for when i am checking to see if it is there for a result that requires me to have the 
item

bool Check_Inv(string check)
{
     for (int i = 0;  i < inventory.size();i++)
{
string item = inventory [i]
if (item == check)
return true;
}
return false;
}
And what are the error messages you get when you use the two lines I posted up there ( http://www.cplusplus.com/forum/beginner/28378/#msg152713 ) and #include <algorithm> ?

On a side note, "Metal Bar" != "metal bar", and this line:

string item = inventory [i]; // you're missing the semicolon

is useless. You can say:

if(inventory[i] == check)
Last edited on
algorithm is opart of the std_lib_facilities.h that is in my library.h

and i wasn't sure what to type since you used std:: which with the std_lib_facilities and the fact that i have also put using namespace std in the library.h as well

so how would i put that in this case?
Bringing the entire standard library to the global namespace doesn't stop you from fully qualifying identifiers (saying std::).

One thing you should never do is put using namespace std; inside a header file. You also shouldn't use std_lib_facilities.h, which is a header file from Stroustrup's book and, if I remember correctly, uses a macro hack to override the standard library vector. Don't use what you don't need.

If you still need help, post the error messages and I'll have a look.
Topic archived. No new replies allowed.