By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

2D Array-FirstTime Why do I hve to have user input less one

P: 47
I'm in wk 12 of my first programming course, assignment is to create program using 2dimen array that displays grade, why does my code work for everything unless the user enters the final element number 500? I needed to include in my input cout << "Enter points (less one) :" << endl; in order for it to work with any scenario. No matter what I change if the user enters 500 the program fails, can anyone point me to where my mistake is? Thank you, it's due Tuesday and I like my code to be perfect, the user shouldn't have to adjust for my coding errors. Any help would be welcome. I'm still going crazy over using functions and pointers with their non value returning void, and the ampersand pointer that I have to rewrite code over and again to get right. I've spent about 12 hours rewriting this thing so far and it's driving me nuts!!! Here's my code:
Expand|Select|Wrap|Line Numbers
  1. //Ch11Ex11
  2. //Joseph L Matzke
  3.  
  4. #include <string>
  5. #include <iostream>
  6.  
  7.  
  8.    using namespace std;
  9.  
  10. int main()
  11. {
  12.     int points = 0;
  13.     int place = 0;
  14.     int grPoints[6] = {0, 300, 350, 400, 450, 500};
  15.     string grade[5] = {"F", "D", "C", "B", "A"};
  16.  
  17.     cout << "GRADING PROGRAM FOR SCORES A-F ON A SCALE OF 500" << endl << endl << endl;
  18.  
  19.     for (;;)
  20.     {
  21.         cout << "Enter points (less one from end) : ";
  22.         cin >> points;
  23.  
  24.         if(points == -1) break;
  25.         if(points > 500 || points < -1)
  26.         {
  27.             cout << "Incorrect Entry, Out of Score Paramaters 0/500" << endl << endl;
  28.             continue;
  29.         }
  30.  
  31.         for (place = 0; place < 5; place = place + 1)
  32.         {
  33.             if (points >= grPoints[place] && points < grPoints[place+1]) break;
  34.         }
  35.         cout << "Grade =  " << grade[place] << endl << endl;
  36.     }
  37.     system("pause");
  38.     return 0;
  39. }
Jun 3 '07 #1
Share this Question
Share on Google+
6 Replies


ilikepython
Expert 100+
P: 844
I'm in wk 12 of my first programming course, assignment is to create program using 2dimen array that displays grade, why does my code work for everything unless the user enters the final element number 500? I needed to include in my input cout << "Enter points (less one) :" << endl; in order for it to work with any scenario. No matter what I change if the user enters 500 the program fails, can anyone point me to where my mistake is? Thank you, it's due Tuesday and I like my code to be perfect, the user shouldn't have to adjust for my coding errors. Any help would be welcome. I'm still going crazy over using functions and pointers with their non value returning void, and the ampersand pointer that I have to rewrite code over and again to get right. I've spent about 12 hours rewriting this thing so far and it's driving me nuts!!! Here's my code:
Expand|Select|Wrap|Line Numbers
  1. //Ch11Ex11
  2. //Joseph L Matzke
  3.  
  4. #include <string>
  5. #include <iostream>
  6.  
  7.  
  8.    using namespace std;
  9.  
  10. int main()
  11. {
  12.     int points = 0;
  13.     int place = 0;
  14.     int grPoints[6] = {0, 300, 350, 400, 450, 500};
  15.     string grade[5] = {"F", "D", "C", "B", "A"};
  16.  
  17.     cout << "GRADING PROGRAM FOR SCORES A-F ON A SCALE OF 500" << endl << endl << endl;
  18.  
  19.     for (;;)
  20.     {
  21.         cout << "Enter points (less one from end) : ";
  22.         cin >> points;
  23.  
  24.         if(points == -1) break;
  25.         if(points > 500 || points < -1)
  26.         {
  27.             cout << "Incorrect Entry, Out of Score Paramaters 0/500" << endl << endl;
  28.             continue;
  29.         }
  30.  
  31.         for (place = 0; place < 5; place = place + 1)
  32.         {
  33.             if (points >= grPoints[place] && points < grPoints[place+1]) break;
  34.         }
  35.         cout << "Grade =  " << grade[place] << endl << endl;
  36.     }
  37.     system("pause");
  38.     return 0;
  39. }
