How to use Conditions in switch case in c++? - c++

Is there any way I can use a switch-case statement based on the answer of condition?
For example: if a percentage is greater than 90 then by using the switch statement, it should print grade A?

You can do something like,
#include<iostream>
#include<string>
int main()
{
int n;
std::cout<<"Enter Mark:";
std::cin>>n;
n=n>90;
switch(n)
{
case 1:
std::cout<<"Grade A";
break;
case 0:
std::cout<<"Grade B";
break;
}
return 0;
}
or if you have more than two
#include<iostream>
#include<string>
// <50 is c , 50 - 90 is b, >90 is a
int main()
{
int n;
std::cout<<"Enter Mark:";
std::cin>>n;
n=(n>50?(n>90?1:2):3);
switch(n)
{
case 1:
std::cout<<"Grade A";
break;
case 2:
std::cout<<"Grade B";
break;
case 3:
std::cout<<"Grade C";
break;
}
return 0;
}

#include <iostream>
using namespace std;
int main()
{
double physics,chemistry,biology,computer,math,totalMarks,obtainMarks,percentage;
char grade;
cout<<"Enter Total Marks=";
cin>>totalMarks;
cout<<"Enter Marks of Physics=";
cin>>physics;
cout<<"Enter Marks of chemistry=";
cin>>chemistry;
cout<<"Enter Marks of biology=";
cin>>biology;
cout<<"Enter Marks of computer=";
cin>>computer;
cout<<"Enter Marks of Mathematics=";
cin>>math;
obtainMarks=physics+chemistry+biology+computer+math;
percentage=(obtainMarks/totalMarks)*100;
grade=percentage>=90?'A':(percentage>=80?'B':(percentage>=70?'C':(percentage>=60?'D':(percentage>=40?'E':'F'))));
switch(grade)
{
case 'A' : cout<<"you have got A grade";
break;
case 'B': cout<<"you have got B grade";
break;
case 'C': cout<<"you have got C grade";
break;
case 'D': cout<<"you have got D grade";
break;
case 'E': cout<<"you have got E grade";
break;
case 'F': cout<<"you have got F grade";
break;
}
return 0;
}
done!

Related

Using while>>cin to keep asking user for input in C++

Code for a blackjack card counting program.
the issue is that it does not exit the while loop upon receiving no cin input from the user.
for example)
User would input x chars and then hit enter to exit the while loop.
#include<iostream>
using namespace std;
int main(){
int count = 0;
char currcard;
cout<<"Enter cards seen on table: "<<endl;
while (cin>>currcard)
{
switch (currcard)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
count++;
break;
case '7':
case '8':
case '9':
break;
case 'A':
case 'J':
case 'Q':
case 'K':
count--;
break;
default:
cout<<"Invalid Entry";
break;
}
}
cout <<"Current count: "<< count << endl;
//user enter cards seen on table and if below 7 increment
//based on count the program returns if you should hit or quit
return 0;
}
Expecting program to exit when enter is hit by user
You can use cin.get() like this.
while (cin>>currcard)
{
// your logic
if (cin.get() == '\n') {
break;
}
}
In this way, your input is supposed to be something like 1 2 3 4 A J Q ending with Enter.
EDIT
As OP wants undetermined length of input, I suggest to switch the input itself from char to std::string.
This way access is gained to more intuitive and effective I\O operations:
#include <iostream> // std::cin, cout, endl
#include <string> // std::string: can omit this line
#include <cctype> // isspace(): can omit
int main(){
int count = 0;
std::string currcard{""};
std::cout << "Enter cards seen on table: "<< std::endl;
std::getline(std::cin, currcard);
for (char c : currcard) {
if (isspace(c))
continue;
switch (c) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
++count;
break;
case '7':
case '8':
case '9':
break;
case 'A':
case 'J':
case 'Q':
case 'K':
--count;
break;
default:
//std::cout << "Invalid Entry\n";
break;
}
}
std::cout <<"Current count: "<< count << std::endl;
//user enter cards seen on table and if below 7 increment
//based on count the program returns if you should hit or quit
return 0;
}
Notice I have added a check for white spaces, and removed the message for invalid entries: both simply get ignored. But if needed that line can be uncommented.
Old solution
You can use cin.getline() as suggested in the comments, in conjunction with a flag that triggers exit from the loop once three inputs are given:
#include <iostream>
int main(){
static int count = 0, flag = 0;
char currcard;
std::cout << "Enter cards seen on table: "<< std::endl;
while(std::cin.getline(&currcard, 3)){
switch (currcard)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
++count;
break;
case '7':
case '8':
case '9':
break;
case 'A':
case 'J':
case 'Q':
case 'K':
--count;
break;
default:
std::cout << "Invalid Entry\n";
--flag;
break;
}
++flag;
if (flag == 3)
break;
}
std::cout <<"Current count: "<< count << std::endl;
//user enter cards seen on table and if below 7 increment
//based on count the program returns if you should hit or quit
return 0;
}
There is also a flag decrement for invalid entries.

