Can someone please help me on how to do the conversion of char to int
using either a c-style cast or function-style cast. I have include my code.

#include <iostream>
using namespace std;

int main()
            char state;
	cout << "Please select one of the following state abbreviations:\n";
	cout << "\n";
	cout << "AL, FL, GA, SC, NC\n";
             cin >> state;
             if ( state == "AL" || "Al" || "al")
		cout << "State selected:\tAL\t\tAlabama\n";
	else if ( state == "FL" || "Fl" || "fl")
		cout << "State selected:\tFL\t\tFlorida\n";
	else if ( state == "GA" || "Ga" || "ga")
		cout << "State selected:\tGA\t\tGeorgia\n";
	else if (state == "SC" || "Sc" || "sc")
		cout << "State selected:\tSC\t\tSouth Carolina\n";
	else if (state == "NC" || "Nc" || "nc")
		cout << "State selected;\tNC\t\tNorth Carolina\n";
		cout << "State not listed. Please select one of the listed states\n";
	return 0;


<< moderator edit: added code tags: [code][/code] >>

Recommended Answers

All 2 Replies

I think you've got a couple of problems. First off, if you define state as 'char', then it can only handle a single character. You probably want to change that to string.

You should also change this;
if ( state == "AL" || "Al" || "al")


if ( state == "AL" || state == "Al" || state == "al")

char state;

The above is only a single character, not a string.

if ( state == "AL" || "Al" || "al")

The above is equivalent to the following, which is not what you want.

if ( state == ("AL" || "Al" || "al"))

Besides, to compare C-style strings, you use strcmp.

If state were a std::string, then you might do this.

if ( state == "AL" || state == "Al" || state == "al" )

As a rule of thumb while you are new, if you think you need a cast to "fix" something, you are wrong. When you can explain what a cast actually is to others, then you may be more ready to use them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.