You are getting a segmentation fault. If points = 500, place will by equal to 5. Then you are trying to print grade[5] (the 6th element), grade only has 5 elements. Why is your grPoints array longer than your grade array?

Also, you aren't using a 2d array. Is that part of the assignment?
Jun 3 '07 #2

Savage
Expert 100+
P: 1,764
Because of this:

if (points >= grPoints[place] && points < grPoints[place+1]) break;

this expreeson doesn't evaluate to true.

savage
Jun 3 '07 #3

AdrianH
Expert 100+
P: 1,251
The problem is on line 33.

I'm not sure what your teacher teaches, and your programme will work, but my personal opinion is that one shouldn't use break and continue unless there is a very good reason or you will get into some bad habbits.

There should be IMHO, only one way out of a loop and that is through the exit condition, and continues are unnecessary by virtue of if/else if/else blocks. Yes, it requires more typing, but you can see the flow going from top to bottom easier and it is also easier to prove using loop invariant analysis.

That's my two cents.


Adrian
Jun 3 '07 #4

P: 47
No my mistake, two dimensional is next chapter it is supposed to be two parellel arrays, I dont even know what two dimensional is, yet. I see what you mean about my boolean statement, it can never be true!
Oh boy, back to the drawing board, and I will use
if(condition;condition;condtion++)
else cout's with indentation next time, well this time when I correct it and it doesn;t work again. Ha- I don't know what you mean use C/C++ <tags>? was it?
I see the guidelines box next to this text--use
Expand|Select|Wrap|Line Numbers
  1. ..code goes here..
Not sure I know what that means but I'll try.

And, thank you. It wasn't our professor that taught me breaks and continue, it was a tutor at school. Joe
Jun 4 '07 #5

Savage
Expert 100+
P: 1,764
No my mistake, two dimensional is next chapter it is supposed to be two parellel arrays, I dont even know what two dimensional is, yet. I see what you mean about my boolean statement, it can never be true!
Oh boy, back to the drawing board, and I will use
if(condition;condition;condtion++)
else cout's with indentation next time, well this time when I correct it and it doesn;t work again. Ha- I don't know what you mean use C/C++ <tags>? was it?
I see the guidelines box next to this text--use
Expand|Select|Wrap|Line Numbers
  1. ..code goes here..
Not sure I know what that means but I'll try.

And, thank you. It wasn't our professor that taught me breaks and continue, it was a tutor at school. Joe
if(condition;condition;condtion++)

???

What is this?

And yes those are the tags,if u want to display c language syntax highlithing u type CODE=c
Expand|Select|Wrap|Line Numbers
  1.  code goes here 
Savage
Jun 4 '07 #6

AdrianH
Expert 100+
P: 1,251
No my mistake, two dimensional is next chapter it is supposed to be two parellel arrays, I dont even know what two dimensional is, yet. I see what you mean about my boolean statement, it can never be true!
Oh boy, back to the drawing board,
Not necessarly back to the drawing board, just change your if statement on line 33 to less than or equal instead of less than (and corrispondingly the other one in that if as well, which may cause you to change other things), or just push 500 up to 501 (which may not be optimal either). How you fix it is up to you however.

My comments on not using break and continue are styalistic. I don't approve of them, but they do not invalidate that they do not work. Depending on your time constraints you may not wish to risk chaning your code drasticly.

and I will use
if(condition;condition;condtion++)
else cout's with indentation next time
You are refering to for(initialise; condition; endCmd(s)) { ... }, right? ;)

, well this time when I correct it and it doesn;t work again. Ha- I don't know what you mean use C/C++ <tags>? was it?
I see the guidelines box next to this text--use
Expand|Select|Wrap|Line Numbers
  1. ..code goes here..
Yeah, like Savage said, if you put =cpp or =c between [CODE and the closing ], it will highlight the code appropriatly. Thanks for indenting next time ;)
And, thank you. It wasn't our professor that taught me breaks and continue, it was a tutor at school. Joe
Hmmm, do you know what your prof thinks about them? Some can be hard on that sort of thing, others don't care.


Adrian
Jun 4 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.