Thread: [Jumping into C++] Chapter 7 Problem 1 Question

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,677
    Well it's a bit of a clue when you're doing this
    > for (int i=10; i < 30; i++)

    It does alright at the start
    Ten
    Eleven
    Twelve
    Thirteen
    Fourteen
    Fifteen
    Sixteen
    Seventeen
    Eighteen
    Nineteen

    But then goes haywire
    Twenty Two
    Thirty Two
    Forty Two
    Fifty Two
    Sixty Two
    Seventy Two
    Eighty Two
    Ninety Two

    It seems to me that all those "Two" should be "Twenty"
    Check your / and %
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Jul 2017
    Posts
    9
    Quote Originally Posted by Salem View Post
    Well it's a bit of a clue when you're doing this
    > for (int i=10; i < 30; i++)

    It does alright at the start
    Ten
    Eleven
    Twelve
    Thirteen
    Fourteen
    Fifteen
    Sixteen
    Seventeen
    Eighteen
    Nineteen

    But then goes haywire
    Twenty Two
    Thirty Two
    Forty Two
    Fifty Two
    Sixty Two
    Seventy Two
    Eighty Two
    Ninety Two

    It seems to me that all those "Two" should be "Twenty"
    Check your / and %
    I figured it out!

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    string tens(int number);
    string singles(int number);
    string bigger_tens(int number);
    int main()
    {
        /*cout << "Please type your number you wish to convert: ";
        int number;
        cin >> number; */
        for (int i=0; i < 100; i++)
        {
            if (i < 10)
            {
                cout << singles(i) << endl;
            }
            else if (i >= 10 && i < 20)
            {
                cout << tens(i) << endl;
            }
            else if (i >= 20 && i < 100)
            {
                cout << bigger_tens(i) << endl;
            }
        }
        return 0;
    }
    string singles(int number)
    {
        char *words[] =
        {
           "Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"
        };
        return words[number%10];
    }
    string tens(int number)
    {
        char *words[] =
        {
            "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
        };
        return words[number%10];
    }
    string bigger_tens(int number)
    {
        char *words[] =
        {
            "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
        };
        string first_word = words[number/10 - 2];
        string second_word = singles(number%10);
        if (second_word == "Zero")
        {
            return first_word;
        }
        else
        {
             string whole_word = first_word + "-" + second_word;
            return whole_word;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 07-09-2016, 08:13 AM
  2. Question on "Jumping into C++" Chapter 13 practice problem 1
    By Ben Sturm in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2015, 01:16 PM
  3. Jumping in C++ Chapter 5 Question 2
    By etricity in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2014, 02:18 AM
  4. Jumping into C++, Chapter 5 Question 1.
    By etricity in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2014, 06:55 AM
  5. Jumping into C++ chapter 8 question 5
    By twigz in forum C++ Programming
    Replies: 5
    Last Post: 03-07-2014, 04:43 PM

Tags for this Thread