C++ part 1 – User input

(UPDATED 4.5.2013)

This is the first part of my C++ tutorial. In the previous three C++ tutorials, I showed some basic theory and introductory material that I will try to update and clean as much as I can, but this is the first part that actually has any meaningful content. Please take look at the previous C++ posts to be to understand what is going on in this post. Someone did note me that It would be a better idea to group the content, and I thought that it would be a good idea as well so I probably will move the C++ to another tab, and have my other projects and courses in their own space.

Anyway, here is the first program that instead of giving predefined values, you can create a program to take input from the user through the console and process this input any way you want.

In the below code, we take users first and last name and print in on the console.

#include <iostream>
using namespace std;
int main()
{

string first_name;
string last_name;
string your_name;

cout << “Enter your first name: “;
cin >> first_name;

cout << “Enter your last name: “;
cin >> last_name;

your_name = first_name + ” ” + last_name;

cout << “Your name is ” << your_name << endl;

system(“PAUSE”);
return 0;
}

Below is picture and link to download the .cpp

first_last

Download the program by clicking here.

————————————————————————–

In the below code, we take two values from the user and just sum it.

#include <iostream>
using namespace std;
int main()
{

int value1;
int value2;

int sum;

cout << “Type the first value(Whole number): “;
cin >> value1;

cout << “Type the second value(Whole number): “;
cin >> value2;

sum = value1 + value2;

cout << “The sum is: ” << sum << endl;

system(“PAUSE”);
return 0;
}

So what happens in the above code is that with the syntax cin we give the program the command to wait input from the user so basically its a command to take input and insert the values in the variables value1 and value2 of data type integer. We then sum these two typed values from the user and sum it with the syntax
sum = value1 + value2;
firstinput

Picture of the process

Click here for the exe or here for the .cpp file.

Basically, the only thing that happened in the above code was the we took two values and summed it and if you still remember the elementary algebra, the code means nothing more than a + b = c or in our code it would be c = a + b where the value that we got from the arithmetic operation is inserted in the left hand side variable.

————————————————————————–

In the next example I will show how to concatenate two strings. In the below code you can see that with the same logic as in the upper code, you can take value from user as strings.

#include <iostream>
using namespace std;
int main()
{

string value1;
string value2;

string concatenation;

cout << “Type the first string: “;
cin >> value1;

cout << “Type the second string: “;
cin >> value2;

concatenation = value1 + ” and “+ value2;

cout << “The two strings you entered were: ” << concatenation << endl;

system(“PAUSE”);
return 0;
}

————————————————————————–

In the below code we do some arithmetic by calculating the square root. The square root program is one of the most famous programs used to introduce students to programming languages be it in Java or C++. It shows how the basic input works, how to process that input in a variable and print in on the console.

#include <iostream>
using namespace std;
int main()
{
int value1;
int squareRoot;
cout << “Type a whole number: “;
cin >> value1;
squareRoot = value1 * value1;

cout << “The square root is: ” << squareRoot << endl;

system(“PAUSE”);
return 0;
}

Here is the .cpp file for the square root program.

————————————————————————–

In the below program we are going to calculate some percentage from some value. The parameters are from the user.

#include <iostream>
using namespace std;
int main()
{
double percentage;
double wholeNumber;
double calculation;
    
cout << “This is a percentage calculator” << endl;
cout << “Please enter the percentage: ” ;
cin >> percentage;
    
cout << “Enter the value you want the percentage to be calculated from: “;
cin >> wholeNumber;
    
calculation = (percentage / 100) * wholeNumber;
    
cout << percentage << “% of ” << wholeNumber << ” = ” << calculation << endl;
    

system(“PAUSE”);
return 0;
}

Here is the .cpp. I will update this part soon, with more content. Again this is for the viewer to have something until I update the section later on. The later update will include the input of data type strings, doubles and other types of input and data processing.

Below is another example. You can copy paste it to your compiler and see what happens.
Just remember to fix the quotation marks because if you copy paste and start the compiler, the compiler will give error message because wordpress quotation marks are in other standard than the one used in C++ compilers.

#include <iostream>
using namespace std;
int main()
{

int value1;
int value2;

int sum;

cout << “Type the first value(Whole number): “;
cin >> value1;

value2 = value1;
sum = value1 + value2;
cout << “You entered: “<<value2 << endl;
cout <<value2 <<” + ” << value1 << ” = ” << sum << endl;

system(“PAUSE”);
return 0;
}

Here is one link if you want more information regarding I/O of C++. You can also Google some theory.

http://www.cplusplus.com/doc/tutorial/basic_io/

To summarize some guidelines that are related to programming in general are that it’s a good programming style to name your variables with meaningful names so you know whats going on and always comment your code so you will understand what is happening in the code later.