C++ Digital Calculator Simulator

I'm currently stuck on having to write a simulator for a real, old calculator (like those one dollar calculators, the ones with the buttons, in which you must enter your numbers one push-of-a-button at a time, can't write the Whole number directly and for now it is only in integers). I know for sure that there has to be a switch case for the 0-9 and one for the operators (including '='). Should break the cycle and print the result of the calculation, but I can use as many operators as I want in the each "session" (ex: 123+7-89/6*8+3). I am finding trouble in connecting the two loops and in printing the result. Thank you so much in advance.
#include <iostream>
using namespace std;
int execute(int n1, int n2, char c);
int main()
{
int acc, num=0;
char c, op;
cin>>c;
while (1) {
switch (c) {
case '0': cout << "0";
case '1': cout << "1";
case '2': cout << "2";
case '3': cout << "3";
case '4': cout << "4";
case '5': cout << "5";
case '6': cout << "6";
case '7': cout << "7";
case '8': cout << "8";
case '9': cout << "9";
num=num*10+static_cast<int>(c-'0');
acc=num;
op=c;
acc= execute(acc, num, op);
}
}
return 0;
}
int execute(int n1, int n2, char c) {
do {
switch (c) {
case '+': return (n1+n2);
case '-': return (n1-n2);
case '*': return (n1*n2);
case '/': return (n1/n2);
case '=': return true;
}
} while (!'=')
}

The case at line 42 keeps falling through to the case at line 49

Answer the first 3 questions with n-y-n. That last statement should have stopped but instead goes to the next question instead.
Please ignore the rest if you want. If there are any mistakes, please tell me.
I'm currently taking a programming class for the first time so be gentle with me
#include <iostream>
using namespace std;
int main()
{
//Tattoo Decision
char ans;
cout <<"This program will help you determine whether you should get a tattoo or not?";
cout <<"\nPlease answer the questions with either y (yes) or n (no)";
cout <<"\n\n\nAre you Drunk?: ";
cin >>ans;
switch (ans) //finished
{
case 'y':
{
cout <<"\n\nWell, for obvious reasons, Don't get a tattoo'";
break;
}
case 'n':
{
cout <<"\n\nAre your friends egging on you?: ";
cin>>ans;
switch(ans)//finished
{
case 'y':
{
cout <<"\n\nAre they laughing?: ";
cin >>ans;
switch(ans)//finished
{
case 'y':
{
cout<<"\n\nDon't get a tattoo";
break;
}
case'n':
{
cout<<"\n\nLaughing or not, Don't get a tattoo";
break;
}
}
}
case'n':
{
cout<<"\n\nDoes the tattoo have a special meaning to you?: ";
cin>>ans;
switch(ans)//finished
{
case'y':
{
cout<<"\n\nIs it a name?: ";
cin>>ans;
switch(ans)//unfinished
{
case'y':
{
cout<<"\n\nIs it your significant other?: ";
cin>>ans;
switch(ans)//unfinished
{
case'y':
{
cout<<"\n\nDon't fucking get that tattoo'";
break;
}
}
}
}
}
case'n':
{
cout<<"\n\nThen why are you getting a tattoo for? Don't get one";
break;
}
}
}
}
}
}
return 0;
}
Looks like you are missing a break, try adding as below:
int main()
{
//Tattoo Decision
char ans;
cout <<"This program will help you determine whether you should get a tattoo or not?";
cout <<"\nPlease answer the questions with either y (yes) or n (no)";
cout <<"\n\n\nAre you Drunk?: ";
cin >>ans;
switch (ans) //finished
{
case 'y':
{
cout <<"\n\nWell, for obvious reasons, Don't get a tattoo'";
break;
}
case 'n':
{
cout <<"\n\nAre your friends egging on you?: ";
cin>>ans;
switch(ans)//finished
{
case 'y':
{
cout <<"\n\nAre they laughing?: ";
cin >>ans;
switch(ans)//finished
{
case 'y':
{
cout<<"\n\nDon't get a tattoo";
break;
}
case'n':
{
cout<<"\n\nLaughing or not, Don't get a tattoo";
break;
}
}
break; // <---------- *****
}
case'n':
{
cout<<"\n\nDoes the tattoo have a special meaning to you?: ";
cin>>ans;
switch(ans)//finished
{
case'y':
{
cout<<"\n\nIs it a name?: ";
cin>>ans;
switch(ans)//unfinished
{
case'y':
{
cout<<"\n\nIs it your significant other?: ";
cin>>ans;
switch(ans)//unfinished
{
case'y':
{
cout<<"\n\nDon't get that tattoo'";
break;
}
}
}
}
break; //<--------- *****
}
case'n':
{
cout<<"\n\nThen why are you getting a tattoo for? Don't get one";
break;
}
}
}
}
}
}
return 0;
}

