This question already has answers here:
C++: Print out enum value as text
(13 answers)
Closed 8 years ago.
I have a C++ Testing.hh header file in which I have declared this ENUM -
enum TestTypeOpt {TestingOne, TestingTwo};
Now in my Testing.cc class I am trying to print out TestingOne and TestingTwo string from the enum as shown below
cout << "From command" << Testing::TestingOne << endl;
cout << "From command" << Testing::TestingTwo << endl;
But above cout prints out 0 and 1 somehow? I recently started working with C++ so little bit confuse.
Is there anything wrong I am doing? I am just trying to print actual string value from the enum class.
Someone please correct me if there is a better approach, but the way I know to do this is with macros.
Just type #define str(x) #x
This replaces x with the name as it was written in the code.
Then use for example:
cout << "From command" << str(Testing::TestingOne) << endl;
Related
This question already has answers here:
Printing variable in quotation marks C++
(4 answers)
how to declare char * with input of {"data": "0001"}'
(1 answer)
Closed 4 years ago.
I am beginning my C++ coding with challenges from the book Exercises for Programmers by Brian P.Hogan. I am capable of doing this, it's just I have never come across this int he 4 weeks I have been coding.
I am attempting to write a simple program that prompts the user for a quote, and the author of the quote.
Code:
#include <iostream>
#include <cstring>
int main(int argc, char const *argv[])
{
std::string quote;
std::string author;
std::cout << "Please enter a quote" << '\n';
std::cin >> quote;
std::cout << "Please enter the author" << '\n';
std::cin >> author;
std::cout << author << " said " << ""quote"" << '\n';
return 0;
}
Output:
compile error
With the above code, it compiles wrong. This is because of the double quotation marks
std::cout << author << " said " << ""quote"" << '\n';
The desired output will look something like this
What is the quote? These aren't the droids you're looking for.
Who said it? Obi-Wan Kenobi
Obi-Wan Kenobi says, "These aren't the droids
you're looking for."
Notice the quotation marks on the desired output around the quote (how a quote should really look anyway). I have looked online, but haven't been able to find a solution specifically for C++.
What I am asking, is how do i display text in the terminal with quotation marks around it. (Like this - "hello")
I hope you understand the question. It is my first post and I tried to make it as clear as possible what the issue is.
Thanks in advance.
escape the quote:
https://ideone.com/lcrYlA
#include <iostream>
int main()
{
// your code goes here
std::cout << " hello " << " \"world\"" << std::endl;
return 0;
}
You can of course do:
std::cout << author << " said \" "<< quote << "\"\n";
Quote the quote with \
std::cout << "the character \" is a quote";
You can escape the string using a backslash \" e.g printf("Quotes \"\" ");
This question already has answers here:
Why in the code "456"+1, output is "56" [duplicate]
(3 answers)
Closed 6 years ago.
I am doing some exercises in C++ when I came upon something not so clear for me:
cout << "String" + 1 << endl;
outputs : tring
I suggest it is something with pointer arithmetic, but does that mean that everytime I print something in quotes that is not part of previous defined array,I actually create a char array ?
A quoted string (formally a string literal) is an array of const char, regardless of whether your printing it or doing anything else with it.
Code:
cout << "String" + 1 << endl;
has the same effect as this:
const char *ptr = "String";
cout << ptr + 1 << endl;
so no you do not create a new array, you just change pointer and pass it to std::cout
This question already has answers here:
Cout long double issue
(4 answers)
Closed 6 years ago.
in C++ I'm defining the following types:
double doubleType = 1.6e-300;
long double longDoubleType = 1.6e-300;
I'll then print the values using:
cout << "double is of size " << sizeof(doubleType) << " and value is " << doubleType << endl;
cout << "long double is of size " << sizeof(longDoubleType) << " and value is " << longDoubleType << endl;
my output reads:
double is of size 8 and value is 1.6e-300
long double is of size 12 and value is -1.43863e-264
Whats causing the difference in the interpretation of the values?
It doesn't seem likely that you got this behavior with std::cout. More likely, you were using printf with the wrong format specifier, something like:
printf("%g", longDoubleType);
This is undefined behavior. It's permitted to do just about anything, but one of the common ways it plays out is that some of the bits of longDoubleType are interpreted as a double instead of a long double.
With printf, I can reproduce your output. With std::cout, I can't.
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 7 years ago.
I'm new to C++ and is trying to learn the concept of pointer. I have declared a pointer *pStart and initialised it to equal to 'text'. When I tried to print out the value of the pointer, I was expecting it to equal to the address to which 'text' was stored in, instead, the compiler printed out the string 'hello'. Could someone please explain it to me why this is happening?
char text[] = "hello";
char *pStart = text;
cout << pStart << " + " << *pStart << " + " << &pStart << endl;
This C++ feature is called operator overloading. Depending on the type of the input value certain type of streaming operator is invoked. For the character input look for "insert characters" at the link above or here.
Try this:
std::cout << std::hex << reinterpret_cast<int>(pStart);
This question already has answers here:
How can I pad an int with leading zeros when using cout << operator? [duplicate]
(7 answers)
Closed 9 years ago.
Here's the code I'm trying to change
string binary = "000000100001000100010000000100000"
bitset<32> set(binary);
cout << hex << set.to_ulong() << endl;
The code shows 2112010 but I want it to show 02112010.
std::cout << std::setfill('0') << std::setw(5) << i << std::endl;
that is the same number you can format it with the 0 by using format specifiers if you need to retain the zero you need to store it as a string,