Hey guys,
I'm totally new to this forum and I'm also totally new to programming.
I've wrote this short code down below and it doesn't really work. Can anyone help me? :)
Oh, and I'm sorry if I spell something wrong! English is not my mother language ;)
You are declaraing a variable named 'name' which can contain a char, i.e. a single character. I think you intended a string which in the C world is an array of char's. If I were you I would use a std::string rather than a char array.
You need to be more specific about what isn't working. If the main problem is the console suddenly closes after the last input, just add cin.get() above return 0;.
If that is the problem, my suggestion really isn't a proper solution for it. Posting your updated codes would help in figuring out what's wrong.
#include<iostream>
usingnamespace std;
constchar MAX_LENGTH = 32;
int main()
{
int age;
cout << "Please insert your age: " << endl;
cin >> age; //leaves a newline in the input stream
cin.ignore(); //so here we ignore the newline
if (age < 18 )
{
cout << "Acces denied! " << endl;
cin.get(); //wait for input
}
else //no need for a new if statement here, only 2 possible outcomes, and it can never be both
{
char name[MAX_LENGTH]; //a non-initialized array of 32 char's
cout << "Please insert your full name: " << endl;
cin.getline(name, MAX_LENGTH); //gets the whole line of your input, does NOT leave a newline in the input stream. Assigns the data to name as a string (terminated by '\0')
cout << "Good morning, " << name << endl;
cin.get(); //wait for input
}
return 0;
}