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

Started by
11 comments, last by Jettoz 17 years, 4 months ago
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
Advertisement
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.
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
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
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
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:)

Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
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.
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.
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 :)
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.

This topic is closed to new replies.

Advertisement