|
-
Jan 7th, 2003, 10:06 AM
#1
Thread Starter
Frenzied Member
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.
Last edited by aewarnick; Jan 7th, 2003 at 11:31 AM.
-
Jan 7th, 2003, 12:30 PM
#2
The array loop is
for(int i = 0; i < arraysize; ++i)
not <=
Other than that it works fine for me.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 7th, 2003, 12:41 PM
#3
Thread Starter
Frenzied Member
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};
-
Jan 7th, 2003, 12:49 PM
#4
Thread Starter
Frenzied Member
Nevermind. I figured it out! Now I am getting excited!!!
-
Jan 7th, 2003, 01:33 PM
#5
Thread Starter
Frenzied Member
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;
}
-
Jan 7th, 2003, 01:49 PM
#6
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 7th, 2003, 01:58 PM
#7
Thread Starter
Frenzied Member
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?
-
Jan 7th, 2003, 02:57 PM
#8
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 7th, 2003, 03:00 PM
#9
Thread Starter
Frenzied Member
Oh! It breaks when he enter a correct number! That makes sense.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|