0

I'm trying to make a guessing the word game on C++. However, whenever I try to add a word with a space, it shouldn't be added to the word bank vector. However, it just infinitely loops when I do add a word with let's say "Hello there." The code is incomplete, but I'm confused on why it's infintely looping.

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <cstring>

using namespace std;

void menu(vector<string> arr) {

    int menuInput;
    cout << "-----MENU-----\n";
    while(menuInput != 4){

        cout << "1. PLAY GAME\n";
        cout << "2. ADD A WORD\n";
        cout << "3. OUTPUT ALL AVAILABLE WORDS\n";
        cout << "4. QUIT\n";
        cout << "USER CHOICE:\n";
        cin >> menuInput;

        switch(menuInput) {

            case 1:
                break;
            case 2:
            {
                string addWord;
                cout << "ADD A WORD:\n";
                getline(cin, addWord);
                int n = addWord.length();
                bool finalAdd = true;

                char addWordArray[n];
                strcpy(addWordArray, addWord.c_str());

                int i;
                for(i = 0; i < n; i++) {

                    if(addWordArray[i] == ' ') {
                        finalAdd = false;
                    }
                }
                if(finalAdd == true) {

                    arr.push_back(addWord);
                    cout << "Word added!\n";
                } else {

                    cout << "Word not added, word had a space.\n";
                }
            }
                break;
            case 3:
                break;
            case 4:
                break;
        }
    }
}

int main() {

    vector<string> arr {"APPLE", "BANANA", "ORANGE", "PINEAPPLE"};
    menu(arr);
}
cxlim
  • 15
  • 3

2 Answers2

1

You did not need to make a char array and then copy it over. Also whenever you do a cin >> before you use getline you need to do cin.ignore() in order to flush the newline character. Change the inner part of your switch to this:

            case 2:
            {
                string addWord = "";
                cout << "ADD A WORD:\n";
                cin.ignore();
                getline(cin, addWord);
                int n = addWord.length();
                bool finalAdd = true;

                int i;
                for(i = 0; i < n; i++) {

                    if(addWord[i] == ' ') {
                        finalAdd = false;
                    }
                }

Also consider initilizing to 0 for example:

int menuInput = 0;

Maybe you want to pass the vector by reference in order for the added words to remain after you exit the menu function because currently you are passing it by value. When you pass by value you are only making changes to the object within the function, you are not changing the object that exists outside of it. Passing by value makes a copy of the object. This is an example of how to correct passing by value/making a copy of the vector:

// the & symbol is used to pass by reference
void menu(vector<string>& arr)
bhristov
  • 3,137
  • 2
  • 10
  • 26
  • Thank you so much! I didn't know the purpose of cin.ignore() since I'm coming from java. – cxlim Dec 09 '19 at 07:51
1

cin.ignore(), solves the problem. Put cin.ignore() after the cin >> menuInput;.

references: https://www.geeksforgeeks.org/clearing-the-input-buffer-in-cc/

Kavita Jain
  • 90
  • 1
  • 9