<< Chapter < Page Chapter >> Page >

When writing a switch statement, you can use multiple case values to refer to the same set of statements; the default label is optional. For example, consider the following example:

switch(number)

{

case 1:

cout<<“Have a Good Morning\n”;

break;

case 2:

cout<<“Have a Happy Day\n”;

break;

case 3:

case 4:

case 5:

cout<<“Have a Nice Evening\n”;

}

The enum specifier

An enumerated data type is a way of attaching names to numbers, thereby giving

more meaning to anyone reading the code. The enum specifier creates an enumerated data type, which is simply a user-defined list of values that is given its own data type name. Such data types are identified by the reserved word enum followed by an optional user-selected name for the data type and a listing of acceptable values for the data type.

Example:

enum day { mon, tue, wed, thu, fri, sat, sun};

enum color {red, green, yellow};

Any variable declared to be of type color can take only a value of red or green or yellow. Any variable declared to be of type day can take only a value among seven given values.

The statement

enum day a, b,c;

declares the variables a, b, and c to be of type day.

Internally, the acceptable values of each enumerated data type are ordered and assigned sequential integer values beginning with 0. For example, for the values of the user-defined type color, the correspondences created by the C++ compiler are that red is equivalent to 0, green is equivalent to 1, and yellow is equivalent to 2. The equivalent numbers are required when inputting values using cin or displaying values using cout.

Example

#include<iostream.h>

int main()

{

enum color{red, green, yellow};

enum color crayon = red;

cout<<“\nThe color is “<<crayon<<endl;

cout<<“Enter a value: “;

cin>>crayon;

if (crayon == red)

cout<<“The crayon is red.”<<endl;

else if (crayon == green)

cout<<“The crayon is green.”<<endl;

else if (crayon== yellow)

cout<<“The crayon is yellow.”<<endl;

else

cout<<“The color is not defined. \n”<<endl;

return 0;

}

The output of the above program:

The color is 0

Enter a value: 2

The crayon is yellow.

Focus on problem solving

Two major uses of C++’s if statements are to select appropriate processing paths and to prevent undesirable data from being processed at all. In this section, an example of both uses is provided.

Problem: Solving Quadratic Equations

A quadratic equation is an equation that has the form ax2 + bx + c = 0 or that can be algebraically manipulated into this form. In this equation, x is the unknown variable, and a, b and c are known constants. Although the constants b and c can be any numbers, including 0, the value of the constant a cannot be 0 (if a is 0, the equation becomes a linear equation in x). Examples of quadratic equations are:

5x^2 + 6x + 2 = 0

x^2 - 7x + 20 = 0

34x^2 + 16 = 0

In the first equation, a = 5, b = 6, and c = 2; in the second equation, a = 1, b = -7, and c = 20; and in the third equation, a = 34, b = 0 and c = 16.

The real roots of a quadratic equation can be calculated using the quadratic formula as:

delta = b2 – 4ac

root1 = (-b + squared-root(delta))/(2a)

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
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