How do I fix this? (99 bottles of beer)

emanuel1337

I started studying Computer Science 2 months ago. I need help with this.

The compiler is giving me this warnings and my program isn't giving the number in words.

Lab4Problem3.cpp:202:11: warning: multi-character character constant [-Wmultichar]

Lab4Problem3.cpp:202:26: warning: character constant too long for its type [enabled by default]

Lab4Problem3.cpp: In function ‘int main()’:

Lab4Problem3.cpp:27:16: warning: the address of ‘std::string word_number(std::string, std::string)’ will always evaluate as ‘true’ [-Waddress]

Lab4Problem3.cpp:29:16: warning: the address of ‘std::string word_number(std::string, std::string)’ will always evaluate as ‘true’ [-Waddress]

Here is what I have:

#include <iostream>
#include <string>

using namespace std;

string word_number(string units, string tens);
int num_entered;

int main ()
{

  char answer;

  do
    {

      cout << "Enter the number of bottles to throw on the wall: " << endl;
      cin >> num_entered;
      cout << endl;
  
      if (num_entered < 99 && num_entered >= 0)
    {

      while (num_entered > 0)
        {
           
          cout << word_number;
          cout << " bottles of beer on the wall,\n";
          cout << word_number;
          cout << " bottles of beer,\n"
           << "Take one down, pass it around.\n";
        
          num_entered--;
        }
      if (num_entered == 0)
        cout << "Zero bottles of beer on the wall" << endl;
    }
      else
    cout << "Invalid number!" << endl;

      cout << "Do you want to try it again? (Y/N)" << endl;
      cin >> answer;

    } while (answer =='Y' || answer=='y');

  return 0;

}


string word_number(string units, string tens)
{
  int tens_number, units_number;
  string result;

  if (num_entered < 20)
    {
      if(num_entered < 10)
    {
      units_number = num_entered;
      switch (units_number)
        {
        case 1:
          units =="One";
          break;
        case 2:
          units =="Two";
          break;
        case 3:
          units =="Three";
          break;
        case 4:
          units =="Four";
          break;
        case 5:
          units =="Five";
          break;
        case 6:
          units =="Six";
          break;
        case 7:
          units =="Seven";
          break;
        case 8:
          units =="Eight";
          break;
        case 9:
          units =="Nine";
          break;
        }

      return(units);

    }
      else
    units_number = num_entered%10;
      switch(units_number)
    {
    case 0:
      units =="Ten";
      break;
    case 1:
      units =="Eleven";
      break;
    case 2:
      units =="Twelve";
      break;
    case 3:
      units =="Thirteen";
      break;
    case 4:
      units =="Fourteen";
      break;
    case 5:
      units =="Fifteen";
      break;
    case 6:
      units =="Sixteen";
      break;
    case 7:
      units =="Seventeen";
      break;
    case 8:
      units =="Eighteen";
      break;
    case 9:
      units =="Nineteen";
      break;
    }
      return (units);
    }
  else
    tens_number = num_entered/10;
    units_number = num_entered%10;
  switch (tens_number)
    {
      /*case 0:
    tens == "Zero";
    break;*/
    case 1:
      tens =="Ten";
      break;
    case 2:
      tens =="Twenty";
      break;
    case 3:
      tens =="Thirty";
      break;
    case 4:
      tens =="Fourty";
      break;
    case 5:
      tens =="Fifty";
      break;
    case 6:
      tens =="Sixty";
      break;
    case 7:
      tens =="Seventy";
      break;
    case 8:
      tens =="Eighty";
      break;
    case 9:
      tens =="Ninety";
      break;
    }
    
  units_number = num_entered%10;
  
  switch (units_number)
    {
    case 1:
      units =="One";
      break;
    case 2:
      units =="Two";
      break;
    case 3:
      units =="Three";
      break;
    case 4:
      units =="Four";
      break;
    case 5:
      units =="Five";
      break;
    case 6:
      units =="Six";
      break;
    case 7:
      units =="Seven";
      break;
    case 8:
      units =="Eight";
      break;
    case 9:
      units =="Nine";
      break;
    }

    result = 'tens' + "-" + 'units';

 return result;

}
LihO

Seems you're having problems with syntax here:

std::string word;
if (word == "One") {    // <-- this is comparison
   ...
}
word = "Two";           // <-- this is assignment

and when you do:

std::string result'
result = 'tens' + "-" + 'units';

'tens' and 'units' are multi-byte characters rather than strings. What you should do there is:

std::string result;
result = tens + "-" + units;

Side note: Indentation of code is important. If it will be easier to read, it will be easier for you to work on it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to learn JAVA and I want to write 99 bottles of beer song

From Dev

Trying to code the 99 bottles of beer song

From Dev

Trouble looping string with 99 bottles of beer

From Dev

Dropbox install stuck at 99%, how do I fix it? (and any dpkg errors)

From Dev

Bottles of Beer - what is this: var bottlesDiv = document

From Dev

New to Java, trying to code Bottles of Beer song

From Dev

What's wrong with my 'Bottles of beer on the wall' loop in Ruby

From Dev

What's wrong with my 'Bottles of beer on the wall' loop in Ruby

From Dev

How do I fix this heart?

From Dev

How do I fix this formula?

From Dev

How do I fix these BSODs?

From Dev

How do I fix the KeyListener

From Dev

How do I fix a DivideByZeroException

From Dev

How do I fix this output?

From Dev

How do I fix a memory leak in java

From Dev

how do i apply the maas bug fix?

From Dev

How do I fix a Git detached head?

From Dev

What is a PartialEvaluationExceptionExpression and how do I fix it?

From Java

What is a NoReverseMatch error, and how do I fix it?

From Dev

How do I fix git certificate errors?

From Java

What is a NullReferenceException, and how do I fix it?

From Java

What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?

From Java

How do I fix a function that returns NAN?

From Dev

How do I fix this dependency issue in Clojure?

From Dev

How do I fix my neomuttrc syntax?

From Dev

What is a NoSuchBeanDefinitionException and how do I fix it?

From Dev

What is an OutOfMemoryError and how do I debug and fix it

From Dev

How do I fix facebook circular redirect?

From Dev

How do I fix postfix TLS?

Related Related

HotTag

Archive