<< 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

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
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