Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [Solved] C++ Switch (with boundaries)
New Posts  All Forums:Forum Nav:

[Solved] C++ Switch (with boundaries)

post #1 of 4
Thread Starter 
Erm first of all hello people, i am new at this site smile.gif but i believe that now i will be more active because i started my computer studies course, anw, more or less i know how to use the switch but i was wonder if i could give boundaries to my cases... for example when you get the grade from a test(example :85) and i would like from 81 to 100 the message to say "Your grade is A". I can do this with an If but i want to see if it's possible with a switch, what am i doing wrong? lol. unfortunatly only the default works :S

switch (aver)
{
case (81-100):
cout << "Your grade is \"A\"." << endl;
break;
case (62-80):
cout << "Your grade is \"B\"." << endl;
break;
case (54-61):
cout << "Your grade is \"C\"." << endl;
break;
case (50-53):
cout << "Your grade is \"D\"." << endl;
break;
default:
cout << "Your grade is \"E\".\n";
break;
}
Edited by Tsunamis - 11/13/11 at 4:01pm
post #2 of 4
no, it has to be discrete values...also switches only accept int values in c, c++ fyi...so you cant use chars or strings for future reference.
Edited by surfbumb - 11/13/11 at 3:15pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #3 of 4
You'd have to do something like this:
Code:
if(aver >= 81)
{
cout << "Your grade is \"A\"." << endl;
}
else if(aver >= 62)
{
cout << "Your grade is \"B\"." << endl;
}
else if(aver >= 54)
{
cout << "Your grade is \"C\"." << endl;
}
else if(aver >= 50)
{
cout << "Your grade is \"D\"." << endl;
}
else
{
cout << "Your grade is \"E\".\n";
}
post #4 of 4
Thread Starter 
a.. i see ok thanks mate
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [Solved] C++ Switch (with boundaries)