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;
}
Related
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.
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!
I'm new to c++ and I have to write a program that takes a user 4-digit number and convert it to words i.e. 7238 would be wrote as seven two three eight. Yet it writes every number as unknown. Any advice for a noob would be greatly appreciated.
#include iostream
using namespace std;
int main() {
char number;
cout << "Please enter a 4 digit number: ";
cin >> number;
switch(number){
case 1 :
cout<< "one";
break;
case 2 :
cout<< "two";
break;
case 3 :
cout<< "three";
break;
case 4 :
cout<< "four";
break;
case 5 :
cout<< "five";
break;
case 6 :
cout<< "six";
break;
case 7 :
cout<< "seven";
break;
case 8 :
cout<< "eight";
break;
case 9 :
cout<< "nine";
break;
case 0 :
cout<< "zero";
break;
default :
cout << "UNKNOWN.";
}
}
Sounds like homework but here are some tips. Change your number variable to type of int You can break the number out into individual variables with division and modulus. I would stuff those into an integer array.
int array[4];
arr[0] = (number / 1000) % 10; // Thousands
....... // You do the hundreds and tens
arr[3] = (number % 10); // Ones
Then use a loop around your switch statement where your counter is less than 4 (the length of the array). Make sure to increase your counter at the end of each loop. Oh, and it's #include <iostream>.
With to_string and range based for:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number;
cout << "Enter the number: ";
cin >> number;
string strnum = to_string(number);
for (auto c : strnum)
{
switch (c)
{
case '0': cout << "zero "; break;
case '1': cout << "one "; break;
case '2': cout << "two "; break;
case '3': cout << "three "; break;
case '4': cout << "four "; break;
case '5': cout << "five "; break;
case '6': cout << "six "; break;
case '7': cout << "seven "; break;
case '8': cout << "eight "; break;
case '9': cout << "nine "; break;
default: cout << "non-digit"; break;
}
}
return 0;
}
You need to put ascii values in your case statements. Currently you are comparing the ascii values for digits with numbers 0 - 9.
Values can be found here : http://www.asciitable.com/
Your variable is of type char. A char stores a character, usually ASCII encoded. If the user inputs a '1', for example, that would usually translate to an integer value of 49, not 1. Either read into an int or change your case labels to use character literals:
case '1':
cout << "one";
break;
You could then use a loop to read multiple digits.
In my opinion I feel like many people are confused about this question. What I want to do is ask the user for a number and if it is between 0 - 17, the output I would want it to be is:
Too Young
And if it is 18 - 42, the output should be:
Adult
And if it's 43 and over to be:
Senior
All while using the switch statement
Here is the code I used:
#include <iostream>
using namespace std;
int main()
{
int age;
cin >> age;
if (age <= 16) {
cout <<"Too young";
}
if (age <= 42) {
cout << "Adult";
}
if (age <= 70) {
cout << "Senior";
}
return 0;
}
The output of my code is:
Too YoungAdultSenior
Please help me out.
Replace your code with:
#include <iostream>
using namespace std;
int main()
{
int age;
cin >> age;
if (age <= 17) {
cout <<"Too young";
} else if (age <= 42) {
cout << "Adult";
} else {
cout << "Senior";
}
return 0;
}
To check if something is 2 conditions at the same time, use the && operator, which means and and checks for two conditions being true either side. Example for age between 44 and 56:
#include <iostream>
int main()
{
int age=55;
if ((age>=44) && (age <= 56))
{
std::cout << "YAY!!!\n";
}
return 0;
}
In C++11 - instead of &&, you can use and keyword.
In the above answer, if you wish to use using namespace std;, simply use this piece of code:
#include <iostream>
using namespace std;
int main()
{
int age;
cin >> age;
if ((age>=44) && (age <= 56))
{
cout << "YAY!!!\n";
}
return 0;
}
This is how you would do it with a switch statement btw:
#include <iostream>
using namespace std;
int main(){
int age;
cin >> age;
switch (age) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
cout << "Too young\n";
break;
case 18: case 19: case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39: case 40:
cout << "Adult\n";
break;
default:
cout << "Senior\n";
}
return 0;
}
The perfect way:-
#include <iostream>
using namespace std;
int main()
{
int age;
cin >> age;
if (age < 18 && age >=0) {
cout <<"Too young";
} else if (age >= 18 && age <= 42) {
cout << "Adult";
} else if(age > 42)
cout << "Senior";
}
return 0;
}
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;
}