Hi, so I am having a bit of a problem, but I don't know if it's my code or my compiler that's the issue. My assignment is to ask the user to input integers, while the program stores the even numbers in one array and the odds in another array. When the user puts in 0, the program is supposed to show them how many evens they put in and how many odds, and then list the values in each array.
So I wrote up the program for it quickly and I thought it would work just fine. No such luck. It always got the number of evens and the number of odds correct, but instead of listing the numbers from the arrays, it usually lists the last x numbers the user input (x being equal to the number of elements in that array). I also occasionally get wacky 5+ digit integers appearing at the ends of the output.
Now, the thing is, my code doesn't look like it has anything wrong with it, so I am thinking it may be my compiler. I'm using Xcode on a Mac OS X 10.7. I am not sure how good this software is, but I've been using it for my class, since I no longer have a Windows readily available. Because of this, I am not sure if my code is bad or if my compiler is the real issue.
After a few reiterations and different approaches, I found my first try had the fewest problems, so that is what I am sticking with in hopes of adjusting it to work.
So here is my code, anyways:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
using namespace std;
int main() {
int input, remainder, even = 0, odd = 0;
int evenArray[even];
int oddArray[odd];
cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
cin >> input;
while (input != 0) {
remainder = input % 2;
if (remainder == 0) {
evenArray[even] = input;
even++;
}
else {
oddArray[odd] = input;
odd++;
}
cout << "Enter another integer: ";
cin >> input;
}
cout << "\nThe number of evens is " << even << ".\n";
cout << "The even values are: ";
for(int i = 0; i < even; i++) {
cout << evenArray[i] << " ";
}
cout << endl;
cout << "The number of odds is " << odd << ".\n";
cout << "The odd values are: ";
for(int i = 0; i < odd; i++) {
cout << oddArray[i] << " ";
}
}
| |
And here's an example of it being run:
This program accepts integers until you enter 0.
Please enter a value: 1
Enter another integer: 2
Enter another integer: 3
Enter another integer: 4
Enter another integer: 5
Enter another integer: 5
Enter another integer: 3
Enter another integer: 2
Enter another integer: 4
Enter another integer: 6
Enter another integer: 4
Enter another integer: 3
Enter another integer: 0
The number of evens is 6.
The even values are: 2 4 2 4 6 3
The number of odds is 6.
The odd values are: 2 4 2 4 1912356720 32767
|
I would very much appreciate it if someone could tell me what the heck is going on.