How to use switch statements if you have four inputs

A class has four exams in one term. Input each exam score and assign the corresponding grades.
90 and above is A
80 to 89 is B
70 to 79 is C
60 to 69 is D
below 60 is F
below 0 is invalid
I can only code using 1 input not 4
#include<iostream>
using namespace std;
int main(){
int a;
cout<<"Input Examination Score: ";
cin>>a;
switch (a / 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: cout <<"Equivalent is F"<<endl;
break;
case 6: cout <<"Equivalent is D"<<endl;
break;
case 7: cout <<"Equivalent is C"<<endl;
break;
case 8: cout <<"Equivalent is B"<<endl;
break;
case 9:
case 10: cout <<"Equivalent is A"<<endl;
break;
default: cout <<"Invalid Score"<<endl;
break;
}
You can do this in different ways. You can either use a for-loop or a while/do-while loop.
for(int i = 0; i < 4; i++) // runs 4 times
{
//Do your thing
}
Or you can create a variable with the value of 4, use a while/do-while loop
and decrease it by 1 each semester until it is 0.
Use a loop to read the input multiple times, then calculate the average when printing the result:
#include<iostream>
using namespace std;
int main(){
int number_grades = 4;
int sum = 0;
for (int i=0; i<number_grades; ++i) {
int a;
cout<<"Input Examination Score: ";
cin>>a;
sum +=a;
}
switch ((sum/number_grades) / 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: cout <<"Equivalent is F"<<endl;
break;
case 6: cout <<"Equivalent is D"<<endl;
break;
case 7: cout <<"Equivalent is C"<<endl;
break;
case 8: cout <<"Equivalent is B"<<endl;
break;
case 9:
case 10: cout <<"Equivalent is A"<<endl;
break;
default: cout <<"Invalid Score"<<endl;
break;
}
return 0;
}

Getting a program to start over and get a new value?

I am working with a program which requires a value to put in a variable and do some stuff on it. The problem is that I want the program to start over again and ask the user for a new value to process the new value again.
For example look at this code, which requires a number as a grade to rate it. When the processing was done I want the program to ask for a new grade ( next student for instance ).
#include <iostream.h>
int main (int argc, const char * argv[])
{
int n;
cout<< " Please Enter your grade : " ;
cin>>n;
switch (n/10) {
case 10: cout<< " A+ : Great! ";
case 9:
break;
case 8: cout<< " A : Very Good ";
break;
case 7: cout<< " B : Good " ;
break;
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout<< " Failed ";
break;
default:
break;
}
return 0;
}
What you need is a while loop
int main (int argc, const char * argv[])
{
int n;
while(1) {
cout<< " Please Enter your grade : " ;
cin>>n;
switch (n/10) {
case 10: cout<< " A+ : Great! ";
case 9:
case 8: cout<< " A : Very Good ";
break;
case 7: cout<< " B : Good " ;
break;
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout<< " Failed ";
break;
default:
break;
}
cout<<"do you wish to continue?(y/n)";
cin>>some_declared_variable;
if (some_declared_variable == 'n')
break; //hopefully this will break the infinite loop
}
return 0;
}