Is there any way "" (double quotes itself) can be displayed as a string in c++
I tried cout << " "" "; which obviously does not work.
You need to escape them with '\' inside your string:
cout << " \"\" "
You need to escape your string.
cout << " \"\" ";
You need to escape your string, for example:
#include <iostream>
using namespace std;
int main()
{
cout << " \"\" ";
}
Output:
""
The two easiest ways I know of are as an escaped value in a string:
cout << "\"";
and as a character:
cout << '"';
I generally prefer the latter, as emacs's C++ mode colorizes it better. If you use the former, it gets confused and thinks everything after the third quote is inside a string. It is (debatably) less confusing for humans too.
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 \"\" ");
given:
string element = "hello";
I would like to have the following printed:
"hello" //with quotation marks
I found how to print words with quotation marks using \"hello\", but I would like to use my variable name element to print out "hello".
The closest you can get to have
string element = "hello";
std::cout << element
and have it print
"hello"
is to use std::quoted That would make the code
string element = "hello";
std::cout << std::quoted(element);
and it would output
"hello"
Do note that std::quoted does more then just add the out quotes. If your string contains quotes then it will modify those and add a \ in from of them. That means
string element = "hello \"there\" bob";
std::cout << std::quoted(element);
will print
"hello \"there\" bob"
instead of
"hello "there" bob"
use forward slash in front of quotation marks
std::cout << "\"" << element << "\"" << std::endl;
if you want to have quotation mark inside your string,
assign the variable with quotation mark with forward slash
string element = "\"hello\"";
You'll have to write the quotation marks and the variable extra, like
std::cout << '\"' << element << '\"';
BTW: in contrast to a string literal like "\"", where you have to escape double quotes, by using a character literal, you can get away without escaping: So the following is valid as well:
std::cout << '"' << element << '"';
string element = "\"hello\"";
std::cout << element;
Why does my C++ program create the strange character shown below in the pictures? The picture on the left with the black background is from the terminal. The picture on the right with the white background is from the output file. Before, it was a "\v" now it changes to some sort of astrological symbol or symbol to denote males. 0_o This makes no sense to me. What am I missing? How can I have my program output just a backslash v?
Please see my code below:
// SplitActivitiesFoo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
string s = "foo:bar-this-is-more_text#\venus \"some more text here to read.\"";
vector<string> first_part;
fstream outfile;
outfile.open("out.foobar");
for (int i = 0; i < s.size(); ++i){
cout << "s[" << i << "]: " << s[i] << endl;
outfile << s[i] << endl;
}
return 0;
}
Also, assume that I do not want to modify my string 's' in this case. I want to be able to parse each character of the string and work around the strange character somehow.This is because in the actual program the string will be read in from a file and parsed then sent to another function. I guess I could figure out a way to programmatically add backslashes...
How can I have my program output just a backslash v?
If you want a backslash, then you need to escape it: "#\\venus".
This is required because a backslash denotes that the next character should be interpreted as something special (note that you were already using this when you wanted double-quotes). So the compiler has no way of knowing you actually wanted a backslash unless you tell it.
A literal backslash character therefore has the syntax \\. This is the case in both string literals ("\\") and character literals ('\\').
Why does my C++ program create the strange character shown below in the picture?
Your string contains the \v control character (vertical tab), and the way it's displayed is dependent on your terminal and font. It looks like your terminal is using symbols from the traditional MSDOS code page.
I found an image for you here, which shows exactly that symbol for the vertical tab (vt) entry at value 11 (0x0b):
Also, assume that I do not want to modify my string 's' in this case. I want to be able to parse each character of the string and work around the strange character somehow.
Well, I just saw you add the above part to your question. Now you're in difficult territory. Because your string literal does not actually contain the character v or any backslashes. It only appears that way in code. As already said, the compiler has interpreted those characters and substituted them for you.
If you insist on printing v instead of a vertical tab for some crazy reason that is hopefully not related to an XY Problem, then you can construct a lookup-table for every character and then replace undesirables with something else:
char lookup[256];
std::iota( lookup, lookup + 256, 0 ); // Using iota from <numeric>
lookup['\v'] = 'v';
for (int i = 0; i < s.size(); ++i)
{
cout << "s[" << i << "]: " << lookup[s[i]] << endl;
outfile << lookup[s[i]] << endl;
}
Now, this won't print the backslashes. To undo the string further check out std::iscntrl. It's locale-dependent, but you could utilise it. Or just something naive like:
const char *lookup[256] = { 0 };
s['\f'] = "\\f";
s['\n'] = "\\n";
s['\r'] = "\\r";
s['\t'] = "\\t";
s['\v'] = "\\v";
s['\"'] = "\\\"";
// Maybe add other controls such as 0x0E => "\\x0e" ...
for (int i = 0; i < s.size(); ++i)
{
const char * x = lookup[s[i]];
if( x ) {
cout << "s[" << i << "]: " << x << endl;
outfile << x << endl;
} else {
cout << "s[" << i << "]: " << s[i] << endl;
outfile << s[i] << endl;
}
}
Be aware there is no way to correctly reconstruct the escaped string as it originally appeared in code, because there are multiple ways to escape characters. Including ordinary characters.
Most likely the terminal that you are using cannot decipher the vertical space code "\v", thus printing something else. On my terminal it prints:
foo:bar-this-is-more_text#
enus "some more text here to read."
To print the "\v" change or code to:
String s = "foo:bar-this-is-more_text#\\venus \"some more text here to read.\"";
What am I missing? How can I have my program output just a backslash v?
You are escaping the letter v. To print backslash and v, escape the backslash.
That is, print double backslash and a v.
\\v
I'm quite new to C++. This is what I'm disposing with at the moment and I suppose you can guess the logic I'm trying to apply to the program so that it may work. I need the user to supply an arithmetic to be performed and if it's a certain one to add the numbers together. Here is the code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x;
int v;
int sum;
string input;
cout << "Choose arithmetic: addition or subtraction? " << endl;
cin >> input;
if(input=='Addition'||input=='addition')
{
int first;
int second;
int sum = first+second;
cout << "Enter the first number: " << endl;
cout << "Enter the second number: " << endl;
cout << "The sum of these numbers is: " << sum << endl;
}
return 0;
}
The compiler gives me the following error:
13 error: no match for 'operator==' in 'input == 1953066862'
Thank you in advance!
Literal strings in C++ use double quotes, not single quotes. That is, "Addition" instead of 'Addition'.
A single-quoted string is something called a "multi-character constant" which is valid but definitely not what you want here.
In C/C++, a string should be in "" rather than ''. It should be "Addition".
Try using double quotation marks (") instead of single quotation marks (') on the following line:
if(input=='Addition'||input=='addition')
In C++, literal/constant character strings are wrapped in double quotes (e.g. "string"). Literal/constant single characters are wrapped in single quotes ('c').
As it's good programming practice, you may also want to convert your user's input to lowercase and then perform the conditional (use the function tolower()). That way you can cover all upper/lower case permutations :).
tolower function for C++ strings
u can use just an operator, check this out:
http://www.programmingtunes.com/a-simple-calculator-in-c/
you just enter your complete statement here and get your answer.
eg: 5+6
I'm working in C++. I'm given a 10 digit string (char array) that may or may not have 3 dashes in it (making it up to 13 characters). Is there a built in way with the stream to right justify it?
How would I go about printing to the stream right justified? Is there a built in function/way to do this, or do I need to pad 3 spaces into the beginning of the character array?
I'm dealing with ostream to be specific, not sure if that matters.
You need to use std::setw in conjunction with std::right.
#include <iostream>
#include <iomanip>
int main(void)
{
std::cout << std::right << std::setw(13) << "foobar" << std::endl;
return 0;
}
Yes. You can use setw() to set the width. The default justification is right-justified, and the default padding is space, so this will add spaces to the left.
stream << setw(13) << yourString
See: setw(). You'll need to include <iomanip>.
See "setw" and "right" in your favorite C++ (iostream) reference for further details:
cout << setw(13) << right << your_string;
Not a unique answer, but an additional "gotcha" that I discovered and is too long for a comment...
All the formatting stuff is only applied once to yourString. Anything additional, like << yourString2 doesn't abide by the same formatting rules. For instance if I want to right-justify two strings and pad 24 asterisks (easier to see) to the left, this doesn't work:
std::ostringstream oss;
std::string h = "hello ";
std::string t = "there";
oss << std::right << std::setw(24) << h << t;
std::cout << oss.str() << std::endl;
// this outputs
******************hello there
That will apply the correct padding to "hello " only (that's 18 asterisks, making the entire width including the trailing space 24 long), and then "there" gets tacked on at the end, making the end result longer than I wanted. Instead, I wanted
*************hello there
Not sure if there's another way (you could simply redo the formatting I'm sure), but I found it easiest to simply combine the two strings into one:
std::ostringstream oss;
std::string h = "hello ";
std::string t = "there";
// + concatenates t onto h, creating one string
oss << std::right << std::setw(24) << h + t;
std::cout << oss.str() << std::endl;
// this outputs
*************hello there
The whole output is 24 long like I wanted.
Demonstration