Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,141 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,070 people online right now. Registration is fast and FREE... Join Now!




Switch Statement

 
Reply to this topicStart new topic

Switch Statement

impedimental
post 4 Oct, 2008 - 05:32 PM
Post #1


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 1

I am working on writing a code in C++ to use a switch statement to output a letter grade for class scores. The scores and corresponding letter grades are as follows:

90-100 = A
80-89 = B
70-79 = C
60-69=D
0-59 = F

Our teacher told us to use a control expression of (score/10). I have been trying to write this code and cant get it to work. Heres what I have. (excluding the usual things #include, return 0 etc)

CODE

int score;

cout<< "Enter your score as an integer and press enter: ";
cin >> score;
cout <<endl;

switch (score/10)
{
       case '10':
       case '9':
       cout<< "Great! Your grade is an A.";
       break;
}



The reason I used 10 and 9 is because score was declared as an integer so if your score is 97 and you divide that by 10 you are going to be given a integer. This code doesn't work. I didnt go through the whole thing because I wanted to make sure that it worked. If anyone could give me some help that would be great!!

** Edit ** code.gif
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 4 Oct, 2008 - 05:44 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,324



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


'9' is a character, where 9 is an integer.

cpp

#include <iostream>
using namespace std;

int main(void) {
int score=0;

cout<< "Enter your score as an integer and press enter: ";
cin >> score;
cout <<endl;

switch (score/10) {
case 10:
case 9:
cout<< "Great! Your grade is an A.\n";
break;
case 8:
cout<< "Great! Your grade is an B.\n";
break;
}

return 0;
}


Gives the following output:

QUOTE

c >$./score.exe
Enter your score as an integer and press enter: 92

Great! Your grade is an A.
c >$./score.exe
Enter your score as an integer and press enter: 88

Great! Your grade is an B.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 12:33PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month