2 Replies - 1217 Views - Last Post: 25 March 2008 - 08:50 AM Rate Topic: -----

#1 student_C++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-March 08

switch statements

Posted 25 March 2008 - 08:16 AM

switch (grade)
{
case 'A':
cout << "the grade is A.";
break;
case 'B':
cout << "the grade is B.";
break;
case 'C':
cout << "the grade is C.";
break;
case 'D':
cout << "the grade is D.";
break;
case 'F':
cout << "the grade is F.";
break;
default:
cout <<"the grade is invalid.";
}

for this code, can some1 tell me how i can put the grades to numbers.
example: input grade .75
your letter grade is C

Is This A Good Question/Topic? 0
  • +

Replies To: switch statements

#2 bluewagon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 24-March 08

Re: switch statements

Posted 25 March 2008 - 08:43 AM

View Poststudent_C++, on 25 Mar, 2008 - 08:16 AM, said:

switch (grade)
{
case 'A':
	cout << "the grade is A.";
	 break;
case 'B':
	cout << "the grade is B.";
	 break;
case 'C':
	cout << "the grade is C.";
	 break;
case 'D':
	cout << "the grade is D.";
	 break;
case 'F':
	cout << "the grade is F.";
	 break;
default:
	  cout <<"the grade is invalid.";
}


for this code, can some1 tell me how i can put the grades to numbers.
example: input grade .75
your letter grade is C


I don't think you can do it with a switch statement, first off, switch statements only work with integer values - so you can't use .75 and you can't use relational operators in switch statements either.

you could write a really long switch statement for every single percent value - or you could just write an if else statement -
if(grade > 90)
  cout << "The grade is A\n";
else if(grade > 80)
  cout << "The grade is B\n";


and so on..
Was This Post Helpful? 0
  • +
  • -

#3 student_C++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-March 08

Re: switch statements

Posted 25 March 2008 - 08:50 AM

View Postbluewagon, on 25 Mar, 2008 - 08:43 AM, said:

View Poststudent_C++, on 25 Mar, 2008 - 08:16 AM, said:

switch (grade)
{
case 'A':
	cout << "the grade is A.";
	 break;
case 'B':
	cout << "the grade is B.";
	 break;
case 'C':
	cout << "the grade is C.";
	 break;
case 'D':
	cout << "the grade is D.";
	 break;
case 'F':
	cout << "the grade is F.";
	 break;
default:
	  cout <<"the grade is invalid.";
}


for this code, can some1 tell me how i can put the grades to numbers.
example: input grade .75
your letter grade is C


I don't think you can do it with a switch statement, first off, switch statements only work with integer values - so you can't use .75 and you can't use relational operators in switch statements either.

you could write a really long switch statement for every single percent value - or you could just write an if else statement -
if(grade > 90)
  cout << "The grade is A\n";
else if(grade > 80)
  cout << "The grade is B\n";


and so on..

thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1