67 014
Assignments Done
99,4%
Successfully Done
In November 2018

# Answer to Question #55684 in C++ for Mhia marquez inon

Question #55684
Write a program that accepts and outputs its equivalent in words.

Sample input/output dialogue:
enter a number: 1540
one thousand five hundred forty

Note: the maximum input is 3000

Numbers.h
#pragma once
#include <iostream>
#include <string.h>

using namespace std;

class Numbers
{
public:
//nonnegative integer
unsigned int numbers;
const int MAX_NUM = 3000;
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] = { "", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety" };
string hundred = "hundred";
string thousand = "thousand";
//ctor with nonnegative integer
Numbers(unsigned int num);
//prints the English description of the Numbers object
void print(unsigned int numbers);
};

Numbers.cpp
#include <iostream>
#include <string>
#include "Numbers.h"

using namespace std;

Numbers::Numbers(unsigned int num)
{
numbers = num;
}

void Numbers::print(unsigned int numbers)
{
if (numbers > MAX_NUM)
{
cout << "Wrong input data!!!";
}
else if (numbers >= 1000)
{
print(numbers / 1000);
cout << thousand;
if (numbers % 1000)
{
if (numbers % 1000 < 100)
{
cout << " and";
}
cout << " ";
print(numbers % 1000);
}
}
else if (numbers >= 100)
{
print(numbers / 100);
cout << hundred;
if (numbers % 100)
{
cout << " and ";
print(numbers % 100);
}
}
else if (numbers >= 20)
{
cout << tens[numbers / 10];
if (numbers % 10)
{
cout << " ";
print(numbers % 10);
}
}
else
{
cout << lessThan20[numbers] << " ";
}
return;
}

NumbersToString.cpp
#include <iostream>
#include <string>
#include "Numbers.h"

using namespace std;

void main()
{
unsigned int inputNum;
while (true)
{
cout << "Enter a number : ";
cin >> inputNum;
Numbers MyNumber = Numbers(inputNum);
MyNumber.print(inputNum);
cout << endl;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!