Sign in to follow this  

New to C++ and need some help with assignment for school

This topic is 4019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm brand new to programming and am taking a C++ class but am having some trouble with an assignment. I figured gamedevs were a good source of information and hopefully you guys can help. Anyways, the problem is i need to create a program that allows you to enter a number and it will print out a grade coresponding to that point using one dimensional parallel arrays. I have no idea where i went wrong but it doesn't work and i don't have the ability for it to print out the grade based on a maximum and minimum amount of points. The arrays need to be setup as follows: Min points max points grade 0 299 F 300 349 D 350 399 C 400 449 B 450 500 A Here is the code for what i have comeup with so far: #include <iostream> #include <string> #include <algorithm> using std::cout; using std::cin; using std::endl; using std::string; int main() { //declare variables and arrays int minPoints[5] = {0, 300, 350, 400, 450}; string grade[5] = {"F", "D", "C", "B", "A"}; string searchByPoints = " "; //get the points the student earned cout << "Enter the number of points (X to exit): "; getline(cin, searchByPoints); while (searchByPoints != "X") { //locate position of grade in array int y = 0; //keeps track of array subscripts while (y < 5 && grade[y] != searchByPoints) y = y + 1; //end while //if grade found display it, otherwise display error message if (y < 5) cout << "The grade is: " << grade[y] << endl; else cout << "Points not possible." << endl; //end if //get the points the student earned cout << "Enter the number of points (X to exit): "; getline(cin, searchByPoints); } //end while return 0; } //end of main function Any help would be greatly appreciated, thanks. :D

Share this post


Link to post
Share on other sites
Homework problems such as this aren't allowed on the forum, as in we cant tell you whats wrong with your code.

Your best bet would be to speak to your class tutor about the problem that your having.

Share this post


Link to post
Share on other sites
I believe your program is meant to take points as input. You are taking grades as input and then searching the grades array. You should take points as input and search the minPoints array.

while (y < 5 && grade[y] != searchByPoints)

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by grekster
Homework problems such as this aren't allowed on the forum, as in we cant tell you whats wrong with your code.

Your best bet would be to speak to your class tutor about the problem that your having.


help with homework is fine as long as you aren't giving the answer. He has done a lot of legwork and is looking for help.

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
Quote:
Original post by grekster
Homework problems such as this aren't allowed on the forum, as in we cant tell you whats wrong with your code.

Your best bet would be to speak to your class tutor about the problem that your having.


help with homework is fine as long as you aren't giving the answer. He has done a lot of legwork and is looking for help.

-me


My mistake then:)

Share this post


Link to post
Share on other sites
getline is probably not what you want to use. Read this if you need some more help with input. Also, try to make it a habit to surround your if's and while's with curly brackets. Leaving them out is a sure way to get hard to find bugs when you make some change to your code.

Share this post


Link to post
Share on other sites
It looks like you are searching for a specific grade in your range. Remember that the score that you receive as input might not exactly equal one of the grade range boundaries.

EDIT: I misread your code. The problem is that you are reading in a string, and expecting it to be either an "A", "B", "C", "D", or "F". You should be expecting it to be a number, and you should check to see in which grade range that number falls.

Share this post


Link to post
Share on other sites
if i'm reading your code correctly searchByPoints is going to be a string number (for example "200") whereas the only values contained in grade[] are string letters ("A","B"...etc). so you're problem is that grade[y] will never equal searchByPoints. You probably want to compare searchByPoints to the minPoints[] array to find out where the number lies in the range and then use that index to print the grade. (also beware that minPoints currently contains int's whereas you are getting your input as a string, so be careful how you compare their values "200" != 200).

Hope that helps :)

Share this post


Link to post
Share on other sites
Quote:
Original post by grekster
Quote:
Original post by Palidine
Quote:
Original post by grekster
Homework problems such as this aren't allowed on the forum, as in we cant tell you whats wrong with your code.

Your best bet would be to speak to your class tutor about the problem that your having.

help with homework is fine as long as you aren't giving the answer. He has done a lot of legwork and is looking for help.
-me

My mistake then:)

No, grekster, you are correct and Paladine is wrong. According to the forum FAQ:
Do not ask homework related questions. The Gamedev.net members are not here to answer your homework questions, its against the forum rules and few people take kindly to it.

Share this post


Link to post
Share on other sites
I don't know, I think it's harsh to ban homework - it's not that he's asking us to do it for him - just a little help.

Erze - from looking through your code the comparison logic you are using is wrong. Remember you are looking for a range not an absolute value.

Try breaking up the problem a bit more - decompose it in to functions that do a particular thing.

Often when starting out it helps to write Pseudocode for a problem when you don't understand the problem properly yet - and are a bit unfamilar with the language you are using.

Share this post


Link to post
Share on other sites
Regardless it's still against rules for Homework related questions, I don't see any expections under rule 9, it's pretty clear. Don't take this the wrong way or anything, if you're at a school and you have trouble with a C++ question ask your teachers it's their job to teach so they should have no trouble helping you. If you don't ask for help from your teachers when you have a problem with any question you wont make learning C++ any easier for yourself.


http://www.gamedev.net/community/forums/showfaq.asp?forum_id=31

"Do not ask homework related questions. The Gamedev.net members are not here to answer your homework questions, its against the forum rules and few people take kindly to it. Besides, we shouldn't need to tell you that figuring it out for yourself will be way more beneficial to you in the long run. Still not sure how to get your question across? Then read this http://www.catb.org/~esr/faqs/smart-questions.html."

Share this post


Link to post
Share on other sites
Erze - in future to avoid political issues, phrase future questions without using the words "school" or "homework". Start off with "I'm trying to learn c++ and have an exercise" or similar I guess. I've seen plenty of those.

As far as I'm concerned if it bothered one of the staff or moderators they would have waded in already. I've certainly seen homework threads that deserve to be closed - personally I don't have a problem with this one - seems others do though.

Erze, as someone with a base rating and this being your first post, I'm sure like most people you didn't read the manual / FAQs (which come in handy for telling you tags to format source code by the way) before posting.

What I don't want to see is new blood being chased off the site for asking what seems to me as quite an intelligent question.

Share this post


Link to post
Share on other sites
Not to be judgmental but I have seen this personally and people will sleep in programming class, do absolutely nothing and go around online asking for homework help to pass their course. I'm not saying the original poster is doing this but if you can stretch the rule for him that means to be fair everyone should be allowed to post Homework help. Bending the rules is fine, as long as you bend them for everyone else who comes along.

Share this post


Link to post
Share on other sites

This topic is 4019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this