Grading Program works. I have a few questions.

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

55 Replies - 2374 Views - Last Post: 27 December 2017 - 11:45 AM Rate Topic: -----

#1 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 411
  • Joined: 15-December 14

Grading Program works. I have a few questions.

Posted 12 October 2017 - 07:24 PM

Going through the list of programs suggested by a member and I just finished this one. The program does work but I have a few questions.
1. Is there a better way to have the user input an integer than using a while loop?.
2. I previously made a similar program but the guide lines were to use a switch statement. Can I get some critiques and suggestions on what I can do to improve the program?.

These were the guidelines for the program:
Grading Program
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)

Write a program that allows the user to enter the grade scored in a programming class (0-100).
If the user scored a 100 then notify the user that they got a perfect score.

★ Modify the program so that if the user scored a 90-100 it informs the user that they scored an A

★★ Modify the program so that it will notify the user of their letter grade
0-59 F 60-69 D 70-79 C 80-89 B 90-100 A


#include<iostream>
#include<string>
#include<sstream>
using namespace std;


int main()
{
	int grade;
	const string listy[5]={"A","B","C","D","F"};
	string a =" You got a: ";

	while(true)
	{
		cout<<"Enter your grade as an integer:\n"<<endl;
		if(cin>>grade)
		{
			switch(grade)
			{

			case 90 ... 100:
			{
				ostringstream str0;
				str0 << grade;
				string duh = str0.str();
				duh.append(a);
				cout<<"You keyed in "<<duh<<listy[4]<<endl;
				break;
			}
			case 80 ... 89:
			{
				ostringstream str0;
				str0 << grade;
				string duh = str0.str();
				duh.append(a);
				cout<<"You keyed in "<<duh<<listy[3]<<endl;
				break;
			}
			case 70 ... 79:
			{
				ostringstream str0;
				str0 << grade;
				string duh = str0.str();
				duh.append(a);
				cout<<"You keyed in "<<duh<<listy[2]<<endl;
				break;
			}
			case 60 ... 69:
			{
				ostringstream str0;
				str0 << grade;
				string duh = str0.str();
				duh.append(a);
				cout<<"You keyed in "<<duh<<listy[1]<<endl;
				break;
			}
			default:
			{
				ostringstream str0;
				str0 << grade;
				string duh = str0.str();
				duh.append(a);
				cout<<"You keyed in "<<duh<<listy[0]<<endl;
				break;
			}
			}
		}
		else if(!(cin>>grade))
		{
			cout<<"Thats an invalid choice. Try again.\n\n "<<endl;
			cin.clear();
			cin.ignore(2048,'\n');
		}
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Grading Program works. I have a few questions.

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,735
  • Joined: 25-December 09

Re: Grading Program works. I have a few questions.

Posted 12 October 2017 - 08:03 PM

First you shouldn't be using non-standard "features" when you're trying to learn the language. Here is what my compiler says about your code:

main.cpp||In function ‘int main()’:|
main.cpp|21|error: range expressions in switch statements are non-standard [-Wpedantic]|
main.cpp|30|error: range expressions in switch statements are non-standard [-Wpedantic]|
main.cpp|39|error: range expressions in switch statements are non-standard [-Wpedantic]|
main.cpp|48|error: range expressions in switch statements are non-standard [-Wpedantic]|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|



If you want ranges you should be using if/else if structures instead of a switch statement.

How does your program ever end?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 411
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 12 October 2017 - 08:38 PM

That was actually a problem I forgot to mention in my original statement (The program keeps looping). I'll be completely honest with you, I might be brain tired from working on this program most of the afternoon and evening. But I don't understand what you mean by non standard features. I looked online to see if maybe I was just having a brain fart to see if maybe I could get an idea what you meant.

Originally, I had just the switch statement but wanted to challenge myself a little more (like your suggestion with the pop machine giving change). So I added an array and append to the program. This is the way I originally wrote it. I couldn't figure out how to make it only accept integers and then remembered a previous program I wrote and mixed it with this program.

#include<iostream>
#include<string>
#include<sstream>
using namespace std;


int main()
{

	int grade;
	const string listy [5]={"A","B","C","D","F"};
	string a =" You got a: ";
	cout<<"Enter your grade as an integer:\n"<<endl;
	cin>>grade;
	switch(grade)
	{
	case 0 ... 59:
	{
		ostringstream str0;
		str0 << grade;
		string duh = str0.str();
		duh.append(a);
		cout<<"You keyed in "<<duh<<listy[4];
		break;
	}
	case 60 ... 69:
	{
		ostringstream str1;
		str1 << grade;
		string duh = str1.str();
		duh.append(a);
		cout<<"You keyed in "<<duh<<listy[3];
		break;
	}
	case 70 ... 79:
	{
		ostringstream str1;
		str1 << grade;
		string duh = str1.str();
		duh.append(a);
		cout<<"You keyed in "<<duh<<listy[2];
		break;
	}
	case 80 ... 89:
	{
		ostringstream str1;
		str1 << grade;
		string duh = str1.str();
		duh.append(a);
		cout<<"You keyed in "<<duh<<listy[1];
		break;
	}
	case 90 ... 100:
	{
		ostringstream str1;
		str1 << grade;
		string duh = str1.str();
		duh.append(a);
		cout<<"You keyed in "<<duh<<listy[0];
		break;
	}
	default:
		cout<<"You entered an invalid choice. Try again.\n\n";
		//break;
	}
}



Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 664
  • View blog
  • Posts: 2,740
  • Joined: 13-June 14

Re: Grading Program works. I have a few questions.

Posted 12 October 2017 - 09:45 PM

View Postalbert003, on 13 October 2017 - 04:38 AM, said:

But I don't understand what you mean by non standard features.


Features that aren't defined in the C++ standard, but are compiler specific. These ranges you're using in the switch statement seem to be an example. I've never seen them before, so don't know which compiler(s) support them.
Was This Post Helpful? 0
  • +
  • -

#5 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 411
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 18 October 2017 - 10:43 AM

I was actually playing around with the program and realised I could use case 1 ... 10: I looked online and found tutorials explaining the same thing. Which actually gave me the idea for my original post.

http://www.geeksforg...switch-case-cc/
Was This Post Helpful? 0
  • +
  • -

#6 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 71
  • View blog
  • Posts: 303
  • Joined: 09-September 16

Re: Grading Program works. I have a few questions.

Posted 18 October 2017 - 11:19 AM

GCC 4.9.2 supports it - see http://cpp.sh
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,735
  • Joined: 25-December 09

Re: Grading Program works. I have a few questions.

Posted 18 October 2017 - 01:34 PM

Quote

GCC 4.9.2 supports it

So? Just because one compiler supports a non-standard "feature" doesn't mean you should use that "feature", especially if you're just learning the language. IMO, until you are very familiar with the language you should avoid such implementation hacks.


Jim
Was This Post Helpful? 3
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,513
  • Joined: 08-August 08

Re: Grading Program works. I have a few questions.

Posted 18 October 2017 - 01:59 PM

Problems like this should be thought of as opportunities to learn how to write short, readable code.
	string grades[5] = {"F", "D", "C", "B", "A"};
	for(int i = 0; i < 101; i++) {
		// Taking advantage of integer math.
		int index = i/10 - 5;
		// 100 would result in 5, going past the end of the array
		if(index > 4) index = 4;
		// 0 - 49 will result in negative values.
		if(index < 0) index = 0;
		cout << i << " = " << grades[index] << endl;
	}


Was This Post Helpful? 1
  • +
  • -

#9 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1414
  • View blog
  • Posts: 4,493
  • Joined: 20-March 10

Re: Grading Program works. I have a few questions.

Posted 18 October 2017 - 02:39 PM

Gawd,

that program is an affront to C++ it looks like something you would get in Python....
Was This Post Helpful? 0
  • +
  • -

#10 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 71
  • View blog
  • Posts: 303
  • Joined: 09-September 16

Re: Grading Program works. I have a few questions.

Posted 18 October 2017 - 02:44 PM

Quote

So? Just because one compiler supports a non-standard "feature" doesn't mean you should use that "feature", especially if you're just learning the language.

This was just an answer to ndc85430's question.
I would never use it or recommend it.
Was This Post Helpful? 1
  • +
  • -

#11 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 411
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 19 October 2017 - 09:11 PM

Ok, so not to pick up bad habits, I'll re do my program using the switch and not use the feature I showed here. Thank you guys for your feed back.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,513
  • Joined: 08-August 08

Re: Grading Program works. I have a few questions.

Posted 20 October 2017 - 04:30 AM

The repetitive nature of your switch should tell you that the code can be shortened using a loop or a recursive function.

Edit: I prefer my mathematical method posted above though. Here it is as a function:
string letterGrade(int numericGrade)
{
	const string grades[5] = {"F", "D", "C", "B", "A"};
	// Taking advantage of integer math.
	int index = numericGrade/10 - 5;
	// 100 would result in 5, going past the end of the array
	if(index > 4) index = 4;
	// 0 - 49 will result in negative values.
	if(index < 0) index = 0;

	return grades[index];
}


This post has been edited by CTphpnwb: 20 October 2017 - 01:05 PM

Was This Post Helpful? 1
  • +
  • -

#13 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 411
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 24 October 2017 - 01:08 PM

CTphpnwb I agree and thats why originally I made a grading program (on a different topic here) that used a loop. But the author of the beginner exercise said the following:

Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)

Write a program that allows the user to enter the grade scored in a programming class (0-100).
If the user scored a 100 then notify the user that they got a perfect score.

★ Modify the program so that if the user scored a 90-100 it informs the user that they scored an A

★★ Modify the program so that it will notify the user of their letter grade
0-59 F 60-69 D 70-79 C 80-89 B 90-100 A

under requirements he said I could use if statements, switch statements. I couldn't think of any other way to do it than the way I showed. I'm beginning to wonder if that was put there by mistake.
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,513
  • Joined: 08-August 08

Re: Grading Program works. I have a few questions.

Posted 24 October 2017 - 01:34 PM

My code uses two if statements, which could be written as a switch. Is there a rule against using math too?
Was This Post Helpful? 0
  • +
  • -

#15 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 411
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 24 October 2017 - 07:07 PM

I actually didn't know you could do it that way untill you showed me. I stand corrected and I will build on your example and make a grading program that uses switch statements. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »