Hello GamaByteLuke,
I was working on the following code before I see that you are doing this for practice. Like what
dutch has presented this version still used the arrays and can be easily changes to use the rest of the code that you started with.
Your first problem has to do with
int neg_nums[];
and
int neg_nums[];
. Both of these are zero length arrays and that is not allowed in C++. They need to be at least one or more in size and that number or variable must be considered a constant value. The other part is having no idea how many numbers you will be storing in the arrays they need to be great than what you might need and you will need a counter to keep track of how much of the array is used.
Also there may be some concepts or code that you do not understand yet, but is very helpful.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <limits>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
// <--- An alternative.
//using std::cin;
//using std::cout;
//using std::endl;
int main()
{
// Constant Variables //
constexpr size_t MAXSIZE{ 10 }; // <--- Or const stze_t MAXSIZE = 10; for pre C++11 standards.
// Variables //
int numbers[MAXSIZE]{};
int neg_nums[MAXSIZE]{}; // <--- Must have a size greater than zero and be a constant value.
int pos_nums[MAXSIZE]{};
int neg_num_counter{}, pos_num_counter{};
int neg_sum{}, pos_sum{}, tot_sum{};
int counter(0);
std::cout << "\n Enter the Data Below:";
std::cout << "\n All numbers should only be integers and can be positive and negative. \n" << std::endl;
for (size_t index = 0; index < MAXSIZE; index++)
{
std::cout << " #" << index + 1 << ' ';
std::cin >> numbers[index];
if (numbers[index] < 0)
{
neg_nums[index] = numbers[index];
neg_sum += numbers[index];
tot_sum += numbers[index];
neg_num_counter++;
}
else
{
pos_nums[index] = numbers[index];
pos_sum += numbers[index];
tot_sum += numbers[index];
pos_num_counter++;
}
}
// COUT Sums //
std::cout << "\nThe sum of the negative numbers is " << neg_sum << std::endl; // <--- Added the "\n".
std::cout << "The sum of the positive numbers is " << pos_sum << std::endl;
std::cout << "The sum of all the numbers is " << tot_sum << std::endl;
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
|
In lines 3 - 9 the comments should be enough to explain it. If not let me know.
Line 14 was added to make things easier. Now yo have only one place to make a change and everywhere in the program that uses "MAXSIZE" will change.
In lines 17 - 21 it is a good idea to initialize your variables so they do not contain a garbage value. The empty {}s are available from C++11 standards on.
In lines 24 and 25 the use of "\n" and the spaces at the beginning are more my personal preference as it makes it easier for me to read the screen. This si only a suggestion or an option.
The for loop is an example, like what
dutch
did, and can easily be changed to use what you did in your original code. Sorry to say I have not looked at that part yet because I did not realize what you are doing.
Sorry about this. I just realized I missed adding the header file "<limits>" and now all the line number references are one less than what they should be.
The last bit of code I use to keep the console window open before the program reaches the "return" and closed the window. This is optional and nor required.
I will go back and take a look at the rest of your code and put it back into the program and see if there is any other problems.
Hope that helps,
Andy