Strings

I need help with a string statement in my code.
I'm trying to make a program like Scrabble Flash(if you don't know what that is, it's a game with scrambled letters and you have to guess the word.)

Well, the code is kind of long, so im not going to show you the whole thing. Besides strings, I get no errors. Here's the part of my code that refers to strings. (there are other parts, so dont think im stupid for trying to run a program out of this) The error im getting is 'kill' undefined (first use his function)

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
32
33
34
35
36
#include<iostream>
#include<string>

using namespace std;

void dictionary () {
/* this is defining all of the scrambled words i will use for the game*/
string kill = "likl";
string text = "xett";
string seas = "esas";
string pint = "ntip";
string sail = "lisa";
string lawn = "wanl";
string shoe = "oehs";
string slam = "mlas";
string base = "asbe";
string rack = "krac";
     }
void Random (int &random) {
/* This is creating a random word each time */   
     if (random == 1) cout << kill << endl;
     if (random == 2) cout << text << endl;
     if (random == 3) cout << seas << endl;
     if (random == 4) cout << pint << endl;
     if (random == 5) cout << sail << endl;
     if (random == 6) cout << lawn << endl;
     if (random == 7) cout << shoe << endl;
     if (random == 8) cout << slam << endl;
     if (random == 9) cout << base << endl;
     if (random == 10) cout << rack << endl;
     }
int main(void) {
    int random = rand() % 11;

/* Then there's a lot more code, some more with string, but if you can help me with this I can figure out the rest. */
}



BTW Im using Dev C++
Last edited on
The compiler is correctly reporting that kill is undefined. Where is kill defined in function Random?!!

void Random (int &random) {
/* This is creating a random word each time */
if (random == 1) cout << kill << endl;
if
All of your strings are declared as local variables in void dictionary();. Which means, when the function is done the variables are gone.

And you haven't seeded rand().

Also, use [ code] and [/code] (without space) around your code snippets.
Thank you, this kind of helped.
But, I am fairly new to c++, and i just have a question..

I know in java, or other coding languages, you can declare them as public integers, strings, etc. Is that possible in C++?
You can just remove the dictionary function (the header and braces) but leave all the string declarations. The strings will then be global and able to be accessed by any function in the file.
It's not a good idea to get used at having global variables. Creating an array of strings and passing it together with an int to Random() would be a better solution. This also lets you reduce the function to simply
1
2
3
4
5
// Doesn't perform any check to ensure a safe access
void Random(int random, string[] words)
{
  cout << words[random] << endl;
}


Note that in your current code, the result of rand() can be 0, leading to Random() not doing anything. You should either write rand() % 10 + 1 or make the condition start from 0. If you use the array then you NEED to have it starting from 0
Last edited on
Let me help you a bit:

1
2
3
void Dictionary() {
   string kill = // .....
}

This "kill"-named string and the others one here will only be available within Dictionary, but not from other functions.
If you want to use them into other functions you can declare them outside:

1
2
string kill = // .....
string another = // ..... 


As someone else (maeriden) pointed out you can, to simplify things, use a table of string:
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
32
33
34
35
36
string Table[] = {
    "likl",
    "xett",
    // add others here, as many as you want as long as your RAM can handle them obviously, LOL.
};
const unsigned int TableSize = sizeof(Table) / sizeof(string);
// TableSize is the Amount of Strings there are in Table[].

// Then into random:
void Random(const unsigned int& ID)
// the 'unsigned' means "You cannot go under 0"
// Also if you only need to "read" ID and not modify it,
// also use the 'const' keyword, or it will make things harder.
{
    if(ID < TableSize) // Just make sure the ID is a good one.
        cout << Table[ID] << endl;
}

int main() // you don't need void, it was only needed in C.
{
    srand((size_t)time(0)); // This is called Seeding the Rand.
    // Now, each time you run the program, you will get a different result,
    // Because you are seeding the program with the current system time.

    int random = rand() % TableSize;
    Random(random); // This is one way to do it

    Random( rand() % TableSize ); // This is another.
    /* Another note is:
    In your example 
    'void Random (int &random)'
    you cannot do the second call ( The one i commented with "this is another".
    If you wanted to do so, you should have used:
    'void Random (const int &random)'
    */
}
Last edited on
@EssGeEich,
I've done some research on what you've said, and it seems like you know what you're talking about. But, the code you gave me only ended up with another problem. In int main(), I start off the game by doing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string name1;
    string name2;
    
    cout << "Player 1, type your name: ";
    cin >> name1;
    cout << "Player 2, type your name: ";
    cin >> name2;
    
    cout << "Welcome, " << name1 << " and " << name2 << "!" << endl;
    cout << name1 << ", you go first." << endl;
    cout << "Ill give you mixed up letters, and you have 5 seconds to figure out what the word is!" << endl;
    cout << "Ready?" << endl;
    sleep(1000);
    cout << "Heres your first mix-match:" << endl;
    sleep(500);
    void Random(const unsigned int& ID);    


And what happens is,
seba
Player 1, type your name: 
and so on and so forth. And when i get to void Random(const unsigned int& ID); it doesnt do anything. Any advice? I know it must sound like i don't know anything about it, but well...i dont...
Because that's not how you call a function.
You call it like it was another function.
Because you're not calling sleep like this:
void sleep(unsigned long);
but you are calling it like:
sleep(value)
So, instead of writing
void Random(const unsigned int& ID);
you must write
Random( rand() % TableSize );

(Look closely, I made you an example in the post over!! Read it, Lol!)

EDIT: Also, a couple of suggestion to use in future:
Use a unscrambled-list of words and mix them up at runtime.
Check the answer case-insensitively.
Last edited on
Topic archived. No new replies allowed.