Why can t the switch statement be applied on strings

0 votes

When the following code is compiled, the following error message appears: 

Enter illegal.

int main()
{
    // Compilation error - switch expression of type illegal
    switch(std::string("raj"))
    {
    case"sda":
    }
}

String cannot be used in either switch or case. 

Why? 

Is there a good way for supporting logic comparable to switch on strings?

Jun 28, 2022 in C++ by Nicholas
• 7,760 points
9,115 views

1 answer to this question.

0 votes
The reason for this is due to the type system.

Strings are not supported as a type in C/C++.

It does support the concept of a constant char array, but it does not fully comprehend the concept of a string.

The compiler must comprehend what it means for two values to be equal in order to create code for a switch statement.

This is a simple bit comparison for objects like ints and enums.

However, how should the compiler compare two strings?

Case sensitive, insensitive, culturally aware, and so forth...

Without a complete awareness of a string this cannot be adequately addressed.

Furthermore, C/C++ switch statements are usually created as branch tables.

A branch table for a string style switch is not nearly as simple to create.
answered Jun 28, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
1 answer

Why is Turbo C++ showing this error in DOSBox on my Mac?

Your installation must be defective!  I have a Mac, and I'm typing this on it while using TurboC++.  Consider uninstalling and then reinstalling the programme. Download the package in the same way as you would a.dmg programme from the internet.  (For example, drag and drop the programme into the Applications folder)  Ascertain that your Applications folder is global to your system.  This is what I mean: When in Finder, select the "GO" option from the top menu bar. From the drop down option, choose "Computer." In the newly opened window, click on your hard disc. There is a "Applications" folder there.  That's where you should put TurboC++. Go to Launchpad, and start Turbo C++. ...READ MORE

answered Jun 28, 2022 in C++ by Damon
• 4,960 points
1,137 views
0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 5, 2022 in C++ by Nicholas
• 7,760 points
399 views
0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
350 views
0 votes
0 answers

how the iterator in c++ could be printed?

Suppose, I have declared a vector in C++ like ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
614 views
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
525 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,385 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
688 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
537 views
0 votes
1 answer

why are copy constructors needed and what are the cases where they are very helpful?

A copy constructor is a member function ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
826 views
0 votes
1 answer

Why does C++ need the scope resolution operator?

No. There is no scope resolution operator ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
866 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP