My first time posting. This program is/was a homework problem. It works as required, but I would like to eliminate negative numbers and have it exit with the input of a negative rather than zero. All mods i have made just screwed it up and I know it's simple. What areas need to be changed to get this ?
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
class Numbers
{
private:
int number;
public:
Numbers(int a)
{number=a;}
void print();
};
void Numbers::print()
{
int n;
string lessthan20[20]={"zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen",
"fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
string tens[10]={"zero","ten","twenty","thirty","forty","fifty","sixty",
"seventy", "eighty", "ninety"};
if(number<0)
cout<<"negative ";
number=abs(number);
n=number/1000;
if(n>0)
cout<<" "<<lessthan20[n]<<" thousand ";
number%=1000;
n=number/100;
if(n>0)
cout<< lessthan20[n]<<" hundred ";
number%=100;
if(number>=20)
{
n=number/10;
if(n>0)
cout<<tens[n]<<" ";
}
else if(number>=10)
{
cout<< lessthan20[number]<<" ";
return;
}
number%=10;
if(number>0)
cout<<lessthan20[number];
cout<<" ";
}
int main()
{
int n;
cout<<" This program will take an input of up to four(4) numerals\n";
cout<<" and change them to a written number.\n";
cout<<" Enter up to a four-digit number(0 to exit): ";
cin>>n;
while(n!=0)
{
Numbers number(n);
number.print();
cout<<"\nEnter a number(0 to exit): ";
cin>>n;
}
system("pause");
return 0;
}