473,779 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
+ 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 2113
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(be ers) << " 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(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;
}
Sep 13 '06 #3
Rakesh Mutharaju
14 New Member
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;
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(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;
}
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
6328
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, column 13: there is no attribute "SRC" <bgsound src="C:\My Documents\zingwent.mids"> You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element. This error is...
5
2196
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: Objectives The purpose of this assignment is to have you practice the design of object-oriented classes, including one or more of the following concepts
0
1840
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, all dont work "Re:" need help "Re:need help"
9
2937
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 a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the device
7
3306
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
15
4644
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 communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
16
2540
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 uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
8
2750
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 the sequence nos and it should be such that i can access it through the structure .plz help me .
0
3963
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 Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
20
4285
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 is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
0
9633
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9928
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6724
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
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 we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.