Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

enum problem 1

Status
Not open for further replies.

Mudassar

Programmer
Oct 3, 2000
110
AU
Hi, I am trying to make a simple program which will convert a letter to another letter. So I am using the enum statement. The only problem is, that when I try ASKING the user to type a letter (eg, cin >> toConvert) I get an error message:

binary '>>' : no operator found which takes a right-hand operand of type 'main::specialChars' (or there is no acceptable conversion)




#include <string>
#include <iostream>
using namespace std;

int main()
{
string firstLetter;
string lettered;


enum specialChars {a='m', b='l'}; // <--- some letters to convert

enum specialChars toConvert;

cout << &quot;Enter a Letter to convert (a or b)&quot;;
toConvert = a; // <---- I want to ASK the user to input a letter instead. It works this way, but not the asking way.
// cin >> toConvert <--- Doesn't work
cout << char(toConvert);

} -----------------
MK

Hope this helps!
 
#include <iostream>

enum specialChars
{
a=109,
b=108,
INVALID=-1
};

specialChars convert( char c )
{
specialChars temp;
switch( c )
{
case 'a': return a;
case 'b': return b;
default : break;
}

return INVALID;
}

int main()
{
specialChars sc;
char cTemp;

std::cout<< &quot;Enter a letter to convert:&quot;;
std::cin >> cTemp;
sc=convert( cTemp );

( sc==INVALID ) ? std::cout<< &quot;No conversion.&quot; :
std::cout<< char( sc );

return 0;
}

Write a conversion function to convert to the proper enum. Mike L.G.
mlg400@blazemail.com
 
Ahh I see.. So you had to first convert it to char and then call it.

I just wanted to know:

specialChars convert( char c )
{
specialChars temp;
switch( c ) //*** <--- What does this line mean?
{
case 'a': return a;
case 'b': return b;
default : break;
} -----------------
MK

Hope this helps!
 
Basically, switch( c ) is just a simple switch statement that drops down to whatever char ( or the default case ) &quot;c&quot; is storing at the time convert() is called. You can clarify that statement by substituting the switch for an if/else if ->

if( c=='a' )
return a;
else if( c=='b' )
return b;
else
return INVALID;
Mike L.G.
mlg400@blazemail.com
 
Thanks a lot LiquidBinary. You have been a great help. -----------------
MK

Hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top