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;
}
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.
Comments