Question

Using C++ Design a class called Numbers that can be used to translate integers in the range 0 to ...

Using C++

Design a class called Numbers that can be used to translate integers in the range 0 to 9999 into an English description of the number.

The class should have a single integer member variable:

int number;

and a static array of string objects that specify how to translate the number into the desired format. For example, you might use static string arrays such as:

string lessThan20[20] = {"zero", "one", ..., "eighteen", "nineteen"};
string hundred = "hundred";
string thousand = "thousand";

The class should have a constructor that accepts a nonnegative integer and uses it to initialize the Numbers object. It should have a member function, print(), that prints the English description of the Numbers object.

Demonstrate the class in a main program that prompts the user to input a number and then prints out its English description.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:::

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
class Numbers
{
public:
   static int numbers;
   static void print();
};

void Numbers::print()
{
   int b;
   string lessThan20[] = { "zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
           "twelve", "thirteen", "fourteen", "fifteen","sixteen", "seventeen", "eighteen", "nineteen" };
   string hundred = " hundred ";
   string thousand = " thousand ";
   string tens[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
   if (numbers < 0)
       cout << "it is a negative number";
   numbers = abs(numbers);
   b = numbers / 1000;
   if (b > 0)
       cout << " " << lessThan20[b] << thousand;
   numbers %= 1000;
   b = numbers / 100;
   if (b > 0)
       cout << lessThan20[b] << hundred;
   numbers %= 100;
   if (numbers>= 20)
   {
       b = numbers / 10;
       if (b > 0)
           cout << tens[b] << " ";
   }
   else if (numbers >= 10)
   {
       cout << lessThan20[numbers] << " ";
       return;
   }
   numbers %= 10;
   if (numbers > 0)
       cout << lessThan20[numbers];
   cout << " ";
}
int Numbers::numbers;
int main()
{
   int b;

   cout << "Please enter a number from 0-999 or enter 0 to Exit : "<<" ";
   cin >> b;
   while (b != 0)
   {
       cout << "The number " << b << " " << "would be translated into - "<<" ";
       Numbers::numbers = b;

       Numbers::print();
       cout << "\nPlease enter a number from 0-999 or enter 0 to Exit : "<<" ";

       cin >> b;
   }

   cin.ignore();
   cin.get();

   system("pause");
   return 0;
}

output:

Console terminated> CPP_Workspace.exe [C/C++ Application] CAUserstMohammad Shahrukhl Desktoplc language_workspaceleclipse cIC

Add a comment
Know the answer?
Add Answer to:
Using C++ Design a class called Numbers that can be used to translate integers in the range 0 to ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i need help making this C++ code. Lab #2 Assignments: Numbers Class that translate whole dollar...

    i need help making this C++ code. Lab #2 Assignments: Numbers Class that translate whole dollar amounts in the range 0 through 9999 into an English description of the number. Numbers Class Design a class numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into an English description of the number. For example, the number 713 would be translated into the string seven hundred thirteen, and 8203 would be translated into eight thousand...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • implement a class called PiggyBank that will be used to represent a collection of coins. Functionality...

    implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods. class PiggyBank The PiggyBank class definition and symbolic constants should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace...

  • implement a class called PiggyBank that will be used to represent a collection of coins. Functionality...

    implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods. class PiggyBank The PiggyBank class definition and symbolic constants should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace...

  • Please write the program in C++ Problem Description: Design a class called Date. The class should...

    Please write the program in C++ Problem Description: Design a class called Date. The class should store a date in three integers: month, day, and year.     Create the necessary set and get methods.       Design member functions to return the date as a string in the following formats:                                     12/25/2012                                     December 25, 2012                                     25 December 2012 Input Validation: Do not accept values for day greater than 30 or less than 1. Do not accept values for month...

  • C++ Programing In this exercise, you will design a class memberType. The class has the following...

    C++ Programing In this exercise, you will design a class memberType. The class has the following data members: memberName. A string that holds the name of a person memberID. A string that holds the member identification number numBooks. An int that holds the number of books bought purchaseAmt. A double that holds the amount spent on books In addition, the class should have the following constructor and other member functions. Constructor. The constructor should accept the person’s name and member...

  • C++ This exercise will introduce static member variables and static methods in class to you. Class...

    C++ This exercise will introduce static member variables and static methods in class to you. Class Department contain information about universities departments: name students amount in addition it also stores information about overall amount of departments at the university: departments amount class Department { public: Department(string i_name, int i_num_students); ~Department(); int get_students(); string get_name(); static int get_total(); private: string name; int num_students; static int total_departments; }; Carefully read and modify the template. You have to implement private static variable "total...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to...

    Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT