C++ adding extra, unwanted characters to a string - c++

I am having a bug that I cannot find a fix for through google searching. I am attempting to make a text based version of the game Mastermind. I am using a string the is set from an array of chars as the criteria for a while loop. When the string is equal to "****" the game is supposed to tell the player that they won and exit, but for some reason a ^A is being added on to the end of the string that is being checked, even though it is not in the char array.
Here is the function that sets the char array and returns a string from that array:
string check(int guess[4], int num[4]) {
char hints[4];
cout << " ";
for (int i = 0; i < 4; i++) {
if (guess[i] == num[i]) {
hints[i] = '*';
cout << "*";
}
else {
for (int x = 0; x < 4; x++) {
if (guess[i] == num[x]) {
cout << "+";
}
}
}
if (guess[i] != num[i]) {
hints[i] = ' ';
}
}
string hint(hints);
cout << endl;
cout << hint << endl;
return hint;
}
And here is the function checking the value of the string:
while (hints.compare("****") != 0) {
if (guessCount == 5) {
break;
}
cout << "Guess?: ";
cin >> guess;
intToArray(guess, guessArr);
hints = check(guessArr, nums);
cout << hints << endl;
guessCount++;
}
if (hints.compare("****") == 0) {
cout << "You win! The number was: ";
for (int i = 0; i < 4; i++) {
cout << nums[i];
}
}

You haven't null-terminated the hints array, so you are getting extra garbage that is lying around on the stack in your string.
You could let the hint string know how long it is when you are constructing it.
string hint(hints, 4);
cout << endl;
cout << hint << endl;

Related

for loop with nested if statement stops iterating after first iteration becomes true. C++

I have a function that receives a character "x" from a user. If the "x" exists in a hard-coded array, "x" is pushed into another array filled with lowdashes in the same position as in the first array. Fo example:
firstArray = ["h", "e", "l", "l","o"]
//user provided character that exist in first array. "e"
lowDashesArray= ["_", "e", "l", "l", "o"]
Then, I want to check if both arrays are equal, my problem is that when the user guesses the firts element in the array (in the above example "h"), the for loop stops iteraring without checking if the other elements are equal. As a result, the block of code that expects both arrays to be equal is executed.
The function looks like so:
bool checkIfLetterExistOnWord(string word, char letter, char* lettersArray, char* lowDashesArray, bool hasWon, bool hasLost, int lives){
bool isCorrectLetter = false;
for(int i = 0; i<word.length(); i++ ){
if(letter == lettersArray[i]){
int position = i;
lowDashesArray[position] = letter;
cout << lowDashesArray;
cout << endl;
isCorrectLetter = true;
}
}
if(isCorrectLetter){
for(int j = 0; j < word.length(); j++){
if(lettersArray[j] == lowDashesArray[j]){
hasWon = true;
//here is the problem. But only when user guesses the first element of the array
}
else{
break;
}
}
if(hasWon){
cout << "You have won";
cout << endl;
cout << hasWon;
cout << endl;
cout << lowDashesArray;
cout << endl;
cout << word;
cout << endl;
return hasWon;
}
else{
cout << "good job. Guess the next letter";
cout << endl;
return hasWon;
}
}
else{
cout << "wrong";
cout << endl;
lives--;
if(lives == 0){
hasLost = true;
cout << "You lost";
return hasLost;
}
else {
cout << "You still have this lives :";
cout << endl;
cout << lives;
cout << endl;
return hasLost;
}
}
}
Both your loop logic is incorrect.
bool isCorrectLetter = false;
for(int i = 0; i<word.length(); i++ ){
if(letter == lettersArray[i]){
int position = i;
lowDashesArray[position] = letter;
cout << lowDashesArray;
cout << endl;
isCorrectLetter = true;
}
}
Here you print the array once for every occurance of the guess character. FUrthermore you do not check, if the letter "was already uncovered". You need to do the printing after the loop and depending on whether you want guessing uncovered chars to be an error, you may need to ajust the check.
for(int j = 0; j < word.length(); j++){
if(lettersArray[j] == lowDashesArray[j]){
hasWon = true;
//here is the problem. But only when user guesses the first element of the array
}
else{
break;
}
}
You're trying to check here, if every char meets a certain criterion (being a non placeholder char). In this case you cannot break early. In general the loop for this kind of check needs to look like this:
bool checkSuccess = true;
for (size_t i = 0; checkSuccess && i != length; ++i)
{
if (!Check(elements[i]))
{
checkSuccess = false;
}
}
Or in your case (using break instead):
hasWon = true;
for(int j = 0; j < word.length(); j++)
{
if(lettersArray[j] != lowDashesArray[j])
{
hasWon = false;
break;
}
}
Imho it's preferrable to separate the replacement logic from io logic. You could e.g. rewrite the logic similar to this:
constexpr char MaskChar = '_';
/**
* Replaces placeholders in \p maskedText, if they the corresponding char in \p clearText
* matches \p guess.
*
* \param[in,out] maskedText the text with some chars replaced with '_'
*
* \return true, if at least one char was replaced, false otherwise
*/
bool GuessChar(std::string const& clearText, std::string& maskedText, char const guess)
{
assert(clearText.length() == maskedText.length());
bool guessCorrect = false;
for (size_t i = 0; i != clearText.length(); ++i)
{
if (maskedText[i] == MaskChar && clearText[i] == guess)
{
guessCorrect = true;
maskedText[i] = guess;
}
}
return guessCorrect;
}
int main()
{
std::string const clearText = "hello";
std::string maskedText(clearText.length(), MaskChar);
size_t lives = 3;
while (lives > 0 && maskedText.find(MaskChar) != std::string::npos)
{
std::cout << "Word: " << maskedText
<< "\nMake your guess!\n";
char c;
std::cin >> c;
if (GuessChar(clearText, maskedText, c))
{
std::cout << "Guess correct!\n";
}
else
{
--lives;
std::cout << "Guess incorrect\n"
<< lives << " lives left\n";
}
}
if (lives > 0)
{
std::cout << "You win!\n";
}
else
{
std::cout << "You loose!\n";
}
}

