I have a problem while trying to compile my C++ code. As I am still learning C++, I still do not understand most of the advanced commands yet. I was trying to create a program which asks the user's first name, last name, age and gender and displaying it back to the user. This is my source code:
#include <iostream>
int main ()
{
char firstName[20];
char lastName[20];
char age[6];
char gender[3];
int i = 0;
std::cout << "Please enter your full name: ";
std::cin.getline (firstName, 19, ' ');
std::cin.getline (lastName, 19);
std::cout << "Enter your age: ";
std::cin.getline (age, 5);
while (i != 1)
{
std::cout << "Enter your gender (m/f)";
std::cin.getline (gender, 2);
switch (gender)
{
case 'm':
std::cout << "\nHello Mr. ";
i++;
break;
case 'f':
std::cout << "\nHello Mrs. ";
i++;
break;
default:
std::cout << "\nThat is not even a gender!\n";
break;
}
}
std::cout << lastName << "!\n";
std::cout << "You are " << age << " years old.";
return 0;
}
When I tried to compile this, my compiler gives me the following error:
NameAgeQ.cpp: In function 'int main()':
NameAgeQ.cpp:24:15: error: switch quantity not an integer
I've tried to code another programs with the 'switch' statements before and it can handle characters. However, in the previous programs I would declare 'gender' as 'char gender;' instead of 'char gender [];'.
Why in this particular case the 'switch' statement doesn't work? Does it not support the array string?