-
illegal operations
Code:
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
int R, F=R+1;
cout << "How many people are there? "; cin >> R;
int responses [20]= {0}; // initialize all memory locations to 0.
int frequency [21]= {0};
cout << "Enter scores: " << endl;
for (int i=0; i<=R; i++){
cin >> responses[i]; if (responses[i]<=-1) break;
}
for (int answer=0; answer <=R; answer++)
++frequency [responses[answer]]; // This is the key to it all!!!
cout << "Number" << setw(14) << "Frequency" << endl;
for (int rating=1; rating <=F; rating++)
cout<< rating << setw(11) << frequency[rating] << endl;
system("PAUSE");
return 0;
}
Every time I run this it loops up to 185 and then says it performed an illegal operation. Even if I put -1 as the first score.
-
The array loop is
for(int i = 0; i < arraysize; ++i)
not <=
Other than that it works fine for me.
-
Why does it crash when I enter -1 when I do not put 20 and 21 in the array size? Like this:
int responses []= {0}; // initialize all memory locations to 0.
int frequency []= {0};
-
Nevermind. I figured it out! Now I am getting excited!!!
-
Here is what I came up with so far. I could not figure out why you said to increment i before the loop. That only made my numbers go up to 9999 and not 10000 and <= works fine.
Code:
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
int R;
cout << "How many people are there? (10000 or less): "; cint: cin >> R;
if (R>10000) {cout << "\nNot greater than 10000 Try again:"; goto cint;}
int responses [10001]= {0}; // initialize all memory locations to 0.
int frequency [10001]= {0};
cout << "Enter scores not above 10000:" << endl;
for (int i=0; i<= R; i++){
cin >> responses[i]; if (responses[i]<0 || responses[i]>10000) break;
}
for (int answer=0; answer<= R; answer++)
++frequency [responses[answer]]; // This is the key to it all!!!
cout << "Number" << setw(11) << "Frequency" << endl;
int F= R;
for (int number=1; number<= F; number++)
cout<< number << setw(8) << frequency[number] << endl;
end:
system("PAUSE");
return 0;
}
-
gotos are bad!
A better way would be:
Code:
for(;;) {
cin >> R;
if(T <= 10000)
break;
cout << "Too large.\n";
}
< is better as it is more natural to C/C++.
The last index of an array of size 10000 is 9999. So if you ask how many people there are and the user enters 10 then you're actually reading in 11.
Actually there's no difference between
for(int i=0; i<10; ++i)
and
for(int i=0; i<10; i++)
except that the first is a little bit faster.
And you might want to insert some newlines, they make the code more readable.
-
If I use break; it will exit the loop and the user will not have a chance to enter in R again.
What is wrong with goto?
-
goto makes it hard to follow program flow. This is the main reason not to use it. jim mcnamara could probably give you more reasons.
Anyway, generations of C programmers have made the same experience: goto is evil.
My loop works. The break is executed when the user enters a correct value and you therefore don't want him to enter it again.
-
Oh! It breaks when he enter a correct number! That makes sense.