the basics of c for beginners

In this tutorial we’ll be learning about basic input and output statements, and the types of variables you can use when you make a program. This will make our programs a little bit more “user-interactive”, instead of just displaying text(or prompts) on the screen.

To start off, make sure you have the basic starting components to your program:

#include
using namespace std;
int main (void){

// Code Goes Here (You do not need to put this, it’s optional)

return 0;
}

Withing the two curly braces, delete the comment (It was acting as a placeholder, it’s useless to us now), and type the following:
cout << "Please enter a number." << endl; Your probably wondering what endl is. Well, it just tells the compiler that, after this piece of text (or statement) is executed, I want a brand new line(Basically, going to the next line under the previous line executed). After you've entered the code above proceed to writing: cin >> number;

right underneath the cout (CEE-OUT) statement you’ve just made.

Your code should look exactly like this:

#include
using namespace std;
int main (void){

cout << "Please enter a number" << endl; cin >> number;

return 0;
}

cin, is the statement you use to collect user-input from the user. It’s a great way to keep your program exciting.

Our program is still not complete though. We have yet to tell the compiler what data type “number is”. Well, this is a bit complicated and it would be best if you could commit these terms to memory:

VARIABLE TYPES:

1. Boolean (bool): This keyword is generally used for variables that are equal to true or false in your program.

2. Character (char): This keyword is used for characters. It can only hold one character such as an “A”.

3. Integer (int): This keyword is used for numbers like 5,455, and so on. There are some regulations (which I will explain in the next tutorial), but for now just know the definition and context of when to use it.

These are the basic variable types and I highly suggest that you memorize them.
Go ahead and add, right before your “cout” statement:
int number; After you’ve added this go right under your “cin” statement and put:

cout << "You typed in " << number << "!" << endl; Be sure to write that "cout" statement just like that. Also, whenever you want to display a variable on screen you type: cout << (Whatever your variable name is)<< endl; So now your program should look like this: #include
using namespace std;
int main (void){
int number;

cout << "Please enter a number" << endl; cin >> number;
cout << "You typed in " << number << "!" << endl; return 0; } When you run your program you should get: Please enter a number (It'll wait for your number here...) 40 You typed in 40 (It displays your number here!)

Publisher: sulaiman isse
Filed in: C++

Share this post