Learn The Write Way to Code in C++ | Part 4 Getting data from user and displaying it on screen

1

What will you learn in this lesson..

1) Declaring string variables..
2) String concatenation..
3) Getting input from user and storing in variables.
4) Printing out all of the data to console screen..

If you haven’t already seen our previous tutorials please read them first then continue with this one..

Lesson 1Lesson 2

Lets begin.!!!!

let straight dig in program.


// this statement includes the header files to our program
#include <iostream>
using namespace std;// this statement includes the namespace by the name std
int main (){
cout << "Enter your name "; // informing user to enter data
return 0; // just for terminating the program
}

Output


Now lets break down this chunk of program we will in the first like of this program we included the ios stream header file in our program which will allow us to use certian statments like cout, in the second like of the program we included namespace std.In the third line we started main function and as disuccess in our previsou tutorials cout prints out the string to console screen or in fact in prints our even any thing to screen, then in the last line we returned back the control.

Now let move forward


#include <iostream>
using namespace std;// this statement includes the namespace by the name std
int main (){
string name = " This is second string "; // declaring variable name
cout << "Enter your name "; // informing user to enter data
cout << "First string" << name << "Third string";// concatenation
return 0; // just for terminating the program
}

Output


Now lets break down this again well what we are doing here is we declared a string variable by the name of “name” and we assigned string to it.After that we we concatenated the string and variable to gather and printed them.

Just a bit about concatenation when we are willing to combine two string we do it by writing just the “<<” under one cout statement its similar to.


cout << "First string" << name << "Third string";
to
cout << "First string";
cout << name;
cout << "Third string";

Now that we are done with concatenation.The next thing is getting data from user.


// this statement includes the header files to our program
#include <iostream>
using namespace std;// this statement includes the namespace by the name std
int main (){
string name; // declaring variable name
cout << "Enter your name "; // informing user to enter data cin &gt;&gt; name. // getting data from user and saving it in name cin &gt;&gt; name; // getting data from user
cout << "My name is " << name << " and I am a programmer"; // concatenation
return 0; // just for terminating the program
}

Output



Now here we have just added one statement and that is cin which takes data from user and stores it in variable.Now to clear out the screen you just need to use one system(“cls”) but it will not work untill we add one more statement and is #include .


// this statement includes the header files to our program
#include <iostream>
#include <conio.h>
using namespace std;// this statement includes the namespace by the name std
int main (){
string name; // declaring variable name
cout << "Enter your name "; // informing user to enter data cin &gt;&gt; name. // getting data from user and saving it in name cin &gt;&gt; name; // getting data from user
system("cls");
cout << "My name is " << name << " and I am a programmer"; // concatenation
return 0; // just for terminating the program
}

If you didn’t understand what’s going on don’t worry please watch the video we hope that will help you..

That’s pretty much it for this tutorial if you have any problem or any question please leave a comment down below…

Share.

About Author

1 Comment

  1. Pingback: Learn The Right Way to Code in C++ | Part 4 Getting data from user and displaying it on screen | Coding Tweaks

Leave A Reply