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);
}