In: Computer Science
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits>
c++ project. need help.
#include <iostream>
#include <limits>
using namespace std;
int digitsInNumber; // global variable to store
bool validNumber(int); // function to check whether entered number is single digit or two digit
void isSingleDigit(int);//if number is single digit
void lessThan19(int); // if number is two digit and less than 19
void greaterThan19(int);// if number is two digit and greater than 10
int main()
{
int n=0; // to store user entered number
bool reEnter=true;// to check whether entered number is valid i.e 1-99 or -99
bool whetherInteger=true;
// this loop will take input from user and check whether number is between 1-99 or -99
do{
cin>>n;
if(!cin) //if value is not integer
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
whetherInteger=false;
}
else //if value is integer
{
if((n>0 &&n<100)|| n==-99)
{
reEnter=false;
}
}
}while(reEnter==1);
// loop to calculate number of digits in the user entered number
int temp=n;
int count=0;
while (temp != 0) {
temp = temp / 10;
++count;
digitsInNumber=count;
}
// for our output there are 4 cases
// case1: when number is -99
// case2: when number is between 1-9
// case3: when number is between 10-19
// case4: when number is between 20-99
// if number is -99 output will be "have a nice day"
if(n==-99) // case 1
{
cout<<"Have a nice day";
}
else
{
if(digitsInNumber==1) // case 2
{
isSingleDigit(n);
}
if(digitsInNumber==2)
{
if(n<20) //case 3
{
lessThan19(n);
}
else // case 4
{
greaterThan19(n);
}
}
}
return 0;
}
// This method will run single digit number
// it will take number as argument and will print specific word
void isSingleDigit(int n)
{
switch(n)
{
case 1:cout<<"one ";break;
case 2:cout<<"two ";break;
case 3:cout<<"three ";break;
case 4:cout<<"four ";break;
case 5:cout<<"five ";break;
case 6:cout<<"six ";break;
case 7:cout<<"seven ";break;
case 8:cout<<"eight ";break;
case 9:cout<<"nine ";break;
}
}
// This method will run two digit number from 10-19
// it will take number as argument and will print specific word
void lessThan19(int n)
{
switch(n)
{
case 10:cout<<"ten";break;
case 11:cout<<"eleven ";break;
case 12:cout<<"twelve ";break;
case 13:cout<<"thirteen ";break;
case 14:cout<<"fourteen ";break;
case 15:cout<<"fifteen ";break;
case 16:cout<<"sixteen ";break;
case 17:cout<<"seventeen ";break;
case 18:cout<<"eighteen ";break;
case 19:cout<<"nineteen ";break;
}
}
// This method will take number from 20-99
// we have similarities in these numbers
// Their 10's place digit are same for 10 numbers
void greaterThan19(int n)
{
int digit1=n%10; // unit place of number
n=n/10;
int digit0=n%10; // 10's place of number
// for digit 0 we can use specific words and for digit we call the method same as thaf of single digit number
switch(digit0)
{
case 2:cout<<"twenty ";break;
case 3:cout<<"thirty ";break;
case 4:cout<<"fourty ";break;
case 5:cout<<"fifty ";break;
case 6:cout<<"sixty ";break;
case 7:cout<<"seventy ";break;
case 8:cout<<"eighty ";break;
case 9:cout<<"ninety ";break;
}
if(digit1>0)
{
isSingleDigit(digit1);
}
}