User writes 2 numbers. How much is the whole part of the number and how much is the decimal part of the number.
I am new to this, and i tried to write a program but i completely failed it. Please help! It also has to ask the user if he wants to restart the program after 2 numbers being entered.
int _tmain(int argc, _TCHAR* argv[])
{
float ncs;
long cd;
float ncds;
short i;
i=1;
for(;i<= 2;)
{
cout<<"Enter a decimal number"<<endl;
cin>>ncs;
cd=(long)ncs;
ncds=ncs-cd;
cout<<setprecision(2)<<"For "<<i+1<<". the number that is "<<ncs<<" is ";
cout<<"decimal part= "<<ncds<<", the whole part: ";
cout<<cd<<endl;
i=i+1;
}
system("pause");
return 0;
}
#include <iostream>
#include <iomanip>
#include <limits>
usingnamespace std;
int main()
{
longdouble ncs;
long cd;
longdouble ncds;
char again = 'Y';
constint BUFFER_SIZE = 256;
do
{
system("CLS");
for(int i = 0; i < 2; i++)
{
cout << "Enter a decimal number: ";
while(!(cin >> ncs))
{
cin.clear();
cin.ignore(BUFFER_SIZE, '\n');
cout << "Enter a decimal number: ";
}
cin.ignore(BUFFER_SIZE, '\n');
cd = static_cast<long>(ncs);
ncds = ncs - cd;
cout << setprecision(numeric_limits<longdouble>::digits10)
<< "The number you entered is "<< ncs << endl;
cout << "The whole part is " << cd << endl;
cout << "decimal part is " << ncds << endl;
}
cout << "Would you like to play again? (Y/N): Y\b";
if((again = toupper(cin.get())) == '\n')
again = 'Y';
else
cin.ignore(BUFFER_SIZE, '\n');
}
while(again == 'Y');
return EXIT_SUCCESS;
}
If your grader is a stickler about rounding errors when there are too many decimal places or handling overflow for numbers that are too large, you may need to elaborate to prevent a user from inputting such data.