Lets start,
with Input values, Newline Character, Compiling from Terminal and Homework. Till now, we have used cout to display values on screen. Now, we will learn how to input values from the user at the time of execution of program (also called RUN TIME). We will use an istream object called cin for this purpose. cin stands for "Console Input" and is used to input values from keyboard, the Standard Input Device.
Our next program inputs two variables, adds them and inputs their sum.
NOTE: Rather than naming our variables as a,b,c etc, we should provide meaningful names to the variables. This makes the purpose of the variables much more clear and makes our program easier to read and understand.
//Filename: calc.cpp
#include
int main()
{
int num1,num2,sum;
std::cout<<"Enter the value of num1: ";
std::cin>>num1;
std::cout<<"Enter the value of num2: ";
std::cin>>num2;
sum=num1+num2;
std::cout<<sum;
return 0;
}
Ok. Let us understand what it is.
The first line is pretty straightforward. It has a preprocessor directive that includes the required header file, iostream in our program. In fact, there are a large number of header files for different tasks and you can create your own header files too. They usually have the extension ".h",similar to C header files. For example: stdio.h is a header file that contains functions for standard input/output.
The new statement in main function is:
std::cin>>num1;
Whille executing or interpreting the program, when this statement is encountered, the program waits until you enter a value. Let's say you press 23 on the keyboard and press enter. On doing so, The value 23 is stored in the variable num1.
Then, the next cout statement is executed. After that, the program again waits for an input. Let's assume you entered a value 15. Now this value is stored in the variable num2.
Next, we assign the sum of variables num1 and num2 in the variable sum and display it on screen. (Prints/Displays 38 in this case.)
The funny thing about cout is that it keeps on displaying the text in the same line unless either the line is full (usually 80 characters) or you explicitly tell it to go to the new line. C++ provides some special characters called BACKSLASH CHARACTERS. As the name suggests, they are accompanied by a backslash '/' symbol. One such backslash character is NEWLINE character represented by '/n'.So, if you type the following statement in int main() function:
std::cout<<"Hello \nworld";
The program will display:
Hello
World
Other backslash characters have special meanings. Some of them are: '/a','/b','/f','/t'.
TIP for LINUX USERS: If you are using linux, you can use terminal to compile and execute your programs. Just open the terminal and type in the following command:
codeblocks calc.cpp
This will open a file named calc.cpp inside CodeBlocks. Type your code, save it and exit from Code Blocks. Inside the terminal, type:
g++ calc.cpp
This will compile the program. And the "Object Code" is stored in a file named a.out. After that,type:
./a.out
This will execute your program.
HOMEWORK: Write a program that inputs the values of two integers (int variables) and displays their sum, difference, product and division. Use operators: +,-,*,/. This is your own simple Calculator :)
Feel free to ask questions in the comments section. :)
The next program talks about using namespace, characters and strings.
Comment below for any queries or follow us on facebook.
No comments:
Post a Comment