<< Chapter < Page Chapter >> Page >

where the new_type portion is the keyword representing the type to which you want to cast the variable.

Example:

int iNum = 100;

float fNum;

fNum = float (inum);

If you do not explicitly cast a variable of one data type to another data type, then C++ will try to automatically perform the cast for you.

Program input using the cin object

So far, our programs have been limited in the sense that all their data must be defined within the program source code.

We will now learn how to write programs which enable data to be entered via the keyboard, while the program is running.

Such programs can be made to operate upon different data every time they run, making them much more flexible and useful.

Standard input stream

The cin object reads in information from the keyboard via the standard input stream.

The extraction operator (>>) retrieves information from the input stream.

When the statement cin>>num1; is encountered, the computer stops program execution and accepts data from the keyboard. The user responds by typing an integer (or float) and then pressing the Enter key (sometimes called the Return key) to send the number to the computer. When a data item is typed, the cin object stores the integer (or float) into the variable listed after the>>operator.

The cin and cout stream objects facilitate interaction between the user and the computer. Because this interaction resembles a dialogue, it is often called conversational computing or interactive computing.

Example

#include<iostream.h>

int main()

{

int integer1, integer2, sum; // declaration

cout<<"Enter first integer\n"; // prompt

cin>>integer1; // read an integer

cout<<"Enter second integer\n"; // prompt

cin>>integer2; // read an integer

sum = integer1 + integer2;

cout<<"Sum is "<<sum<<endl;

return 0; // indicate that program ended successfully

}

The output of the above program:

Enter the first integer

45

Enter the second integer

72

Sum is 117

Example

#include<iostream.h>

int main()

{

int integer1, integer2, sum; // declaration

cout<<"Enter two integers\n"; // prompt

cin>>integer1>>integer2; // read two integers

sum = integer1 + integer2;

cout<<"Sum is "<<sum<<endl;

return 0;

}

The output of the above program:

Enter two integers

45 72

Sum is 117

Symbolic constants

C++ introduces the concept of a named constant that is just like a variable, except that its value cannot be changed. The qualifier const tells the compiler that a name represents a constant. Any data type, built-in or user-defined, may be defined as const. If you define something as const and then attempt to modify it, the compiler will generate an error.

To define a constant in a program, we use const declaration qualifier.

Example:

const float PI = 3.1416;

const double SALESTAX = 0.05;

const int MAXNUM = 100;

Once declared, a constant can be used in any C++ statement in place of the number it represents.

Example

// this program calculates the circumference of a circle

// given its radius

#include<iostream.h>

int main()

{

const float PI = 3.1416

float radius, circumference;

radius = 2.0;

circumference = 2.0 * PI * radius;

Questions & Answers

I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask