# Help with array

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-24-2009
walkonwater
Help with array
I have this code and I am trying to make it look like this:
Code:

```How many values are you going to enter (5-25)? 30 Too many values; try again. How many values are you going to enter (5-25)? 2 Too few values; try again. How many values are you going to enter (5-25)? 8 Please enter value 1: 12.2 Please enter value 2: 7.6 Please enter value 3: -200.3 Please enter value 4: 140.7 Please enter value 5: 890.23 Please enter value 6: 67.78 Please enter value 7: 99.9 Please enter value 8: 100.1```
I can get the first part as in how many values you want to enter which i can get until I add the next part into it which is Please enter value. When I add this is a for loop it messes it up.

This is my code when I just add the first part
Code:

```   numEntered = 0;   cout << "How many values are you going to enter (5-25)"; cin >> numEntered; while (numEntered < 5 || numEntered > 25 ) { if (numEntered < 5) { cout << "To Few values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; } else if (numEntered > 25) { cout << "To many values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; }```
and this is what i get:
Code:

``` How many values are you going to enter (5-25)30 To many values; try again How many values are you going to enter (5-25)2 To Few values; try again How many values are you going to enter (5-25)8```
This on top looks good but when i add the next part, it misses up:
This is my new code:
Code:

```numEntered = 0;   cout << "How many values are you going to enter (5-25)"; cin >> numEntered; while (numEntered < 5 || numEntered > 25 ) { if (numEntered < 5) { cout << "To Few values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; } else if (numEntered > 25) { cout << "To many values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; } for (i=1; i <= numEntered; i++) { cout << "Please enter value " << i <<  ": "; cin >> A[i]; }```
It makes it look like this:

Code:

```How many values are you going to enter (5-25)30 To many values; try again How many values are you going to enter (5-25)2 Please enter value 1: 8 Please enter value 2: 12.2 To Few values; try again How many values are you going to enter (5-25)To Few values; try again How many values are you going to enter (5-25)7.6 Please enter value 1: -200.3 Please enter value 2: 140.7 Please enter value 3: 890.23 Please enter value 4: 67.78 Please enter value 5: 99.9 Please enter value 6: 100.1 Please enter value 7:```
Can you tell me what I am doing wrong
• 04-24-2009
walkonwater
• 04-24-2009
Sentral
You are missing a brace to close the while loop. If you indented your code correctly you would have saw that.
• 04-24-2009
walkonwater
Ok I fixed the brace but still not luck
• 04-24-2009
cyberfish
What's your new correctly indented code?
• 04-24-2009
walkonwater
Code:

```numEntered = 0;   cout << "How many values are you going to enter (5-25)"; cin >> numEntered; while (numEntered < 5 || numEntered > 25 ) { if (numEntered < 5) { cout << "To Few values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; } else if (numEntered > 25) { cout << "To many values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; } } cout << endl << endl;```
This part works right for what I want it to do but when I add the rest of it, it screws up. I don't know what I am doing wrong? It is only a little mistake but it adds another output.

Code:

```   numEntered = 0;   cout << "How many values are you going to enter (5-25)"; cin >> numEntered; while (numEntered < 5 || numEntered > 25 ) { if (numEntered < 5) { cout << "To Few values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; } else if (numEntered > 25) { cout << "To many values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; } } cout << endl << endl; for (j=0; j <= numEntered; j++){ cout << "Please enter value ";     cin >> A[j]; }```
This above code looks like this:

Code:

```How many values are you going to enter (5-25)30 To many values; try again How many values are you going to enter (5-25)2 To Few values; try again How many values are you going to enter (5-25)8 Please enter value 12.2 Please enter value 7.6 Please enter value -200.3 Please enter value 140.7 Please enter value 890.23 Please enter value 67.78 Please enter value 99.9 Please enter value 100.1 Please enter value```
• 04-24-2009
walkonwater
Ok guys I did it,I just changed j =1
• 04-24-2009
walkonwater
but now I am trying to find the average and sum and also the average of numbers over 100.
• 04-24-2009
walkonwater
This is my new code but for some reason it gives me an outrageous number for sum
Code:

``` numEntered = 0;   cout << "How many values are you going to enter (5-25)"; cin >> numEntered; while (numEntered < 5 || numEntered > 25 ) { if (numEntered < 5) { cout << "To Few values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; } else if (numEntered > 25) { cout << "To many values; try again" << endl; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; } } cout << endl << endl; for (j=1; j <= numEntered; j++){ cout << "Please enter value ";     cin >> A[j]; } cout << endl << endl; sum = 0.0; for (j=0; j < 10; j++) {     sum += A[j];     }         cout << "The sum is " << sum;```
• 04-24-2009
cyberfish
Quote:

correctly indented
If you aren't willing to spend a few seconds fixing your indentation and make your code a lot easier to read, why should we spend our time helping you?
• 04-24-2009
walkonwater
Is this better?
Code:

```numEntered = 0;   cout << "How many values are you going to enter (5-25)";   cin >> numEntered; while (numEntered < 5 || numEntered > 25 ) { if (numEntered < 5) {   cout << "To Few values; try again" << endl;   cout << "How many values are you going to enter (5-25)";   cin >> numEntered; } else if (numEntered > 25) {   cout << "To many values; try again" << endl;   cout << "How many values are you going to enter (5-25)";   cin >> numEntered; } }   cout << endl << endl; for (j=1; j <= numEntered; j++){   cout << "Please enter value ";   cin >> A[j]; }   cout << endl << endl; sum = 0.0; for (j=0; j < numEntered; j++) {     sum = sum + A[j];     }         cout << "The sum is " << sum;```
• 04-24-2009
cyberfish
Code:

```numEntered = 0; cout << "How many values are you going to enter (5-25)"; cin >> numEntered; while (numEntered < 5 || numEntered > 25 ) {         if (numEntered < 5) {                 cout << "To Few values; try again" << endl;                 cout << "How many values are you going to enter (5-25)";                 cin >> numEntered;         }         else if (numEntered > 25) {                 cout << "To many values; try again" << endl;                 cout << "How many values are you going to enter (5-25)";                 cin >> numEntered;         }         } cout << endl << endl; for (j=1; j <= numEntered; j++){         cout << "Please enter value ";         cin >> A[j]; } cout << endl << endl; sum = 0.0; for (j=0; j < numEntered; j++) {         sum = sum + A[j]; }```
• 04-24-2009
walkonwater
Anyways since you corrected the "indention" why is my sum not working right?
• 04-24-2009
cyberfish
What is your input? What is the output? What is the expected output?
• 04-24-2009
walkonwater
This is what is wrong:
Code:

```How many values are you going to enter (5-25)30 To many values; try again How many values are you going to enter (5-25)2 To Few values; try again How many values are you going to enter (5-25)8 Please enter value 12.2 Please enter value 7.6 Please enter value -200.3 Please enter value 140.7 Please enter value 890.23 Please enter value 67.78 Please enter value 99.9 Please enter value 100.1 The sum is: 66550```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last