Help with error!

Hi, I'm currently learning bloodshed c++ and this is my 3rd time coding. Please help me with the error I am facing.

Problem occurs at: cout << "Enter the Integer value: " << (ctr++) ": ";
Message: expected `;' before string constant

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
#include <iostream>

using namespace std;

int main ()

{
    int sump=0, sumn=0, ctr=0;
    int num, n;
    
    cout << "This program intends to add all positive and negative given integers";
    cout << endl;
    cout << endl;
    
    cout << "Please enter input size: ";
         cin >> n;
    
    cout << endl;
    cout << endl;
    
    for (ctr=0; ctr<n; ctr++)
    
{
    
    cout << "Enter the Integer value: " << (ctr++) ": ";
         cin >> num;
    
    if (num>0) sump = sump+num;
    
    if (num<0) sumn = sumn+num;
    
}
    
    cout << endl << "The sum of all positive integers: " << sump;
    cout << endl << "The sum of all negative integers: " << sumn;
    
    cout << endl;
    cout << endl;
    
system ("pause");
    
}


Thanks!
You are missing operator << in that line
cout << "Enter the Integer value: " << (ctr++) ": ";

you should use

cout << "Enter the Integer value: " << (ctr++) << ": ";
Thanks for that!

However it seems to skip by 2, 4, 6
"Enter the Integer value: 2:
--- 4:
--- 6:

but I solved it by modifying it

(ctr++)

into

(ctr+1)


Can you explain how did that work? Aren't they the same?
with ctr + +, the compiler takes the last value reached by ctr in the for loop and when inserted into the

cout << (ctr + +),

it still increases by 1;

while if you consider (ctr +1),
the compiler takes the initial value of ctr,
ctr = 0;
and the sum 1, then in the for loop, via ctr + +, will increase the value of one at a time.

Can you try this, if You change (ctr + 1), with (ctr+4)

Last edited on
I changed it to (ctr+4) and it immediately started at 4. I find out that

1
2
3
4
5
for (ctr=0; ctr<n; ctr++)
    
{
    
    cout << "Enter the Integer value: " << (ctr++) ": "; 


is the reason messing up the program therefore I added the value

1
2
3
int unk=1;

cout << "Enter the Integer value: " << (unk++) ": "; 



and it worked perfectly fine.


Thanks for the information!
Last edited on
Topic archived. No new replies allowed.