Well I can get the sum every time the user inputs an integer until they either input a negative number, or put a non-integer character. Problem: My sum calculations are slight off. Starts on line 24 & attempted output at 30. I just want to know why it's not adding correctly, so I can rework on my sum calculations
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
int j , i = 0, k = 0,number;
double sum = 0;
cout << "Enter Positive integer number: ";
while(cin >> number)
{
cout << endl;
if( number < 0)//test if the number is negative
{
cout << "Ending program since user has input a negative number" <<endl;
break;
}
int temp = number;
int p = 1;
while( temp > 0) //counting number of digits
{
sum = sum+temp; //Sum attempt.
temp /= 10;
p *= 10;
i++;
}
cout << sum << endl;
j = i % 3;
p /= 10;
while( i > 0 )//display integer number with 1000 seperator
{
cout << char ((number/p) +'0');
number %= p;
p /= 10;
i--;
k++;
j--;
if ((k % 3 == 0 && i > 0)||(j == 0 && i > 2) )
{
cout <<",";
k = 0;
}
}
cout << endl << endl;
cout << "This program will exit if you input any non-integer characters\n";
cout << "Enter another integer number: ";
}
return 0;
}
I don't quite understand. But is it your expected output?
Enter Positive integer number: 5000
Sum : 5000
Number with 1000 seperator : 5.000
This program will exit if you input any non-integer characters
Enter another integer number: 250000
Sum : 255000
Number with 1000 seperator : 255.000