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

  • SEARCH
  • Home
  • 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 8537361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:53:03+00:00 2026-06-11T10:53:03+00:00

I have one C++ define statement as: #define PING 10 In my main function,

  • 0

I have one C++ define statement as:

#define PING 10

In my main function, I have something like:

int main()
{
    int code;
    cout<<"enter command code";
    cin>>code;      // value entered 10
    cout<<code;   //here i want "PING" output instead of 10
                
    return 0;
}

How can I replace 10 with PING in my output?

Edit:

i will have multiple #define as

#define PING 10
#define STATUS 20
#define FETCH 74
#define ACK 12
#define TRAIL 9
#define EXIT 198

now my bussiness logic will get only command code i.e 10 or 12 etc etc

i want to retreive corresponding command name for that code..how is it possible??

  • 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-11T10:53:04+00:00Added an answer on June 11, 2026 at 10:53 am

    How about replacing:

    cout << code;
    

    with:

    if (code == PING)
        cout << "PING";
    else
        cout << code;
    

    That’s the easiest way if you have a single #define. For more complicated cases, you could have an array of strings to look up, based on the #define values, something like:

    #define E_OK        0
    #define E_NOMEM     1
    #define E_BADFILE   2
    #define E_USERERROR 3
    #define E_NEXT_ERR  4
    
    static const char *errStr[] = {
        "Okay",
        "No memory left",
        "Bad file descriptor",
        "User is insane",
    };
    :
    if ((errCode < 0) || (errCode >= E_NEXT_ERR))
        cout << "Unknown error: " << errCode << '\n';
    else
        cout << "Error: " << errStr[errCode] << '\n';
    

    If the values are disparate, you can opt for a non-array-based solution, such as:

    #define PING 10
    #define STATUS 20
    #define FETCH 74
    #define ACK 12
    #define TRAIL 9
    #define EXIT 198
    :
    const char *toText (int errCode) {
        if (errCode == PING  ) return "Ping";
        if (errCode == STATUS) return "Status";
        if (errCode == FETCH ) return "Fetch";
        if (errCode == ACK   ) return "Ack";
        if (errCode == TRAIL ) return "Trail";
        if (errCode == EXIT  ) return "Exit";
                               return "No idea!";
    }
    

    The other thing you may want to consider doing is replacing #define values with enumerated constants. It may not matter for simple things like this but the type safety and extra information provided will almost certainly ease your debugging efforts at some point in your career.

    Nowadays, I generally only use #define for conditional compilation. Constants are better done with enumerations and it’s been a long time since I could out-think compilers on what should and shouldn’t be an inline function 🙂

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

Sidebar

Related Questions

I have Javascript function defined in the main index.html file. One part of this
Duplicate: Should a function have only one return statement? Often times you might have
For debug logging, I have often seen and used something like #ifdef DEBUG #define
Many statically typed languages have parametric polymorphism. For example in C# one can define:
I have many cookie-cutter spring beans and don't want to explicitly define each one
I would like to have a nant task which builds code into either a
I have the following legacy code that I would like to mimic, with all
I have the following code snippet in one of my COBOL program. IF FIRST
I have a delete statement that's going against one of my core application tables.
I have several questions related curried function. Here I ask them one-by-one 1) http://twitter.github.com/scala_school/basics.html

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.