-
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
-
-
You are missing a brace to close the while loop. If you indented your code correctly you would have saw that.
-
Ok I fixed the brace but still not luck
-
What's your new correctly indented 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;
}
}
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
-
Ok guys I did it,I just changed j =1
-
but now I am trying to find the average and sum and also the average of numbers over 100.
-
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;
-
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?
-
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;
-
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];
}
-
Anyways since you corrected the "indention" why is my sum not working right?
-
What is your input? What is the output? What is the expected output?
-
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
-
Quote:
Code:
for (j=1; j <= numEntered; j++){
cout << "Please enter value ";
cin >> A[j];
}
Arrays are 0-based.
-
Quote:
Originally Posted by
cyberfish
Arrays are 0-based.
So should i set j=0
-
Should you?
Trace the code on paper.
-
-
walkonwater-
i have a very similair problem I am working on. would you mind contacting me and emailing me your completed source code so i can view the header files?