Jump to content

Hello , I need some hlep.

RaresKool

I need some help with a c++ program that would read any input number from 1 to 100 and outout it as text ( without using  100 if statements ;p , for example 21 -> twenty one )

I tried a few of my ideas but all of them failed horribly.

Please help

Link to comment
Share on other sites

Link to post
Share on other sites

I barely know any C++, but couldn't you just take the input, assign a variable to the string version of the input, then output the variable?

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites


void print(int& count, int unit, int xx, std::string const& units)

{

int div = unit * (xx==0?1:10);

int index = count / div;

int sub = index * div * (index >=2 ? 1 : 0);

std::string name = names[xx][index];

if (name[name.size()-1] == '-') {

name.erase(name.size()-1);

index = (count-sub)/unit;

name.append(names[0][index]);

}

if (name != "") {

std::cout << name << " " << units << " ";

}

count = count - (count/unit*unit);

}

void printNumber(int number)

{

if (number == 0) {

std::cout << "zero";

return;

}

print(number, 1000000, 0, "million");

print(number, 100000, 0, "hundred");

print(number, 1000, 1, "thousand");

print(number, 100, 0, "hundred");

print(number, 1, 1, "");

}

int main()

{

int number;

std::cin >> number;

printNumber(number);

std::cout << "\n";

}

Link to comment
Share on other sites

Link to post
Share on other sites

There's enough examples of this from a simple Google search:

#include <iostream>#include <string>using namespace std;int main(){    string numStr;// for user entered number    // names for use in output    string onesName[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };    string teensName[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };    string tensName[] = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };    string illion_preName[] = { "m", "b", "tr", "quadr", "quint", "sext", "sept", "oct", "non", "dec" };    string decillion_preName[]={ "un", "duo", "tre", "quattuor", "quin", "sex", "septen", "octo", "novem" };    char repeat = 'y';    do// as long as user wishes to enter number for naming    {        cout << "Number = "; cin >> numStr;        cout << "power = " << numStr.size() - 1 << endl;        if( numStr.size() > 66 )        {            cout << "The number's too damn big! Try again." << endl;            continue;        }        while( numStr.size()%3 != 0 )            numStr = '0' + numStr;// pad the string with leading '0' until size = multiple of 3        // for each group of 3 digits from most to least significant        for( unsigned int i = 0; i < numStr.size(); i += 3 )        {            if( numStr[i + 0] > '0' )// treat the hundreds place                cout << onesName[ numStr[i + 0] - '0' - 1 ] << " hundred ";            if( numStr[i + 1] == '0' || numStr[i + 1] > '1' )// treat tens and ones digits for non-teens case            {                if( numStr[i + 1] > '1' ) cout << tensName[ numStr[i + 1] - '0' - 2 ] << " ";                if( numStr[i + 2] > '0' ) cout << onesName[ numStr[i + 2] - '0' - 1 ] << " ";            }            else// special teens case                cout << teensName[ numStr[i + 2] - '0' ] << " ";            // naming each factor of 1,000            unsigned int j = ( numStr.size() - i )/3;            if( j == 2 ) cout << "thousand ";            else if( j > 2 )            {                if( j <= 12 ) cout << illion_preName[ j - 3 ];// 'xx' before "illion" cases                else if( j <= 21 ) cout << decillion_preName[ j - 13 ] << "dec";// 'xx' before "dec" + "illion" cases                else if( j == 22 ) cout << "vigint";// special 'xx' before "vigint" + "illion" case                cout << "illion ";// the "illion" suffix            }        }        cout << endl << "Repeat? (y/n): ";        cin >> repeat;    }while( repeat == 'y' );    cout << endl;    return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

A nice python example here, if you can translate it?

import mathnumber = int(input("Enter number to print: "))unit_array = ["zero","one","two","three","four","five","six","seven","eight","nine"]teen_array = ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]decades_list =["twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]if number <= 9 and number > 0:    print(unit_array[number].capitalize())elif number >= 10 and number <= 19:    tens = int(number % 10)    print(teen_array[tens].capitalize())elif number > 19 and number <= 99:    ones = math.floor(number/10)    twos = int(ones - 2)    tens = int(number % 10)    if tens == 0:        print(decades_list[twos].capitalize())    elif tens != 0:        print(decades_list[twos].capitalize() + "-" + unit_array[tens])else:    print("Input out of range")

Eeh, by gum.
 

ThrustJetViperPowerMustang: FX-6100 @4.4GHz (Stock Cooler) / 4x4GB Hyperam @ 1333MHz / OCZ Octane 250GB SSD / Asus HD6670 2GDDR3 / Asus M5A78LM-USB3

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×