What Do I Put?

Hello. I'm a bit new to C++, so forgive me if it's a bit choppy. I am trying to make a program that takes 10 integers, and COUTs the sum of the negative numbers, positive numbers, and total of all 10. So I built the below program, and it said that the storage size of num_nums and pos_nums is undefined. I don't know how to fix this since I don't have a set amount of positive or negative numbers. What do I put?

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
using namespace std;

int main()
{
    // Variables //
    int numbers[10];
    int neg_nums[];
    int pos_nums[];
    int num_negs, num_pos;
    int neg_sum, pos_sum, tot_sum;
    int counter(0);

    // Data Collection //
    cout << "Enter the Data Below: \n";
    cout << "All numbers should only be integers and can be positive and negative. \n";
    cout << "#1 > ";
    cin >> numbers[0];
    cout << "#2 > ";
    cin >> numbers[1];
    cout << "#3 > ";
    cin >> numbers[2];
    cout << "#4 > ";
    cin >> numbers[3];
    cout << "#5 > ";
    cin >> numbers[4];
    cout << "#6 > ";
    cin >> numbers[5];
    cout << "#7 > ";
    cin >> numbers[6];
    cout << "#8 > ";
    cin >> numbers[7];
    cout << "#9 > ";
    cin >> numbers[8];
    cout << "#10 > ";
    cin >> numbers[9];

    // Splitting //
    for (int i = 0; i < 10; i++) {

        if (numbers[i] < 0)
            neg_nums[i] = numbers[i];
        else
            pos_nums[i] = numbers[i];

    }

    // Setting num_negs and num_pos //
    num_negs = sizeof(neg_nums) / sizeof(neg_nums[0]);
    num_pos = sizeof(pos_nums) / sizeof(pos_nums[0]);

    // Finding Sums //
    while (counter < num_negs) {
        neg_sum += neg_nums[counter];
        counter ++;
    }
    counter = 0;
    while (counter < num_pos) {
        pos_sum += pos_nums[counter];
        counter ++;
    }
    counter = 0;
    while (counter < 10) {
        tot_sum += numbers[counter];
        counter ++;
    }

    // COUT Sums //
    cout << "The sum of the negative numbers is " << neg_sum << endl;
    cout << "The sum of the positive numbers is " << pos_sum << endl;
    cout << "The sum of all the numbers is " << tot_sum << endl;


    return(0);
}


[ I'll put a for loop for the input later ;) ]
Last edited on
Hello and welcome!

So on lines 8 and 9, you omit how large neg_nums and pos_nums are supposed to be. How large they are does need to be known at compile time. You can't resize them as you go along.

If you're up for some reading (and possibly skipping ahead of whatever material you're reading from), this is the sort of thing that std::vector would be really good for. Here's a primer about it:
http://www.cplusplus.com/articles/37Mf92yv/

Otherwise, you could have neg_nums and pos_nums be as large as they could possibly be (10 elements) and repeatedly increment num_negs and num_pos in your loop on line 39.

One more warning: it's generally a good habit to initialize all your variables when you declare them. In your code, neg_sum, pos_sum, and tot_sum are uninitialized when you first read them on lines 54, 59, and 64.

Hope that helps.

-Albatross
Last edited on
Ok, Thank you! I'll try the vector, and if I don't have any luck, then I'll just ask my teacher. Thanks for the help!
You don't need numbers, neg_nums or pos_nums at all.

1
2
3
4
5
6
7
8
9
10
11
12
int tot_sum = 0, pos_sum = 0, neg_sum = 0;

for (int i = 0; i < 10; ++i) {
    int n;
    cout << '#' << i + 1 << "> ";
    cin >> n;
    tot_sum += n;
    if (n < 0)
        neg_sum += n;
    else
        pos_sum += n;
}

I know, I just wanted to get some practice in arrays.
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
Topic archived. No new replies allowed.