need help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lblock
    New Member
    • Sep 2006
    • 5

    need help

    this is what i have and i need to display the actual number name not the number itself.
    #include <cstdlib>
    #include <iostream>
    using namespace std;


    int main()
    {
    int beers=99;
    while (beers>0)
    {

    cout <<beers <<" bottles of beer on the wall"<< endl;
    cout<< beers <<" bottles of beer"<< endl;
    beers--;
    cout << "Take one down pass it around" <<endl;
    cout << beers << " bottles of beer on the wall"<< endl;
    cout << endl;
    }



    system ("Pause");
    return 0;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    If you want the number beers as text then you will have to write it

    change

    cout << beers << " bottles of beer on the wall"<< endl;

    to

    cout << NumberString(be ers) << " bottles of beer on the wall"<< endl;

    Then define the function

    Code:
    #include <string>
    using namespace std;
    
    string NumberString(unsigned number)
    {
        string result = "";
    // Handle special cases of number,
    //               11, 12, 13, 14, 15, 16, 17 ,18, 19
    //               and finally number > 99
        if (number > 99)
        {
            result = "lots";
        }
        else
        {
            switch(number)
            {
            case 11:
                result = "eleven";
                break;
    
    // handle other special cases
    
            default:
                // Get tens digit
                switch(number/10)
                {
                case 0:
                    break;
    
                case 2:
                    result += "twenty";
                    break;
                // etc
                }
    
                if (number >= 20)
                {
                    result += " ";
                }
    
                // Get units digit
                switch(number%10)
                {
                case 1:
                    result += "one";
                    break;
    
                case 2:
                    result += "two";
                    break;
                // etc
                }
            }
        }
    
        return string;
    }

    Comment

    • lblock
      New Member
      • Sep 2006
      • 5

      #3
      this is what i have now but still having the same problem with not being able to display the number name and not the number. thank for the last help.

      #include <iostream>
      #include <cstdlib>

      using namespace std;

      // Function prototypes
      void print_stanza(in t numstanzas);
      void print_num_in_en glish(int num);

      // =============== =======
      // print_num_in_en glish
      // Outputs n in English.
      // N must be between 0-99.
      // =============== =======
      void print_num_in_en glish(int num)
      {
      int n;
      string result = "";
      switch(num)
      {
      case 11:
      result = "eleven";
      break;
      case 12:
      result = "twelve";
      break;
      case 13:
      result = "thirtteen" ;
      break;
      case 14:
      result = "fourteen";
      break;
      case 15:
      result = "fifteen";
      break;
      case 16:
      result = " sixteen";
      break;
      case 17:
      result = "seventeen" ;
      break;
      case 18:
      result = "eighteen";
      break;
      case 19:
      result = "nineteen";
      break;
      }
      // handle other special cases


      // Get tens digit
      switch(n/10)
      {
      case 0:
      break;
      case 2:
      result += "twenty";
      break;
      case 3:
      result += "thirty";
      break;
      case 4:
      result += "fourty";
      break;
      case 5:
      result += "fifty";
      break;
      case 6:
      result += "sixty";
      break;
      case 7:
      result += "seventy";
      break;
      case 8:
      result += "eighty";
      break;
      case 9:
      result += "ninety";
      break;
      }

      if (n >= 20)
      {
      result += " ";
      }

      // Get units digit
      switch(n%10)
      {
      case 1:
      result += "one";
      break;
      case 2:
      result += "two";
      break;
      case 3:
      result += "three";
      break;
      case 4:
      result += "four";
      break;
      case 5:
      result += "five";
      break;
      case 6:
      result += "six";
      break;
      case 7:
      result += "seven";
      break;
      case 8:
      result += "eight";
      break;
      case 9:
      result += "nine";
      break;
      }

      return;
      }

      // =============== =======
      // print_stanza
      // Outputs an entire stanza for n bottles.
      // =============== =======
      void print_stanza(in t n)
      {
      // function below outputs n in English
      print_num_in_en glish(n);

      // account for "one bottle" vs. many "bottles"
      if (n==1)
      {
      cout << " bottle of beer on the wall, " << endl;
      }
      else
      {
      cout << " bottles of beer on the wall, " << endl;
      }
      print_num_in_en glish(n);

      if (n==1)
      {
      cout << " bottle of beer on the wall, " << endl;
      }
      else
      {
      cout <<" bottles of beer on the wall, " << endl;
      }
      cout << "Take one down, pass it around," << endl;
      n--;
      print_num_in_en glish(n);
      if (n==1)
      {
      cout << "bottle of beer on the wall, " << endl;
      }
      else
      {
      cout <<"bottles of beer on the wall, " << endl;
      }
      cout << endl;
      return;
      }


      // =============== =======
      // main function
      // =============== =======
      int main()
      {

      // Variable declarations
      int num;

      // Loop from 99 down to 0
      for (num=99; num>0; num--)
      {
      print_stanza(nu m);
      }

      cout << endl;

      system ("pause");
      return 0;
      }

      Comment

      • Rakesh Mutharaju
        New Member
        • Sep 2006
        • 14

        #4
        one small change..

        in the method void print_num_in_en glish(int num)

        string result is not printed.

        before return; statment add cout<<result;

        Comment

        • lblock
          New Member
          • Sep 2006
          • 5

          #5
          now i have this and it displaies all of the right ones through ten and then when it gets to the teens it displaies "nineteen nine bottels of beer on the wall. it never even gets to the other numbers it will start over again with zero after nineteen nine.
          #include <iostream>
          #include <cstdlib>

          using namespace std;
          string result = " ";
          // Function prototypes
          void print_stanza(in t numstanzas);
          void print_num_in_en glish(int num);

          // =============== =======
          // print_num_in_en glish
          // Outputs n in English.
          // N must be between 0-99.
          // =============== =======
          void print_num_in_en glish(int num)
          {
          int n, tens, ones;
          tens=num/10;
          ones=num%10;
          n=num;
          switch(num)
          {
          case 10:
          cout << "Ten";
          break;
          case 11:
          cout << "Eleven";
          break;
          case 12:
          cout << "Twelve";
          break;
          case 13:
          cout << "Thirtteen" ;
          break;
          case 14:
          cout << "Fourteen";
          break;
          case 15:
          cout << "Fifteen";
          break;
          case 16:
          cout << " Sixteen";
          break;
          case 17:
          cout << "Seventeen" ;
          break;
          case 18:
          cout << "Eighteen";
          break;
          case 19:
          cout << "Nineteen";
          break;
          case 0:
          cout << "Zero";
          break;

          switch(tens)
          {
          case 20:
          cout <<"Twenty";
          break;
          case 30:
          cout << "Thirty";
          break;
          case 40:
          cout << "Fourty";
          break;
          case 50:
          cout << "Fifty";
          break;
          case 60:
          cout << "Sixty";
          break;
          case 70:
          cout << "Seventy";
          break;
          case 80:
          cout << "Eighty";
          break;
          case 90:
          cout << "Ninety";
          break;
          }
          }
          switch(ones)
          {

          case 1:
          cout << "One";
          break;
          case 2:
          cout << "Two";
          break;
          case 3:
          cout << "Three";
          break;
          case 4:
          cout << "Four";
          break;
          case 5:
          cout << "Five";
          break;
          case 6:
          cout <<"Six";
          break;
          case 7:
          cout << "Seven";
          break;
          case 8:
          cout << "Eight";
          break;
          case 9:
          cout << "Nine";
          break;
          }



          return;
          }

          // =============== =======
          // print_stanza
          // Outputs an entire stanza for n bottles.
          // =============== =======
          void print_stanza(in t n)
          {


          // function below outputs n in English
          print_num_in_en glish(n);

          // account for "one bottle" vs. many "bottles"
          if (n==1)
          {

          cout << " bottle of beer on the wall, " << endl;
          }
          else
          {
          cout << " bottles of beer on the wall, " << endl;
          }
          print_num_in_en glish(n);

          if (n==1)
          {
          cout << " bottle of beer on the wall, " << endl;
          }
          else
          {
          cout << " bottles of beer on the wall, " << endl;
          }
          cout << "Take one down, pass it around," << endl;
          n--;
          print_num_in_en glish(n);
          if (n==1)
          {
          cout << "bottle of beer on the wall, " << endl;
          }
          else
          {
          cout << " bottles of beer on the wall, " << endl;
          }
          cout << endl;
          return;
          }


          // =============== =======
          // main function
          // =============== =======
          int main()
          {

          // Variable declarations
          int num;

          // Loop from 99 down to 0
          for (num=99; num>0; num--)
          {
          print_stanza(nu m);
          }

          cout << endl;

          system ("pause");
          return 0;
          }

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by lblock
            now i have this and it displaies all of the right ones through ten and then when it gets to the teens it displaies "nineteen nine bottels of beer on the wall. it never even gets to the other numbers it will start over again with zero after nineteen nine.
            That is because the structure you have is slightly wrong the switch for units should be inside the first switch statement and you are missing a "default:" just before the switch for 10's.

            The basic structure is

            Code:
            switch(num)
            {
            case special_case:
              handle special case
              break;
            
            case next_special_case:
              handle next special case
              break;
            
            // other special cases
            
            default:
              // handle general case
              switch to get 10's string
            
              switch to get units string
              break;
            }
            also please use &#91;code&#9 3; &#91;/code&#93; round your code, if maintains white space and makes it easier to read.

            Comment

            • lblock
              New Member
              • Sep 2006
              • 5

              #7
              thanks for the help.

              Comment

              Working...