First off I am a beginner and I am teaching myself C++.
I am using a hotel as my bases and implementing all learned pieces as I go.
The code below functions, but there is a little bug still and I cannot wrap my head around it.
If I enter 3 for the "info" count I will be asked 3 times to enter guest data questions. If I enter single words, the code works, but if I enter multiple words with spaces "First Name", the system jumps forward as there are words in the string.
I assume if I enter three words (with spaces) "My First Name :" it will jump to the forth guest entry right away.
Question is: Why does the space within the string cause each word to be stored in its own array position instead of the whole thing in the first position as intended?
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int Guests;
int GuestInfo;
cout << "How many guests can your hotel hold?" << endl;
cin >> Guests;
cout << "How much info do you wish to store for each guest?" << endl;
cin >> GuestInfo;
string signup_questions_arr[GuestInfo]={};
//This is where the signup questions are created!!
for (int x=1; x<=GuestInfo; x++)
{
cout << "Name your guest info Nr. " << x << endl;
cin >> signup_questions_arr[x];
}
for (int x=1; x<=GuestInfo; x++)
{
cout << signup_questions_arr[x] << endl;
}
return 0;
}
This is not conforming C++ code; the size of an array must be a constant known at compile-time.
The GNU (GNU-compatible) compiler will accept this by default, but that does not make it valid C++.