My First Program

This programs prompts the user to enter their name, grabs their name, then spits it back out again.


int main()

{

string name;

cout << "What's your name? \n";

cin >> name;

cout << "Hello " << name << ". How are you? " << endl;

}

Explanation:

Line 5 the variable name has been declared, with type string. Variables can be many different types for example; an integer number (int), a character (char), a float or decimal number (float), a double or long decimal number (double), a string or sentence (string).

Line 7 has cout which means C out, this prints to the control panel. This is followed by >> an insertion operator. Anything that is output needs to be inspeech marks. The /n should be placed in the speech marks too, it means new line. And finally each line of code should finish with a ; semi-colon

Line 9 has a cin for C in, this reads what the user inputs. This is followed by the << extraction operator and the variable name. This line of code assigns the what the user input to the variable ‘name’.

Line 11 then outputs some more “speech” and the ‘name’ variable. Here I’ve used endl instead of \n for the new line.

Feel free to copy this and try it yourself.