C++ Programming Code Examples
C++ > If Else and Switch Case Code Examples
Switch statement program in c++ to calculate grade points gpa
/* Switch statement program in c++ to calculate grade points gpa
A simple c++ program which shows using of switch statement in c++
C++ Program which takes input a grade and display Grade Points Average GPA
Program takes inputs A,a, B,b, C,c,D,d,F,f other option will be consider as invalid input */
#include<iostream>
using namespace std;
int main()
{
char grade; double gpa=0.0;
cout<<"Enter your Grade= ";
cin>>grade;
switch(grade)
{
case'A':
case'a':
gpa=4.0;
cout<<"your GPA is "<<gpa;
break;
case'B':
case'b':
gpa=3.0;
cout<<"your GPA is "<<gpa;
break;
case'C':
case'c':
gpa=2.0;
cout<<"your GPA is "<<gpa;
break;
case'D':
case'd':
gpa=1.0;
cout<<"your GPA is "<<gpa;
break;
case'F':
case'f':
gpa=0.0;
cout<<"your GPA is "<<gpa;
break;
default:
cout<<"invalid grade entered";
break;
}
return 0;
}
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
A relational operator is used to check the relationship between two operands. C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc. Relational Operators are also called Comparison Operators.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.
Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and
To generate 'Armstrong numbers' in C++, you have to ask to the user to enter the interval to print the Armstrong number between a given interval as shown here in the following codes.
Similar to friend class, this 'C++ Function' can access the private and protected members of another class. A 'Global Function' can also be 'declared as friend' as shown in the C++ Code
You'll learn to print the number entered by a user using C++ cout statement. This program asks user to enter a number. When enters an 'integer', it is stored in variable number using
Program to find longest path in DAG. Given a weighted directed acyclic graph and a source vertex s in it, find the longest distances from s to all other vertices in the given graph. Create
To sort strings in alphabetical order, you have to ask to the user to enter the two string, now start comparing the strings, and if found then make a t variable of same type, and place the