473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
6 1896
ilikepython
844 Expert 512MB
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
1,764 Expert 1GB
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
1,251 Expert 1GB
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
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
1,764 Expert 1GB
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
1,251 Expert 1GB
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

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

Similar topics

2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
0
by: sjaak538 | last post by:
Hello I've a question about printing complex array's with print_r $xmlC->obj_data I get this example bellow But can anybody give me one example on how to echo $xmlC->obj_data; I try'd with...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
2
by: maciej | last post by:
I got an array that consists of elements that are arrays also. Now I wish I could add an element to the middle of it. Let mi give you an example: array ( - array(1,15,apple), -...
13
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if...
7
by: Bangalore | last post by:
Hi all, Plz clarify me, on the implementation of two or three dimensional array using overloaded operator. Thanks, in advance Bangalore
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
19
by: Henry | last post by:
I finally thought I had an understanding of multi dimensional arrays in C when I get this: #include <stdio.h> #define max_x 3 #define max_y 5 int array;
27
by: Ian Tuomi | last post by:
Say I have an array: int foo and it has an unknown number of integers in it. How can I find out how many? I tried: #include <stdio.h> int ArraySize(int array) { int i = 0; while(array !=...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.