-5

I have this code , i need output x variable as double (3.14) without changing anything in the main function

#include <iostream>

int main() {
    int x = 3.14;
    std::cout << x << "\n";
    
    return 0;
}

What should i do ?

Alexander
  • 95
  • 1
  • 6

1 Answers1

1

There are two solutions, one correct and one your professor probably wants.

  1. The correct solution (one method, there are other similar ones).
    Add this right after the #include line:

     int main() {
         double x = 3.14;
         std::cout << x << "\n";    
         return 0;
     }
     #if 0
    

    Add this at the end of the file:

     #endif
    
  2. The incorrect solution for your professor.
    Add this line before main:

     #define int double
    

    [macro.names] A translation unit shall not #define or #undef names lexically identical to keywords [...]

    • Why do I think your professor probably wants this? Because I've seen a few C++ assignments, and observed that their authors all too often disregard and abuse the C++ standard.
    • Your professor might want the first solution instead. I have no way to predict that.
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243