I have a small problem. I have attempted to make the game 'Word Jumble' with a scoring system. But sometimes, when the computer guesses a word, then it'll say: The word is: blank here. There should be a jumbled word there. When I try any word, it just subtracts 1.#INF points.
Code:
#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>
using namespace std;
const int size=10;
string Words[size] = {
"consecutive",
"alternative",
"consequently",
"jumbled",
"computer",
"charger",
"food",//I'm hungry
"library",
"strawberry",
"carrier"
};
string Hints[size] = {
"Following continuously.",
"Something available as another opportunity.",
"As a result.",
"This word is rather jumbled, isn't it ;)",
"The enitiy you are reading this off of",
"My phone battery is running low",
"I'm hungry, I need some _",
"Where can I go get a book?",
"It's red, and not a berry."
"Either carries stuff, or is what your data company is called."
};
void main()
{
string word,hint;
double points=0;
bool correct=false,playAgain=true;
cout << "Welcome to Word Jumble!\n";
cout << "The objective of this game is to guess the jumbled word, correctly.\n";
cout << "Say 'quit' to quit, or 'hint' for a hint.\n";
while (playAgain==true)
{
correct = false;
int guesses = 0;
srand(static_cast<unsigned int>(time(0)));
int num = rand() % size + 1;
word = Words[num];
hint = Hints[num];
string jumble = word;
int length = jumble.size();
for (int i = 0; i < length*2; ++i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}
cout << "The word is: " << jumble << endl;
double tempPoints=0;
while (correct==false)
{
string theGuess;
cout << "Guess the word: ";
cin >> theGuess;
guesses++;
while (!cin)
{
cin.sync();
cin.clear();
cout << "Ivalid entry, try again: ";
cin >> theGuess;
}
if (theGuess == word)
{
cout << "Correct! You guessed the word in only " << guesses << " tries!\n";
tempPoints += jumble.size()*1.5;
tempPoints -= (guesses - 1) / 4.0;
points += tempPoints;
cout << "You have been awarded " << tempPoints << " points this round for a total of " << points << "!\n";
correct = true;
cout << "Would you like to play again? (y or n): ";
char tempYN;
cin >> tempYN;
while (!cin || tempYN != 'y' && tempYN != 'n')
{
cin.sync();
cin.clear();
cout << "Invalid entry.\nWould you like to play again? (y or n): ";
cin >> tempYN;
}
if (tempYN == 'y')
{
playAgain = true;
}
else
{
playAgain = false;
}
}
else if (theGuess == "hint")
{
tempPoints -= (1.0 / (jumble.size())) * 40;
cout << "Hint: " << hint << endl;
correct = false;
playAgain = true;
}
else if (theGuess == "quit")
{
correct = true;
playAgain = false;
}
else
{
double sub = (1.0 / (jumble.size())) * 20;
cout << "Incorrect word, deducting "<<sub<<" points\n";
tempPoints -= sub;
playAgain = true;
correct = false;
}
};
};
cout << "Goodbye\n";
}
In the line:
int num = rand() % size + 1;
You are saying to select a random number between 0 and 9 then add 1.
If the random number is 9 the + 1 will make it 10. This means that you are trying to access a value in the array Words and Hints at index 10. Since arrays are 0 indexed and it's size is 10 that means you only have elements at 0 - 9.
You also will never get the first string in the arrays.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I am writing a program to calculate the grade of 3 test scores. The lowest of the first 2 scores is dropped and added to the third test score to make the final grade. The 3 test scores cannot be higer than 50, lower than 0 and cannot be a character or string. So far, I have satisified all those requirment but I need to implement decimal grades to the program like for instance 45.5. Also to round the final grade up or down. For example if final grade is 89.5 round up to an A.
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
char getGrade(int num) {
if (num < 60)
return 'F';
if (num < 69)
return 'D';
if (num < 79)
return 'C';
if (num < 89)
return 'B';
return 'A';
}
bool isnumeric(string temp) {
for (char &chr : temp) {
if ((chr >= '0' and chr <= '9') or chr == '-')
continue;
else
return false;
}
return true;
}
int main(int argc, char const *argv[]) {
cout << "Welcome to the grade calculator.You will input three test "
"scores.\nThe highest of the first two grades and the third grade "
"will be\nadded together to determine the numeric grade average for "
"the\ncourse.Each test score has a maximum of 50 points.\n";
int arr[3];
int ctr = 0;
string temp;
int num;
while (ctr < 3) {
cout << "\nPlease enter test score " << (ctr + 1) << ": ";
label1:
cin >> temp;
if (isnumeric(temp)) {
num = atoi(temp.c_str());
if (num > 50) {
cout << "\nTest scores cannot be higher than 50, try again: ";
goto label1;
} else if (num < 0) {
cout << "\nTest scores cannot be negative, try again: ";
goto label1;
} else {
arr[ctr++] = num;
}
} else {
cout << "\nInvalid test score entered, try again: ";
goto label1;
}
}
int average = 0;
average = max(arr[0], arr[1]);
average = average + arr[2];
cout << "\nThe average for the course = " << average << "\n";
cout << "The letter grade = " << getGrade(average);
cout << "\n\n\nThank you for using this program\n";
return 0;
}
Just changed a couple of things to make it work with decimals:
1. Added chr == '.' to the isNumeric() function:
bool isnumeric(string temp) {
for (char& chr : temp) {
if ((chr >= '0' and chr <= '9') or chr == '-' or chr == '.')
continue;
else return false;
}
return true;
}
2. Changed variable types:
double arr[3]{};
int ctr = 0;
std::string temp;
double num;
3. Removed goto: (You can just use continue)
while (ctr < 3) {
std::cout << "\nPlease enter test score " << (ctr + 1) << ": ";
std::cin >> temp;
if (isnumeric(temp)) {
num = atof(temp.c_str());
if (num > 50) {
std::cout << "\nTest scores cannot be higher than 50, try again: ";
continue;
}
else if (num < 0) {
std::cout << "\nTest scores cannot be negative, try again: ";
continue;
}
else {
arr[ctr++] = num;
}
}
else {
std::cout << "\nInvalid test score entered, try again: ";
continue;
}
}
4. For rounding off, you can use std::round() as such:
double average = 0;
average = std::max(arr[0], arr[1]);
average = std::round(average + arr[2]);
You can also change your cout statements:
std::cout << "\nThe average for the course = " << average;
if (std::round(average) != average) std::cout << ", rounded off to = " << std::round(average);
std::cout << ".\nThe letter grade = " << getGrade(average);
std::cout << "\n\n\nThank you for using this program\n";
Just make all these changes and your program will successfully work with decimals.
Also, consider not using the following in your code:
using namespace std;
..as it's considered as a bad practice. For more info on why, look up to Why is using namespace std considered as a bad practice.
Edit: To accomplish your requirement, you can just change the while loop as such:
while (ctr < 3) {
if (temp.size() == 0)
{
std::cout << "\nPlease enter test score " << (ctr + 1) << ": ";
std::cin >> temp;
}
if (isnumeric(temp)) {
num = atof(temp.c_str());
if (num > 50) {
std::cout << "\nTest scores cannot be higher than 50, try again: ";
std::cin >> temp;
continue;
}
else if (num < 0) {
std::cout << "\nTest scores cannot be negative, try again: ";
std::cin >> temp;
continue;
}
else {
arr[ctr++] = num;
temp.clear();
}
}
else {
std::cout << "\nInvalid test score entered, try again: ";
std::cin >> temp;
continue;
}
}
The above code works as you said.
I am in need of assistance with my program. I have to code a three-round word scramble program using 10 keywords that will appear scrambled to the user for them to guess it. My problem is that after one word the code just simply exits the loop. My intention is for the loop to be used again for a second and third time before exiting.
Here is the code:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
int main() {
while (true) {
enum fields { KEY, HINT, Locked };
const int NUM_WORDS = 10;
const string WORDS[NUM_WORDS][Locked] = {
{"MITCHELL", "NICKNAME IS MITCH,SO WHAT IS MY NAME?"},
{"PIZZA", "MY FAVORITE FOOD, IS ITALY."},
{"ROYCE", "SOME TUMBLE AND SOME ROLLS...?"},
{"INFINITY", "THE IMAGINARY NUMBER IS?"},
{"AMY", "FAVORITE SONIC CHARACTER IS ?"},
{"FIAT", "CRYPTO OVER; THIS TYPE OF CURRENCY?"},
{"RUSSIA", "SAINT PETERSBURG IS IN ?"},
{"DONETELLO", "TEENAGE MUTANT NINJA TURTLES"},
{"SON", "BROOKLYN PEOPLE SAY THIS WORD"},
{"FUN", "FEELING HAPPY"}
};
srand(static_cast<unsigned int>(time(0)));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][KEY]; // GUESSING THE WORDS
string theHint = WORDS[choice][HINT]; // Hint for Words
// Randomizing using the string; swapping charcters equal to the length of the words
string Jumble = theWord; // jumbled version of word
int length = Jumble.size();
for (int i = 0; i < length; ++i) {
int index1 = (rand() % length); // Random
int index2 = (rand() % length); // Random
char temp = Jumble[index1];
Jumble[index1] = Jumble[index2]; // Swapping charcters
Jumble[index2] = temp;
}
cout << "\t\t\Welcome to Word Jumble!\n\n"; // USING CARRIAGE RETURN; USING TITLE
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 << "Sorry, that's not it.";
}
cout << "\n\nYour guess:";
cin >> guess;
}
if (guess == theWord) {
cout << "\nTHat's it! You guessed it!\n";
}
cout << "\nThanks for playing.\n";
return 0;
}
}
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
int main() {
while (true) {
enum fields { KEY, HINT, Locked };
const int NUM_WORDS = 10;
const string WORDS[NUM_WORDS][Locked] = {
{"MITCHELL", "NICKNAME IS MITCH,SO WHAT IS MY NAME?"},
{"PIZZA", "MY FAVORITE FOOD, IS ITALY."},
{"ROYCE", "SOME TUMBLE AND SOME ROLLS...?"},
{"INFINITY", "THE IMAGINARY NUMBER IS?"},
{"AMY", "FAVORITE SONIC CHARACTER IS ?"},
{"FIAT", "CRYPTO OVER; THIS TYPE OF CURRENCY?"},
{"RUSSIA", "SAINT PETERSBURG IS IN ?"},
{"DONETELLO", "TEENAGE MUTANT NINJA TURTLES"},
{"SON", "BROOKLYN PEOPLE SAY THIS WORD"},
{"FUN", "FEELING HAPPY"}
};
srand(static_cast<unsigned int>(time(0)));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][KEY]; // GUESSING THE WORDS
string theHint = WORDS[choice][HINT]; // Hint for Words
// Randomizing using the string; swapping charcters equal to the length of the words
string Jumble = theWord; // jumbled version of word
int length = Jumble.size();
for (int i = 0; i < length; ++i) {
int index1 = (rand() % length); // Random
int index2 = (rand() % length); // Random
char temp = Jumble[index1];
Jumble[index1] = Jumble[index2]; // Swapping charcters
Jumble[index2] = temp;
}
cout << "\t\t\Welcome to Word Jumble!\n\n"; // USING CARRIAGE RETURN; USING TITLE
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 << "Sorry, that's not it.";
}
cout << "\n\nYour guess:";
cin >> guess;
}
if (guess == theWord) {
cout << "\nTHat's it! You guessed it!\n";
}
cout << "\nThanks for playing.\n";
}
return 0;
}
You can use goto statement for what you are wanting to do, but before using it, we will ask the user if he/she want to play it again, we will take input, if he/she says yes(y) then we will restart the code, if no(n) then exit.
The final code:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
int main() {
char res;;
x:
while (true) {
enum fields { KEY, HINT, Locked };
const int NUM_WORDS = 10;
const string WORDS[NUM_WORDS][Locked] = {
{"MITCHELL", "NICKNAME IS MITCH,SO WHAT IS MY NAME?"},
{"PIZZA", "MY FAVORITE FOOD, IS ITALY."},
{"ROYCE", "SOME TUMBLE AND SOME ROLLS...?"},
{"INFINITY", "THE IMAGINARY NUMBER IS?"},
{"AMY", "FAVORITE SONIC CHARACTER IS ?"},
{"FIAT", "CRYPTO OVER; THIS TYPE OF CURRENCY?"},
{"RUSSIA", "SAINT PETERSBURG IS IN ?"},
{"DONETELLO", "TEENAGE MUTANT NINJA TURTLES"},
{"SON", "BROOKLYN PEOPLE SAY THIS WORD"},
{"FUN", "FEELING HAPPY"}
};
srand(static_cast<unsigned int>(time(0)));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][KEY]; // GUESSING THE WORDS
string theHint = WORDS[choice][HINT]; // Hint for Words
// Randomizing using the string; swapping charcters equal to the length of the words
string Jumble = theWord; // jumbled version of word
int length = Jumble.size();
for (int i = 0; i < length; ++i) {
int index1 = (rand() % length); // Random
int index2 = (rand() % length); // Random
char temp = Jumble[index1];
Jumble[index1] = Jumble[index2]; // Swapping charcters
Jumble[index2] = temp;
}
cout << "\t\t\Welcome to Word Jumble!\n\n"; // USING CARRIAGE RETURN; USING TITLE
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 << "Sorry, that's not it.";
}
cout << "\n\nYour guess:";
cin >> guess;
}
if (guess == theWord) {
cout << "\nTHat's it! You guessed it!\n";
}
cout<<"Do you want to play again?(y/n)";
cin>>res;
if (res=='y'||'Y'){
goto x;
}
else{
cout << "\nThanks for playing.\n";
}
return 0;
}
}
My program will repeat output: "You are currently on the 2 floor out of 5
The sum of the codes is: 7 and the product of the codes is: 12
Try again before he catches onto you!"
Based on how many wrong characters are added how can I fix this? I have inserted the cin.clear and cin.ignore but it will repeat the part above.
i.e. if I type wasds it will repeat 5x. Any other notes are also appreciated.
#include <iostream>
#include <ctime>
using namespace std;
int PlayerLevel = 0;
int MaxLevel = 5;
bool GamePlay ()
{
srand(time(NULL));
int PlayerGuessA, PlayerGuessB, PlayerGuessC;
int CodeA = rand() % PlayerLevel + PlayerLevel;
int CodeB = rand() % PlayerLevel + PlayerLevel;
int CodeC = rand() % PlayerLevel + PlayerLevel;
int SumofCodes = CodeA + CodeB + CodeC;
int ProductofCodes = CodeA * CodeB * CodeC;
cout << "You are currently on the " << PlayerLevel << " floor out of 5" << endl;
cout << "The sum of the codes is: " << SumofCodes << " and the product of the codes is: " << ProductofCodes << endl;
cin >> PlayerGuessA >> PlayerGuessB >> PlayerGuessC;
int PlayerProduct = PlayerGuessA * PlayerGuessB * PlayerGuessC;
int PlayerSum = PlayerGuessA + PlayerGuessB + PlayerGuessC;
if (PlayerProduct == ProductofCodes && SumofCodes == PlayerSum) {
cout << "Great Job you got this!!!\n" << endl;
++PlayerLevel;
return true;
}
else
{
cout << "Try again before he catches onto you!\n" << endl;
return false;
}
}
int GameStart()
{
string Introduction = "Welcome to your worst nightmare. You are trapped in a murderer's house. You are on the 5th floor and need to get to the first floor to escape.\n";
string Instructions = "He has each door locked behind a security system that requires a 3 number code to disarm it.\nEnter the codes and move foward. Each level will the code will be harder to figure out.\n";
string PlayerStart;
cout << Introduction << endl;
cout << Instructions << endl;
cout << "Would you like to escape? Yes or No" << endl;
cin >> PlayerStart;
if (!(PlayerStart != "Yes" && PlayerStart != "yes")) {
++PlayerLevel;
}
return 0;
}
int main ()
{
if (PlayerLevel == 0) {
GameStart();
}
while (PlayerLevel <= MaxLevel)
{
bool bLevelComplete = GamePlay();
cin.clear ();
cin.ignore();
}
cout << "You Made it out! Now run before he finds out!" << endl;
return 0;
}
When the type of the input doesn't match the type of the variable that it is being extracted to, cin sets the fail bit. Once this happens, all subsequent reads fail until the stream is reset. The offending characters are still left in the buffer, so that needs to be cleared out as well.
Your usage of cin.clear() and cin.ignore() meant that the fail bit was getting reset, but only one offending character was being removed (cin.ignore() ignores one character by default). This is why you saw the output repeating x times for x erroneous characters.
You could do something like this:
while (PlayerLevel <= MaxLevel)
{
bool bLevelComplete = GamePlay();
if (cin.fail())
{
//Input extraction failed, need to reset stream and clear buffer until newline
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
I made a game where the player types in the scrambled word. Take for example, I have the word 'wall' which is then jumbled up to saying wlal. For a correct answer I multiply the length of the given word to the user by 1000, then report the score at the end.
However, I also have a hint feature set up, so when they type in hint they get a hint. As a penalty, I'd like it so the user get's their score cut in half. Also, if the player answers incorrectly, there is a 1000 point reduction to the score.
My program always sets the score to 0. What's the problem here.
EDITED CODE:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
using namespace std;
int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] = //5x2 array
{
{"wall", "Do you feel you're banging your head against something?"},
{"glasses", "These might help you see the answer."},
{"labored", "Going slowly, is it"},
{"persistent", "Keep at it."},
{"jumble", "It's what the game is all about."}
};
srand(static_cast<unsigned int>(time(0)));
int choice = rand() % NUM_WORDS;
//Choice value in array, than area in array where word and hint are
string theWord = WORDS[choice][WORD]; //word to guess
string theHint = WORDS[choice][HINT]; //hint for word
string jumble = theWord; //jumbled version of word
int length = jumble.size();
//Index1 and index2 are random locations in the string theWord
//last two lines swaps areas, ending the for function with a different
//jumble variable every time.
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;
}
cout << "\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the letters to make a word.\n";
cout << "\n\n\nReady? (y/n)";
//I'm asking here if the player is ready
string isready;
cin >> isready;
if ((isready == "y") || (isready == "Y"))
{
cout << "Ok this is how the scoring works\n";
cout << "The length of the word you will guess is times by 5000.\n";
cout << "If you ask for a hint, your score will go down by half.\n";
cout << "If you get the wrong answer, your score will go down by 1000.";
cout << "\nOk, lets start!\n";
int counter = 3;
for(int i = 0; i < 3; ++i)
{
sleep(1);
cout << counter << "..." << endl;
counter--;
}
sleep(1);
}
else
{
cout << endl;
return 0;
}
cout << "Enter 'quit' to quit the game.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "The jumble is: " << jumble;
//Score system
double score;
double amount_of_guesses, amount_of_wrong = 0;
string guess;
cout << "\n\nYour guess: ";
cin >> guess;
double wrong = 0;
while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
{
cout << theHint;
amount_of_guesses++;
}
else if (guess != theWord)
{
cout << "Sorry, that's not it.";
wrong++;
}
cout << "\n\nYour guess: ";
cin >> guess;
}
if (guess == theWord)
{
score = (theWord.length() * 1000);
}
if (amount_of_guesses != 0)
{
score = score / (amount_of_guesses * 2);
}
if( wrong != 0)
{
score = score - (wrong * 1000);
}
cout << "Your score is: " << score;
cout << "\nThanks for playing.\n";
return 0;
}
Your code works fine for me when double amount_of_guesses, amount_of_wrong = 0; is changed as: double amount_of_guesses=0; double amount_of_wrong = 0;
Additionally, the code score = score / (amount_of_guesses * 2); does not correctly penalize the player by cutting his or her score in half each time they ask for a hint.
You could replace that line with the following code segment to correctly penalize by cutting the score in half every time:
if (amount_of_guesses != 0)
{ while(amount_of_guesses!=0)
{
score = (score / 2);
amount_of_guesses--;
}
}
The code
int amount_of_guesses, amount_of_wrong = 0;
does not initialize amount_of_guesses to 0. It is likely to be a huge number since it will be what is in memory when program is running. If score is lower than amount_of_guesses, result will be 0 using integer division.
I believe the issue is that you never initialize amount_of_guesses. This declaration
int amount_of_guesses, amount_of_wrong = 0;
initializes amount_of_wrong to 0, but amount_of_guesses will just hold some random value that depends on what happens to be in memory. If that's a large value, then this:
if (amount_of_guesses != 0)
{
score = score / (amount_of_guesses * 2);
}
will end up making score == 0 (note that since score is an integer score / (amount_of_guesses * 2) ends up being the floor of that division)
I am encountering a logical error with this app. It is a word jumble app that displays a jumbled word and asks the player if he/she would like to play again once they guess correctly.
When I tell the app I do not want to play again it continues through the sequence anyway. I have a feeling that its bad nesting on my part.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"wall", "Are you banging your head against something?"},
{"jumble", "Its what this game is all about."},
{"glasses", "You might need these to read this text."},
{"labored", "Going slowly, is it?"},
{"persistent", "Keep at it."},
};
srand(static_cast<unsigned int>(time(0)));
cout << "\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the the letters to make the word!\n";
cout << "Enter 'hint' for a hint\n";
cout << "Enter 'quit' to quit the game\n\n";
const int MAX_LEVEL = NUM_WORDS - 1;
int totalScore = 0;
for (int level = 0; level <= MAX_LEVEL; ++level)
{
string theWord = WORDS[level][WORD]; // Word to guess
string theHint = WORDS[level][HINT]; // Word hint
char playAgain;
string jumble = theWord; //Jumbled version of the word
int length = jumble.size();
int score = jumble.size() * 10;
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;
}
cout << jumble << endl;
string guess;
cout << "\nYour Guess: ";
cin >> guess;
while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
{
cout << theHint;
score = score / 2;
}
else
{
cout << "\n\nSorry thats not it.\n\n";
}
cout << "\n\nYour Guess: \n\n";
cin >> guess;
}
if (guess == theWord)
{
cout << "Thats it! You guessed it!\tYou scored: " << score << "\n\n";
cout << "Would you like to play again? (y/n): ";
cin >> playAgain;
if (playAgain = 'y')
{
continue;
}
else if (playAgain = 'n')
{
cout << "Your total score is: " << totalScore << endl;
break;
}
}
else if (guess == "quit")
{
if (totalScore > 0)
{
cout << "Your total score is: " << totalScore << endl;
}
break;
}
}
cout << "\nGoodbye.";
return 0;
}
When comparing playAgain to 'y' and 'n', you only have one equals sign, causing the first one ('y') to always execute instead of it being an actual choice, since the value of 'y' is not 0.
To fix this, they should be:
if (playAgain == 'y') //note ==
{
continue;
}
else if (playAgain == 'n') //note ==
{
cout << "Your total score is: " << totalScore << endl;
break;
}
Also, any sane (more modern) compiler should warn you about this if you have warnings turned on. Be sure to turn those on and take heed of them.
I think you will need == for your playAgain question. I often make mistakes with that.