Docsity

C++ Programming User Interaction and Working with Variables (Lesson 2), Exams of Computer Science

Toggle sidebar
bg1
C++ Programming: User Interaction and Working with
Variables (Lesson๎˜2)
In programming, input, as well as other data, is stored in variables. There
are several different types of variables that store different kinds of
information; when you tell the compiler you are declaring a variable, you
must include the data type, or just type, along with the name of the
variable. The most common basic types available to you are๎˜Ÿchar๎˜Ÿ(stores
single character),๎˜Ÿint๎˜Ÿ(stores integers) and๎˜Ÿdouble๎˜Ÿ(numbers with decimal
places). Here are some examples of declaring variables:
int whole_number;
char letter;
double number_with_decimals;
You use๎˜Ÿcin๎˜Ÿ(pronunced โ€œC inโ€) to accept input and it is followed by an
insertion operator going in the other direction, >>, followed by the variable
into insert the value typed by the user.
Here is a sample program demonstrating using a variable:
#include
using namespace std;
int main()
{
int thisisanuber;
cout<> thisisanuber;
cout << โ€œYou entered โ€ <<thisisanuber <<โ€œ\nโ€;
}
Shortcut operators:
These are shortcut operators for adding any value to a variable:
x +=5; // adds 5 to x
x -=5; // subtract 5 from x
x *=5; // divide x by 5
x++ // add 1 to x;
x-- // subtract 1 from x;
With these operations, you can make a small calculator in C++:
#include
using namespace std;
int main()
{
int first_argument;
int second_argument;
pf2

Partial preview of the text

Download C++ Programming User Interaction and Working with Variables (Lesson 2) and more Exams Computer Science in PDF only on Docsity!

C++ Programming: User Interaction and Working with

Variables (Lesson 2)

In programming, input, as well as other data, is stored in variables. There

are several different types of variables that store different kinds of

information; when you tell the compiler you are declaring a variable, you

must include the data type, or just type, along with the name of the

variable. The most common basic types available to you are char (stores

single character), int (stores integers) and double (numbers with decimal

places). Here are some examples of declaring variables:

int whole_number; char letter; double number_with_decimals;

You use cin (pronunced โ€œC inโ€) to accept input and it is followed by an

insertion operator going in the other direction, >>, followed by the variable

into insert the value typed by the user.

Here is a sample program demonstrating using a variable:

#include

using namespace std;

int main()

int thisisanuber;

cout<> thisisanuber;

cout << โ€œYou entered โ€ <

cout << โ€œEnter first argument: โ€œ; cin >> first_argument;

cout << โ€œEnter second argument: โ€œ; cin >> second_argument;

cout<< first_argument <<โ€œ*โ€ << second_argument << โ€œ=โ€ <<

first_argument*second_argument <<โ€œ\nโ€;

cout<< first_argument <<โ€œ+โ€ << second_argument << โ€œ=โ€ <<

first_argument+second_argument <<โ€œ\nโ€;

cout<< first_argument <<โ€œ/โ€ << second_argument << โ€œ=โ€ <<

first_argument/second_argument <<โ€œ\nโ€;

cout<< first_argument <<โ€œ-โ€ << second_argument << โ€œ=โ€ <<

first_argument-second_argument <<โ€œ\nโ€;

Storing Strings:

A string can hold multiple characters. C++ string class allows you to work

with strings. To use string, you must use the header file. Here is a program

that can read a string from the user using cin.

#include #include using namespace std; int main() { string user_name; cout <> user_name; cout<< "Hi " << user_name <<"\n"; }

If you want to put two strings together, known as appending one string onto

another, you can use the + sign:

#include

include

using namespace std; int main() { string user_first_name; string user_second_name; cout <> user_first_name; cout <> user_second_name; string user_full_name = user_first_name + " " + user_second_name; cout << "Your name is "<< user_full_name << "\n"; }