Allowing user to input decimal.

This code is near complete, the only task that is left is allowing the user to input a decimal and then two integers, or automatically using .00 decimal.

The automatic part: /*This is not correct.

printf(".%.2d\n", number);

*/

But that does no good for me. Question: Do I have to create a some sort of while loop again, to allow the user to input a decimal followed by integer?

Code:

`#include <iostream>`

#include <cstdlib>

using namespace std;

int main()

{

int j , i = 0, k = 0;

int number;

static_cast<double>(number);

double sum = 0.0;

double Average = 0.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;

sum = (sum+temp);

Average = sum/2;

while(temp > 0) //counting number of digits

{

temp /= 10;

p *= 10;

i++;

}

cout << "Total Sum: " << sum << endl;

cout << "Average: "<< Average << endl;

j = i % 3;

p /= 10;

while( i > 0 )//display integer number with 1000 seperator

{

//entering gives me error if digits exceed 9

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;

}

}

/*This is not correct.

printf(".%.2d\n", number);

*/

cout << endl << endl;

cout << "This program exits if you input negative number and/or input non-integer\n";

cout << "Enter another integer number: ";

}

return 0;

}

Re: Allowing user to input decimal.

This line does nothing

Code:

`static_cast<double>(number);`

as the result of the cast is not used.

The average of a set of numbers is their sum divided by the number of numbers. So in this case you need to keep a count of the number of numbers entered and use that for the division to find the correct average.

For 32 bit signed integers, the maximum value is 2147483647 (see limits.h)

Quote:

//entering gives me error if digits exceed 9

because when the number of digits exceeds 9,

causes p to overflow its max value.

Code:

`/*This is not correct.`

printf(".%.2d\n", number);

*/

try

Code:

`printf("\n%.2lf\n", (double)number);`

which casts number to a double so it can be displayed as a double with decimals.

Note that at this point in your code, number does not have the correct value to display.

Quote:

allowing the user to input a decimal and then two integers,

If the user needs to be able to input a number containing a decimalpoint, then this is a double/float number and can be input directly as such.

When confronted with a program which does not work as expected, you need to use the debugger to trace through the program to see where its behaviour deviates from that which is expected. being able to use successfully the debugger is as important as being able to write code and is a skill which needs to be mastered.

Also, just using single letter variable names is not very helpful when trying to understand the code. Use meaninful variable names and don't re-use a variable for more than one purpose.