Reputation: 335
I'm having issues with my code. I am trying to build a game and it somehow gives errors when reaching the main loops. I will show to code and the errors I'm getting.
Just a small note, the point when choosing option 1 and playing the game is that the game loops after the correct answer has been given and presents the player with a second random word, and keeps doing this until the player writes 'quit'.
This is the code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 3;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"jumble1", "First word."},
{"jumble2", "Second word."},
{"jumble3", "Third word."}
};
srand(static_cast<unsigned int>(time(0)));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD];
string theHint = WORDS[choice][HINT];
string jumble = theWord;
int length = jumble.size();
for (int i = 0; i < length; ++i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}
int choice;
bool choiceNotMade = true;
while (choiceNotMade)
{
cout << "[1] Play\n";
cout << "[2] Credits\n";
cout << "[3] Quit\n\n";
cout << "Your choice: ";
cin >> choice;
}
switch (choice)
{
case 1:
cout << "Unscramble the letters to make a word.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "Enter 'quit' to quit the game.\n\n";
cout << "The jumble is: " << jumble;
string guess;
cout << "\n\nYour guess: ";
cin >> guess;
while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
{
cout << theHint;
}
else
{
cout << "That's not the right word.";
}
cout << "\n\nYour guess: ";
cin >> guess;
}
if (guess == theWord)
{
cout << "\nYou guessed it!\n";
}
cout << "\nThank you for playing.\n";
system("Pause");
choiceNotMade = false;
break;
case 2:
cout << "\n\nThis game has been made by:\n\n";
choiceNotMade = false;
break;
case 3:
cout << "Program will exit";
exit(1);
default:
cout << "\nYou did not pick a valid option.\n\n";
choiceNotMade = false;
break;
}
return 0;
}
And this is the error:
word_jumble.cpp: In function `int main()':
word_jumble.cpp:32: error: redeclaration of `int choice'
word_jumble.cpp:17: error: `int choice' previously declared here
word_jumble.cpp:83: error: jump to case label
word_jumble.cpp:53: error: crosses initialization of `std::string guess'
word_jumble.cpp:88: error: jump to case label
word_jumble.cpp:53: error: crosses initialization of `std::string guess'
word_jumble.cpp:92: error: jump to case label
word_jumble.cpp:53: error: crosses initialization of `std::string guess'
word_jumble.cpp:83: warning: destructor needed for `guess'
word_jumble.cpp:83: warning: where case label appears here
word_jumble.cpp:83: warning: (enclose actions of previous case statements requiring destructors in their own scope.)
word_jumble.cpp:88: warning: destructor needed for `guess'
word_jumble.cpp:88: warning: where case label appears here
word_jumble.cpp:92: warning: destructor needed for `guess'
word_jumble.cpp:92: warning: where case label appears here
word_jumble.cpp:100:2: warning: no newline at end of file
make[2]: *** [build/Debug/MinGW-Windows/word_jumble.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
Upvotes: 1
Views: 826
Reputation: 318
You are declaring
int choice;
twice, once
int choice = (rand() % NUM_WORDS);
and then, right before
while (choiceNotMade)
Upvotes: 1
Reputation: 409364
I think the message
redeclaration of `int choice'
should be pretty obvious.
The other error messages are a little harder to understand, but will disappear if you declare the variable guess
outside of the switch
statement.
Upvotes: 1
Reputation: 726839
I assume that this is a homework, so I'll stay away from too specific recommendations.
You declared the variable choice
on line 19, so you need to remove the second declaration at line 34.
You also need to move the declaration of string guess
to before the switch
statement. This is because C++ requires all locals to be initialized exactly once, and there is no way a compiler could make sure of that if the first time through the loop you take case 2:
and the second time around it takes case 1:
where string guess
is declared.
This will make your program compile, but it would not work as expected. Look at the opening/closing braces in your program, and ensure that the code blocks are nested in the way that you expect.
Upvotes: 1
Reputation: 5664
You have declared choice
at two places. Also, you have written the switch
outside the while
loop which means that even if your program compiles, it will be stuck in an infinite loop.
Upvotes: 1
Reputation: 258618
You're declaring int choice
twice. The error message is pretty clear on that.
Once you've declared a variable, you can't re-declare it in the same scope:
{
int x;
//...
int x; // <-- illegal, just use x
}
Upvotes: 2