473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

need help

5
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 2092
Banfa
9,065 Expert Mod 8TB
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
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
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
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 Expert Mod 8TB
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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...

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.