### Expected primary expression

I need some help writing an inclusive program that computes the sum of the range of a user input. I have an idea of how to get an inclusive program to work, but I can not get the program I wrote to compile.

Here is the problem: write a program that computes the sum of all the integers in a range specified by the user. In addition to the main function, there should be a function that accepts two parameters that specify a lower and upper bound. It should compute the sum of all the integers between those bounds, inclusive, and display that sum. The main function should prompt the user for the two bounds and call that function passing in those values. It should then allow the user to compute another interval sum or to quit.

Here is the code:
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include using namespace std; void inclusive(int num1, int num2, int sum){ while (num1 <= num2){ if ((num1 % == 0) || (num1 % == 1)){ // I get an "expected primary-expression before '==' token here num1 = sum; sum += num1; } cout << "Sum = " << num1 << endl;} } int main() { int num1, num2, sent1 = 1, sum; while (sent1 != 0){ cout << "Enter a lower bound integer: "; cin >> num1; cout << "Enter an upper bound integer: "; cin >> num2; inclusive(num1,num2); cout << "Enter 0 to quit, 1 to continue: "; cin >> sent1; } return 0; } ``````
Last edited on
Line 7 is nonsense. The % operator requires values on both left and right sides... but you only have a value on the left side.

What are you trying to accomplish with that line?

To clarify:

`num1 % ___what____ == 0`
Last edited on
I was trying to store and add both even and odd numbers. I made a program earlier that added all the even numbers and was thinking this program would be similar.

What do you mean by values on both sides?
Last edited on
 What do you mean by values on both sides?

The mod operator (%) divides two numbers and returns the remainder after that division.

So `11 % 3` would be `2` because 11/3 is 3 remainder 2.

For it to work, you have to give it a number to divide by. `num1 % == 0` makes no sense, because the part after the % sign needs to be the number you want to divide by.

If you want to check for odd/even numbers, then you'd want to divide by 2: `num1 % 2 == 0`

Although there is no reason to check for odd/even numbers here, as whether a number is odd or even has no significance at all for this problem.
I'm aware of that but I cannot think of another way to store the values. For instance lower bound = 2 upper bound = 4 sum = 9.

I tried to write it another way but I get an astronomical number.
 ``12345678910111213141516171819202122232425262728293031`` ``````#include using namespace std; void inclusive(int num1, int num2, int sum){ while (num1 <= num2){ num1 = sum; // stores the first integer num1++; // adds 1 (inclusive?) sum += num1; // adds sequentially...? } cout << "Sum = " << num1 << endl; } int main() { int num1, num2, sent1 = 1, sum; while (sent1 != 0){ cout << "Enter a lower bound integer: "; cin >> num1; cout << "Enter an upper bound integer: "; cin >> num2; inclusive(num1,num2,sum); cout << "Enter 0 to quit, 1 to continue: "; cin >> sent1; } return 0; }``````
 For instance lower bound = 2 upper bound = 4 sum = 9.

How do you know the sum is 9? I'm not being a wiseass or anything... I'm trying to get you to think critically.

You have to think about the very basic steps involved. Try to explain them in English. Once you have the basic steps explained... that's your logic. All you have to do from there is convert that logic to code.

So what do you do, mentally, to know that the sum is 9 in this case?
Thanks for the help.

I do get the math though. 2 + 3 + 4 = 9.

I think I got it, but I still have a question. Can you define an integer within a function?

 ``12345678910111213141516171819202122232425262728293031`` ``````#include using namespace std; void inclusive(int num1, int num2, int sum = 0){ // can I declare sum = 0 in this statement? // int v = 0; << ---- Works fine when used while (num1 <= num2){ sum += num1; num1++; } // sum = v; cout << "Sum = " << sum << endl; //When I don't add an extra variable I get the same number 2130567177 } int main() { int num1, num2, sent1 = 1, sum; while (sent1 != 0){ cout << "Enter a lower bound integer: "; cin >> num1; cout << "Enter an upper bound integer: "; cin >> num2; inclusive(num1,num2,sum); cout << "Enter 0 to quit, 1 to continue: "; cin >> sent1; } return 0; }``````

Some questions to consider:
what is the value of `sum` at line 18?
what is the value of `sum` at line 6 (before the loop begins)
 I do get the math though.

This isn't necessarily about math. This is about logic.

Although your latest attempt seems like it would work. So it looks like you've figured the logic out. Well done. =)

 Can you define an integer within a function?

Yes. And you should.

Furthermore, `sum` is not being used in main at all, so it probably should not be there. There's also no reason for sum to be a parameter:

 ``12345`` ``````void inclusive(int num1, int num2){ // <- sum shouldn't be a parameter int sum = 0; // <- it's only used in the 'inclusive' function, so make it local to 'inclusive' //... }``````
Last edited on
 `Although your latest attempt seems like it would work`

As posted, it didn't work because of the garbage parameter `sum` which over-rode the default parameter.
Thanks for the help. Worked fine after I declared the sum in the int main.

Thanks for the tip about sum in the parameter. I was under the impression that for sum to be, for lack of a better word , a "returned" value it had to be in the parameter.

Thanks again for the help.
Topic archived. No new replies allowed.