using namespace std;

int main()
    char name;
    cout<<"Enter your name.\n";
    cin >> name;
    cout<<"Hello, " << name;

this is what I have..
Very simple program but I cannot get it to work, as soon as I enter two or more letters when debugging, the cmd crashes. I think it is because I am using char instead of another kind of variable, but i dont know what else to use! Please help. Thanks

Edited by Dani: Formatting fixed

7 Years
Discussion Span
Last Post by poopynoob

A char is for a single character meaning if you type in "Hello" it will only save the 'H' to the char variable.

You can make a char array char myarray[size]; This will be able to hold as many characters in it as you have "size".

Or you can include the string header. #include <string> A string is similar to a character array but you do not have to specify the size of it upon declaration. string mystring; I would go check out this website for learning the basics of C++ it helped me out. here.


This simple change might help ye'

char name[80];

This array of chars (also known as a C-String) will provide you with 79 available char-containers in which ye' can populate with ascii characters. Remember, with c-strings to always make sure you have a '\0' NULL terminator somewhere in your array. Also, be sure to become familiar with the <cstring> library of functions that will make your life easier.

Edited by Clinton Portis: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.