Char Array Formatting

I am working on a hangman game where incorrect letter guesses are stored in a char array called wrongletters. Of course, the user begins the game with zero wrongletters, so the wrongletters array remains empty upon declaration. The problem I am having is that when I try to display the wrong letters, the letters are spaced very far to the right because of all the other non-value elements in the array
Intended: (Guessed Letters: A B C D)
Current: (Guessed Letters: (Extra Spaces) A B C D)
Any thoughts? (I am aware game does not function properly yet):
void gameSequence() // Runs the hangman game loop
{
// Local and Global Variable Declaration and Initialization
char guessLetter = ' ';
guessWord = strToUpper(getNextWord());
string maskedWord(guessWord.size(), '_');
char wrongLetters[26] = {};
int numWrongLetters = sizeof(wrongLetters) / sizeof(wrongLetters[0]);
// Input, Process, and Output
cout << "\nLet's PLAY\n\n";
for (int i = 0; i < maskedWord.length(); i++)
cout << maskedWord[i] << " ";
while (incorrectCount < 6)
{
drawHangman(incorrectCount);
cout << "<<<<<<<<<< MAKE A GUESS >>>>>>>>>>\n\n";
cout << "Guessed Letters: ";
for (int i = 0; i < 26; i++)
cout << wrongLetters[i] << " ";
cout << "\n\nEnter a letter to guess: ";
cin >> guessLetter;
cout << endl;
guessLetter = toupper(guessLetter);
for (int i = 0; i < maskedWord.length(); i++)
cout << maskedWord[i] << " ";
if (guessWord.find(guessLetter) != string::npos)
{
for (int i = 0; i < maskedWord.length(); i++)
{
if (maskedWord[i] == guessLetter)
maskedWord[i] = guessLetter;
}
}
else
{
incorrectCount++;
wrongLetters[incorrectCount] = guessLetter;
bubbleSort(wrongLetters, numWrongLetters);
}
if (incorrectCount == 6)
{
drawHangman(incorrectCount);
cout << "Sorry you lose - the word was: " << guessWord << endl << endl;
}
}
incorrectCount = 0;
}
As I understand the array wrongletters contain at the beginning it of the wrong letters guesed so far. So there is no point of print all of it and especially sort all of it.
Hence you should change:
for (int i = 0; i < incorrectCount; i++) // incorrectCount replaced 26
cout << wrongLetters[i] << " ";
...
else
{
incorrectCount++;
wrongLetters[incorrectCount] = guessLetter;
bubbleSort(wrongLetters, incorrectCount+1); // incorrectCount replaced numWrongLetters
}
Otherwise when you sort all of the array the spaces go first before the wrong letters.
Because even if your char sequence is empty, you ask in your loop to display a space after the wrongLetters[i]. Replace the ' ' by endl and you will have
A
B
C
D

