Why won't this end line?

#include <iostream>
using namespace std;
int main()
{
int total = 0, counter = 0, subtotal, preSub = 0;

cout << "Please enter an integer " << endl;
cin >> subtotal;
while (counter < 2)
{
if (subtotal == 0)
{
cout << "subtotal " << subtotal << endl;
cin >> subtotal;
counter ++;
if (subtotal == 0)
{
cout << "total " << total << endl;
break;
}
}
else
{
counter = 0;
total += subtotal;
preSub += subtotal;

cin >> subtotal;
}
if (subtotal == 0)
{
cout << "subtotal " << preSub << endl; //<- I want it to end line here
preSub = 0;
cin >> subtotal;
if (subtotal == 0)
{
cout << "total " << total;
break;
}
}

}


return 0;
}

Result:

Please enter an integer
1
2
0
subtotal 34
1
0
subtotal 50
total 8Program ended with exit code: 0

My issue is that when the subtotal is printed it groups the user input (cin >> subtotal in bold) with the printed subtotal. I want it to result like so:

1
2
0
Subtotal 3 <---- end line here
4
1
0

help is appreciated, thanks.
closed account (Dy7SLyTq)
what do you mean by end line? you are ending it with endl
It should work. It works fine for me when I copy/paste

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
#include <iostream>
using namespace std;
int main()
{
    int total = 0, counter = 0, subtotal, preSub = 0;
 
    cout << "Please enter an integer " << endl;
    cin >> subtotal;
    while (counter < 2) {
	if (subtotal == 0) {
	    cout << "subtotal " << subtotal << endl;
	    cin >> subtotal;
	    counter++;
	    if (subtotal == 0) {
		cout << "total " << total << endl;
		break;
	    }
	} else {
	    counter = 0;
	    total += subtotal;
	    preSub += subtotal;
 
	    cin >> subtotal;
	}
	if (subtotal == 0) {
	    cout << "subtotal " << preSub << endl;	//<- I want it to end line here
	    preSub = 0;
	    cin >> subtotal;
	    if (subtotal == 0) {
		cout << "total " << total;
		break;
	    }
	}
 
    }
 
 
    return 0;
}
Please enter an integer
1
2
0
subtotal 3
4
1
0
subtotal 5
0
total 8
Process returned 0 (0x0)   execution time : 12.562 s
Press any key to continue.



All I did was format using http://indentcode.net
closed account (Dy7SLyTq)
thats amazing!!! i need to start writting these sites down
@ Giblet:

That website is amazing thanks!

maybe it's my compiler (xcode) then, formatted but still having the same issue :/
Topic archived. No new replies allowed.