Dec 31, 2015 at 4:01pm Dec 31, 2015 at 4:01pm UTC
I Try to make a game..and you have to Create Unique name Everytime or game over
but It crash when I start the game
#include <iostream>
using namespace std;
string name[0];
string Input;
int score = 0;
int name_number = 0;
int number = 0;
int Play = 1;
void check_name()
{
for(int i = 0; i < number; i++)
{
if(Input == name[i])
{
Play = 0;
}
}
}
int main()
{
cout << "Welcome to the Fake Name Generator Game" << endl;
cout << "Press <Any> Key To Start" << endl;
cin >> Input;
do
{
cout << "Type A New Name: " << flush;
cin >> Input;
name[name_number] = Input;
number += 1;
name_number += 1;
check_name();
} while(Play == 1);
if(Play == 0)
{
cout << "Game Over" << endl;
}
}
Last edited on Dec 31, 2015 at 4:07pm Dec 31, 2015 at 4:07pm UTC
Dec 31, 2015 at 4:03pm Dec 31, 2015 at 4:03pm UTC
name is a zero-sized array so you can't access any of it's elements because it hasn’t got any.
Dec 31, 2015 at 4:09pm Dec 31, 2015 at 4:09pm UTC
How to check If they Array have the same Elements or not
Dec 31, 2015 at 5:03pm Dec 31, 2015 at 5:03pm UTC
Use a std:;vector instead. vector will allow you to store an unlimited number of names.
http://www.cplusplus.com/search.do?q=std%3A%3Avector
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 26 27 28 29 30 31
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> names;
int Play = 1;
void check_name (const string & input)
{ for (size_t i = 0; i < names.size(); i++)
{ if (input == names[i])
{ Play = 0;
}
}
}
int main()
{ string Input;
cout << "Welcome to the Fake Name Generator Game" << endl;
cout << "Press <Any> Key To Start" << endl;
cin >> Input;
do
{ cout << "Type A New Name: " << flush;
cin >> Input;
names.push_back (Input);
check_name(Input);
} while (Play == 1);
cout << "Game Over" << endl;
system ("pause" );
}
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on Dec 31, 2015 at 5:08pm Dec 31, 2015 at 5:08pm UTC