Skip to content

Instantly share code, notes, and snippets.

@xseano
Last active July 1, 2018 18:41
Show Gist options
  • Select an option

  • Save xseano/d4d9cea7c6dda3bffe0eb7fe1b1fb12c to your computer and use it in GitHub Desktop.

Select an option

Save xseano/d4d9cea7c6dda3bffe0eb7fe1b1fb12c to your computer and use it in GitHub Desktop.
Chapter 6 #9
#include <iostream>
#include "tgmath.h"
// For the sake of not having to prepend datatypes with std:: :P
using namespace std;
int main ()
{
cout << "Enter a number 1-4 digits: ";
string num;
cin >> num;
bool thousand = false;
bool hundred = false;
bool ten = false;
bool one = false;
int thousandCount = 0;
int hundredCount = 0;
int tenCount = 0;
int oneCount = 0;
int number = stoi(num);
string howMany = num + " is ";
if (number / 100 >= 10)
{
thousand = true;
thousandCount = (number / 100) / 10;
number -= thousandCount * 1000;
}
if (number / 10 >= 10)
{
hundred = true;
hundredCount = (number / 10) / 10;
number -= hundredCount * 100;
}
if (number / 10 >= 1)
{
ten = true;
tenCount = (number / 10) / 1;
number -= tenCount * 10;
}
if (number / 1 < 10)
{
one = true;
oneCount = (number / 1) / 1;
number -= oneCount * 1;
}
if (thousand == true)
{
howMany.append(to_string(thousandCount) + " thousand ");
}
if (hundred == true)
{
howMany.append(to_string(hundredCount) + " hundred ");
}
if (ten == true)
{
howMany.append(to_string(tenCount) + " tens ");
}
if (one == true)
{
howMany.append(to_string(oneCount) + " ones ");
}
cout << howMany << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment