-
October 21st, 2013, 04:01 PM
#1
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|