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";
	else
		cout << "State not listed. Please select one of the listed states\n";
	return 0;

}

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

Dani AI

Generated

Brief correction and a practical plan: the thread’s problem is not a mysterious cast — it’s the data type and how the input is compared. and already pointed out the type/comparison issues. Two clear, maintainable options are (1) treat the input as a two-character std::string, normalize the case, and look it up in a table; or (2) if you truly need the numeric value of a single character, convert that char explicitly to an int.

A robust, modern approach is to normalize the user token and use a lookup map so you don’t end up with long chained if/else tests. Example pattern:

#include <string>
#include <unordered_map>
#include <algorithm>
#include <cctype>
#include <iostream>

// read token into `code`, convert each character to uppercase safely,
// then find it in an unordered_map that maps "AL" -> "Alabama", etc.

Key implementation notes: read into a std::string (or use getline), remove surrounding whitespace if needed, convert characters with a cast to unsigned char inside std::toupper to avoid undefined behavior, then use map.find(normalized) to get the full name.

If the original intent was to convert a single char to its numeric code (ASCII/encoding), prefer the explicit C++ cast for clarity:

char ch = 'A';
int value = static_cast<int>(ch); // value is 65 on ASCII machines

Avoid C-style casts unless you need their broader (and riskier) behavior; static_cast communicates intent.

Final troubleshooting tips: verify input length (expect 2 for state codes), handle extra whitespace and unexpected input, and enable compiler warnings (e.g. -Wall) to catch suspicious expressions. This keeps logic simple, safe, and easy to extend (add more states or use case-insensitive matching without repeating comparisons).

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")

to:

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.