473,473 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

need help

5 New Member
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;
}
Sep 12 '06 #1
6 2095
Banfa
9,065 Recognized Expert Moderator Expert
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(beers) << " bottles of beer on the wall"<< endl;

Then define the function

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. using namespace std;
  3.  
  4. string NumberString(unsigned number)
  5. {
  6.     string result = "";
  7. // Handle special cases of number,
  8. //               11, 12, 13, 14, 15, 16, 17 ,18, 19
  9. //               and finally number > 99
  10.     if (number > 99)
  11.     {
  12.         result = "lots";
  13.     }
  14.     else
  15.     {
  16.         switch(number)
  17.         {
  18.         case 11:
  19.             result = "eleven";
  20.             break;
  21.  
  22. // handle other special cases
  23.  
  24.         default:
  25.             // Get tens digit
  26.             switch(number/10)
  27.             {
  28.             case 0:
  29.                 break;
  30.  
  31.             case 2:
  32.                 result += "twenty";
  33.                 break;
  34.             // etc
  35.             }
  36.  
  37.             if (number >= 20)
  38.             {
  39.                 result += " ";
  40.             }
  41.  
  42.             // Get units digit
  43.             switch(number%10)
  44.             {
  45.             case 1:
  46.                 result += "one";
  47.                 break;
  48.  
  49.             case 2:
  50.                 result += "two";
  51.                 break;
  52.             // etc
  53.             }
  54.         }
  55.     }
  56.  
  57.     return string;
  58. }
  59.  
Sep 12 '06 #2
lblock
5 New Member
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(int numstanzas);
void print_num_in_english(int num);

// ======================
// print_num_in_english
// Outputs n in English.
// N must be between 0-99.
// ======================
void print_num_in_english(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(int n)
{
// function below outputs n in English
print_num_in_english(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_english(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_english(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(num);
}

cout << endl;

system ("pause");
return 0;
}
Sep 13 '06 #3
Rakesh Mutharaju
14 New Member
one small change..

in the method void print_num_in_english(int num)

string result is not printed.

before return; statment add cout<<result;
Sep 14 '06 #4
lblock
5 New Member
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(int numstanzas);
void print_num_in_english(int num);

// ======================
// print_num_in_english
// Outputs n in English.
// N must be between 0-99.
// ======================
void print_num_in_english(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(int n)
{


// function below outputs n in English
print_num_in_english(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_english(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_english(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(num);
}

cout << endl;

system ("pause");
return 0;
}
Sep 14 '06 #5
Banfa
9,065 Recognized Expert Moderator Expert
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

Expand|Select|Wrap|Line Numbers
  1. switch(num)
  2. {
  3. case special_case:
  4.   handle special case
  5.   break;
  6.  
  7. case next_special_case:
  8.   handle next special case
  9.   break;
  10.  
  11. // other special cases
  12.  
  13. default:
  14.   // handle general case
  15.   switch to get 10's string
  16.  
  17.   switch to get units string
  18.   break;
  19. }
  20.  
also please use [code] [/code] round your code, if maintains white space and makes it easier to read.
Sep 15 '06 #6
lblock
5 New Member
thanks for the help.
Sep 18 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.