I keep getting subscript out of range don't know how to fix it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX_NUMS = 200; // Constant for the maximum number of words.
const int MAX_GUESSES = 8;
const string LETTERS = "abcdefghijklmnopqrstuvwxyz";
char inputLetter();
int findChar(char letter, string&word);
string getGuessedWord(string&secretWord, string&lettersGuessed);
string getLettersGuessed(char letter, string&lettersGuessed, int n);
void display(string&lettersGuessed, string&wordGuessed, int num, int pos);
bool isDone(string wordGuessed);
int main()
{
string oneWord; // holds one word from input file
string secretWord; // holds secret word to be guessed
string words[MAX_NUMS]; // holds list of words from input file
int randomValue; // holds index of secret word
int count = 0; // holds number of words in the file
// Declare an ifstream object named myFile and open an input file
ifstream myFile;
myFile.open("P4Words.txt");
// Exit program if cannot open file for input
if (!myFile)
{
cout << "Error: Unable to open file for input" << endl;
return 0;
}
// Input words from a file into words array
// Add your code here ...
myFile >> oneWord;
while (!myFile.eof())
{
words[count] = oneWord;
count++;
myFile >> oneWord;
}
myFile.close();
cout << count << " words loaded." << endl;
srand(static_cast<unsigned int>(time(0)));
// Select a secret word
// Add your code here ...
secretWord = words[rand() % (count + 1) ];
// Possible useful variables the loop
string lettersGuessed = ""; // holds letters guessed so far
string wordGuessed; // holds current word guessed like �_ pp_ e�
int incorrectGuesses = 0; // holds number of incorrect guesses so far
char letter; // holds a guessed letter
bool done = false; // have not guessed the word yet
int num = 8; int pos;
cout << "Welcome to the game, Hangman V1 by Your Name!" << endl;
cout << "I am thinking of a word that is " << secretWord.length()
<< " letters long." << endl;
// Set up a loop to input guesses and process
// Add your code here ...
do
{
letter = inputLetter();
pos = findChar(letter, secretWord);
wordGuessed = letter;
lettersGuessed = getLettersGuessed(letter, lettersGuessed, 8 - num);
wordGuessed = getGuessedWord(secretWord, lettersGuessed);
display(lettersGuessed, wordGuessed, num, pos);
done = isDone(wordGuessed);
num--;
} while ((num > 1) && (done == false));
// Check for won or lost
// Add your code here ...
if (done == false)
{
cout << "sorry you lose..." << endl;
}
if (done == true)
{
cout << "congratulations! " << endl;
}
system("pause"); // stop program from closing, Windows OS only
return 0;
}
// Add function definitions here ...
char inputLetter()
{
int i;
char letter;
do
{
cout << "please guess a letter: " << endl;
cin >> letter;
for (i = 0; i < 25; i++)
{
if (letter == LETTERS[i])
{
return letter;
}
}
if (letter != LETTERS[i])
{
cout << "Oops! That is an invalid character." << endl;
}
} while (letter != LETTERS[i]);
}
int findChar(char letter, string &word)
{
int i = 0; int pos = 0; bool found = false;
do
{
if (word[pos] == letter)
{
return pos;
found = true;
}
pos++;
} while (pos<word.length() - 1);
if (found == false)
{
return -1;
}
}
string getGuessedWord(string&secretWord, string&letterGuessed)
{
string temp;
temp = secretWord;
for (size_t k = 0; k <= temp.length() - 1; k++)
{
temp[k] = '_';
}
for (size_t i = 0; i <= temp.length() - 1; i++)
{
for (size_t j = 0; j <= temp.length() - 1; j++)
{
if (letterGuessed[i] == secretWord[j])
{
temp[j] = letterGuessed[i];
}
}
}
return temp;
}
string getLettersGuessed(char letter, string&lettersGuessed, int n)
{
string temp;
temp = letter;
lettersGuessed.insert(n, temp);
return lettersGuessed;
}
void display(string&lettersGuessed, string&wordGuessed, int num, int pos)
{
if (pos != -1)
{
cout << "You have " << num << " guesses left." << endl;
cout << "Letters guessed so far: " << lettersGuessed << endl;
cout << "Good guess!: " << wordGuessed << endl;
cout << "-----------------------------------------------------" << endl;
}
if (pos == -1)
{
cout << "You have " << num << " guesses left." << endl;
cout << "Letters guessed so far: " << lettersGuessed << endl;
cout << "Oops! that letter is not my word: " << wordGuessed << endl;
cout << "-----------------------------------------------------" << endl;
}
}
bool isDone(string wordGuessed)
{
bool done = false; int k = 0;
for (size_t i = 0; i <= wordGuessed.length() - 1; i++)
{
if (wordGuessed[i] == '_')
{
k++;
}
}
if (k == 0)
{
done = true;
}
return done;
}
it says subscript is out of range I need help to fix it
let me know how to fix it please its a project that is due very soon
that's all I got so far
Change:
for (size_t i = 0; i <= temp.length() - 1; i++)
to:
for (size_t i = 0; i <= letterGuessed.length() - 1; i++)

