Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8821049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:45:30+00:00 2026-06-14T05:45:30+00:00

How can I rewrite this code in such a way that user is always

  • 0

How can I rewrite this code in such a way that user is always shown the correct state(CA,AL etc.) string whenever he passes a valid direction.

i.e How do I make sure there is a valid case statement for each of those enum_types in the directions enum during compile time than in runtime?

For. Eg. I have intentionally commented out the case statement for east. Is there a way to catch this at compile time.

My gut feel is (NO) that this is why the language provides a default case and hence this may not be possible. But I’ll leave this to the experts.

#include <iostream>
#include <string>

using namespace::std;

typedef enum 
{
    min_dir = -1,
    north,
    south,
    east,
    west,
}directions;

directions get_direction( string user_choice)
{
    if(user_choice == "north")
    {
        return north;
    }
    else if (user_choice == "south")
    {
        return south;   
    }
    else if (user_choice == "east")
    {
        return east;    
    }
    else if (user_choice == "west")
    {
        return west;    
    }
    else 
    {
        return min_dir;
    }
}

int main()
{
    string user_direction;
    cout << "Enter direction\n";
    cin >> user_direction;

    directions my_dir = get_direction(user_direction);
    cout << " Print direction's description\n";

    if( my_dir == min_dir)
    {
        // User passed junk
        return -1;
    }

    switch(my_dir)
    {
    case north:
        cout << "North - New york\n";break;
    case south:
        cout << "South - Alabama\n";break;
//  case east:
//      cout << "East - North Carolina\n";break;
    case west:
        cout << "West - California\n";break;
    default:
        cout << "Should Ideally never get here\n";break;
    }
    system("pause");
    return 0;
}

Edit:
This is just an example to illustrate the point. This for code at work. They have compile this both in Windows(MSVC) and linux (gcc). Would this only be a warning? I’ll need a stricter enforcement.

Can I write some code that will error out during a make process if an enum doesn’t have a case statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:45:31+00:00Added an answer on June 14, 2026 at 5:45 am

    In GCC (g++) and Clang, there is -Wswitch-enum, which will warn you if you do not have a case for a possible value for the enum type you’re switching over (even if you have a default case).

    In MSVC, there is the comparable C4062 which comes from warning level 3, but it will not warn you if you have a default statement. If you want warnings in that case, you need to enable the level 4 warning C4061, which produces a warning when an enumerated value is missing, even if a default case is provided.

    As far as making it an error goes: all compilers have a “treat warnings as errors” option. In GCC and Clang, it’s -Werror; in MSVC, it’s /WX.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I rewrite this code to check for all characters including the swedish
How can I rewrite this code to coffeescript: $('.delete_action').click(function(event) { $.get('/delete_action?name=' + event.target.name +
Isn't there some way to re-write the following code, such that I don't need
how I can rewrite this: for (int i = 0; i < numberOfSpaces; i++)
How can I rewrite this query to Doctrine DBAL query or better to createQuery()
Can anyone spot what is wrong with this URL rewrite? I can't get it
Can this be simplified to a one liner? Feel free to completely rewrite it
How can I rewrite he following code using C#3.0 automatic properties? private int _myValue;
How can I rewrite Apple's GLCameraRipple example so that it doesn't require iOS 5.0?
I have a url rewrite that looks like this: RewriteEngine On RewriteRule ^cancun/tours/$ location-tours-listings.php?locationName=Cancun

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.