Random characters popping up

When I debug my program, it outputs a random stream of characters when outputting the asterisked line.
int main ()
{
string inputPuzzle;
cout << "Enter a word or set of words: ";
getline(cin, inputPuzzle);
char* puzzle = new char[inputPuzzle.size()+1];
memcpy(puzzle, inputPuzzle.c_str(), inputPuzzle.size()+1);
puzzle[inputPuzzle.size()+1] = '';
int strikes = 0;
char userInput;
char* userSoln = new char[inputPuzzle.size()];
for (int i = 0; i < inputPuzzle.size(); i++)
{
userSoln[i] = '-';
if (puzzle[i] == ' ')
{
userSoln[i] = ' ';
}
}
bool solved = false;
int numberOfLetters;
for (;;)
{
numberOfLetters = 0;
cin >> userInput;
for (int i = 0; i < inputPuzzle.size(); i++)
{
if (userInput == puzzle[i])
{
numberOfLetters++;
userSoln[i] = puzzle[i];
}
}
if (numberOfLetters == 0)
{
cout << "There are no " << userInput << "'s\n" ;
strikes++;
}
else
{
cout << "There are " << numberOfLetters << " " << userInput << "'s\n";
}
if (userSoln == puzzle)
{
break;
}
if (strikes == 10)
{
break;
}
**cout << "PUZZLE: " << userSoln << "\n";**
cout << "NUMBER OF STRIKES: " << strikes << "\n";
}
if (strikes == 10)
{
cout << "Sorry, but you lost. The puzzle was: " << puzzle;
}
else
{
cout << "Congratulations, you've solved the puzzle!!! YOU WIN!!!!!";
}
}
I've tried clearing cin buffers, but nothing doing. I have all the necessary include files (string and iostream) too, so that isn't the issue, and i have the namespace std above the main method.
This isn't a valid character constant.
puzzle[inputPuzzle.size()+1] = '';
If you intended a terminating character, it should be
puzzle[inputPuzzle.size()+1] = '\0';
or just
puzzle[inputPuzzle.size()+1] = 0;
or you could replace both these lines
memcpy(puzzle, inputPuzzle.c_str(), inputPuzzle.size()+1);
puzzle[inputPuzzle.size()+1] = '';
with strcpy
strcpy(puzzle, inputPuzzle.c_str());
Edit:
You also need to put a terminating character at the end of userSoln before printing it.
userSoln[ inputPuzzle.size() ] = '\0';
puzzle[inputPuzzle.size()+1] = '';
should be
puzzle[inputPuzzle.size()+1] = '\0';
you were trying to add the null terminator to the end of the string to signify the end, but '' is not quite it.

formatting phone number in c++

Please assist, I am trying to format a phone number like (111)111-1111. I have the following code which works but I would like to write much shorter.
int main(){
string phone_number;
cin >> phone_number;
cout<<"(";
for(int i = 0; i < 3; i++) {
cout << phone_number[i];
}
cout << ")";
for(int i = 3; i < 6; i++) {
cout << phone_number[i];
}
cout << "-";
for(int i = 6; i < 10; i++) {
cout << phone_number[i];
}
cout<<endl;
return 0;
}
please assist
Another possibility:
cout << "(" << phone_number.substr(0,3) << ")"
<< phone_number.substr(3,3) << "-" << phone_number.substr(6,4) << endl;
Use string::insert. But since you are taking a string as input, why wouldn't you give the input in format you need. Any how, this is how it can be done with out any loops if you wish to modify the string. In case you don't wish to change the original string then store the modified to a different temporary variable.
string phone_number = "123456789";
phone_number = phone_number.insert( 0, "(" ); // Original string is modified
// Rest can be achieved in similar fashion
cout << phone_number << endl;
for (int i=0; i<10; ++i)
{
if (i == 0) std::cout << '(';
else if (i == 3) std::cout << ')';
else if (i == 6) std::cout << '-';
std::cout << phone_number[i];
}
Probably wouldn't be good to change your data just to format.
string str(phn);
// Format Phone# 1: void function
printf("(%s)%s-%s",
str.substr(0,3).c_str(),
str.substr(3,3).c_str(),
str.substr(6, 4).c_str());
// Format Phone# 2: returns std::string&
stringstream p;
p << "(" << str.substr(0, 3) << ")"
<< str.substr(3, 3) << "-"
<< str.substr(6, 4);
return p.str();
Ignoring the question as to why you want to write this "shorter" ...
printf("(%c%c%c)%c%c%c-%c%c%c%c\n", phone_number[0], phone_number[1], phone_number[2],
phone_number[3], phone_number[4], phone_number[5],
phone_number[6], phone_number[7], phone_number[8],
phone_number[9]);
That being said - your code (nor this) checks to see if there's actually 10 numbers present, or if they're numbers at all. That above all is more important than wanting it "shorter".
Your code is too short, not too long. Any input from the user must always be checked for validity. For instance, what if the used already entered the punctuation, so that phone_number contains "(111)111-1111"? Then your code would output "((11)-)11-1-11" (I think). This is not what you want. At the very least, you must throw out all non-digit characters.
#include <iostream>
using namespace std;
int main(){
string phone_number;
cin >> phone_number;
cout<<"(";
for(int i = 0; i < 3; i++) {
cout << phone_number[i];
}
cout << ")";
for(int i = 3; i < 6; i++) {
cout << phone_number[i];
}
cout << "-";
for(int i = 6; i < 10; i++) {
cout << phone_number[i];
}
cout<<endl;
return 0;
}