How would I print quotation marks in the output in C++? [duplicate] - c++

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 \"\" ");

Related

Question about asteriks and string concatenation in C++

So I am currently going through Accelerated C++ course on udemy by Jeremy Siek and I was on that tutorial which he is mentioning string concatenation and in one part he gave task to print something (WITHOUT USING NESTED LOOPS) like this:
OUTPUT:
*
**
***
****
(- So I know about for loops from earlier and I am not completely new to C++ programming, but I am just complementing knowledge from earlier and I know that this problem can be solved with nested for loops. But THIS IS NOT MY QUESTION, KEEP READING, BECAUSE I HAVE TO MAKE INTRO INTO MY QUESTION)
Before he made program which is source code like this (WHICH I COMPLETE UNDERSTAND)
int main()
{
cout << "Please enter your name:";
string name;
cin>>name;
string greeting="Hello, " + name + "!";
string spaces(greeting.size(), ' ');
string stars(greeting.size(), '*');
cout << "**" stars << "**" << endl;
<< "* "<< spaces << " *"<< endl;
<< "* "<< greeting << " *"<< endl;
<< "* "<< spaces << " *" <<endl;
<< "**" stars << "**" << endl;
return 0;
}
Now, about my question:
I was trying to do that task he gave, and I came up with something like this:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string star="*";
int br=1;
cout<<star<<endl;
while(br<4)
{
br+=1;
string (newstar.size(br, '*') );
cout<<newstar<<endl;
}
return 0;
}
Now, that program resulted error because of string (newstar.size(br, '*') ); which I don't understand why is that wrong and why is string newstar(br, '*'); correct
without .size and without () ?
I think your intention was to have:
string newstar(br, '*');
istead of:
string (newstar.size(br, '*') );
also maybe increment br after that line, and you could remove 'cout << star << endl;'
See the fill constructor: http://www.cplusplus.com/reference/string/string/string/
The error is
prog.cc: In function 'int main()':
prog.cc:14:13: error: 'newstar' was not declared in this scope
string (newstar.size(br, '*') );
^~~~~~~
prog.cc:14:13: note: suggested alternative: 'star'
string (newstar.size(br, '*') );
^~~~~~~
star
Because you did not declare newstar. The compiler already stops there, but there is more wrong: std::string::size takes no paramters and it is not clear what you expect from writing string ( some_number);.
As I dont really understand the logic of your code I cannot offer you a complete fix, but I can give a hint. This:
std::cout << std::string(4,'*');
constructs a std::string consisting of 4 copies of the character * and prints that on the console.

How do i recall a string into a quote?

I am having trouble with recalling the name of a person that was previously entered into the program with a cout statement.
int main()
{
string donorName;
string donorGender;
int donorWeight;
int donorHeight;
int donorAge;
cout << "What is the donor's name?" << endl;
getline(cin, donorName);
if (donorAge < 16)
{
cout << "--- No, **(name)** you cannot donate blood" << endl;
}
Can you help me how I can put "No, "donor Name" you can not donate blood into the cout statement from the name they put in for donor name?
Thank you so much.
You are probably looking for string interpolation like for example in PHP or in TypeScript, where you can conveniently write:
const my_variable = 123;
const text = `some text ... ${my_variable} ...`;
Unfortunately, C++ doesn't have such a feature. There has been a proposal for it, but I don't think it ever got anywhere.
If the goal is just to print the text, then the safest and easiest way to do so is to split the literal into two parts and print them separately, along with the variable in between:
std::cout << "--- No, " << donorName << ", you cannot donate blood\n";

Having problems declaring a string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So, I started using C++ today, and wanted to make a small, text-based adventure, but that didn't turn out all that well.
Here's my code:
string answer;
string name;
string charName;
std::cout << "<Ominous voice> Hi there, what's your name?" << std::endl;
std::cout << "Enter your name:" << std::endl;
getline(cin, name);
std::cout << "<Ominous voice> " << name << "? That's an... interesting name." << std::endl;
std::cin.ignore();
std::cout << "<Ominous voice> Before we start off... You need to learn the basics, so let's break the fourth wall shall we?" << std::endl;
std::cin.ignore();
std::cout << "<Ominous voice> I'm here to guide you on this wonderfull adventure... for just $ 9.99." << std::endl;
std::cin.ignore();
std::cout << "<Ominous voice> You can name me everything you want, since I'm a fragment of your imagination anyway... So, what about it?" << std::endl;
std::cout << "Enter a name:" << std::endl;
getline(cin, charName);
strcpy (str1,"<");
strcpy (str2,">");
strcat (str1,charName, 1);
strcat (charName,str2, charName.size();
std::cout << charName << " Well then, it seems I'm now called '" << charName << "' not sure if I like that." << std::endl;
std::cin.ignore();
std::cout << "This line does not show up" << std::endl;
Now I have 2 problems:
I get these messages when I start the program:
/home/ubuntu/workspace/hello-cpp-world.cc: In function ‘int main()’:
/home/ubuntu/workspace/hello-cpp-world.cc:23:13: error: ‘str1’ was not declared in this scope
strcpy (str1,"<");
^
/home/ubuntu/workspace/hello-cpp-world.cc:24:13: error: ‘str2’ was not declared in this scope
strcpy (str2,">");
^
/home/ubuntu/workspace/hello-cpp-world.cc:26:43: error: expected ‘)’ before ‘;’ token
strcat (charName,str2, charName.size();
^
That last line, saying "This line does not show up", actually doesn't show up.
I know I'm basic and this is probably not the most efficient way to do this, but I'm a beginner.
You have to declare string str1 before using it. Decraling means
std::string str1;
In C++ the definition is e.g.
str1 = "heureka";
And the declaration has always be in front of the definition.
But you can do both in one line:
std::string str1 = "heureka";
or
std::string str1("heureka");
If you want to add "<" in front and ">" behind the characters name. You can do it as follows:
charName = "<" + charName + ">";
There is no need to create new strings for it. There is simply a + operator for strings and it is easier to use than strcpy, strcat or so.
The issue is that you haven't declared the strings str1 and str2.

howto get with scanf a string ( char * ) from a user input, delimited by double quotes and containing spaces [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am quite noob with c and c++, and I am stucked trying to read a user input delimited between double quotes for a program I have to deliver to my algorithm class.
The entry would be in this form: "something like this, with spaces, and delimited by this two double quotes".
What I need to get from that is the string ( char * ) contained between the delimiters.
Unfortunatelly I have been trying without luck to solve this small issue...
The development environment is a virtualized Windows 7 and the ide (both are requirements from the teacher) is DEVC++
Anyone could give a hint or help me out? I am stucked with this and I am running out of time.
thanks in advance!
Assuming you have a stream where the current character is a double quote, you can just
ignore() the current character.
getline() using '"' as the delimiter.
Here is code which skips leading space, verifies that the next character is a '"' and, if so, reads the value into str:
std::string str;
if ((in >> std::ws).peek() == '"' && std::getline(in.ignore(), str, '"')) {
std::cout << "received \"" << str << "\"\n";
}
If I understood the question correctly, then the following suits you. This approach will eliminate every punctuation.
#include <string>
#include <algorithm>
#include <iostream>
int main ()
{
std::string input ;
std::cout << "Please, enter data: ";
std::getline (std::cin,input);
input.erase( remove_if(input.begin(), input.end(), &ispunct), input.end());
std::cout << input << std::endl;
std::cin.get();
return 0;
}
This is the result.
>Please, enter data: There' ?are numerous issues.
There are numerous issues
This approach is exactly what you are looking for by using strtok
#include <stdio.h>
#include <iostream>
int main()
{
char sentence[] = "\"something like this, with spaces, and delimited by this two double quotes\"";
char * word;
std::cout << "Your sentence:\n " << sentence << std::endl;
word = strtok (sentence,"\"");
std::cout << "Result:\n " << word << std::endl;
return 0;
}
The result
Your sentence:
"something like this, with spaces, and delimited by this two double quotes"
Result:
something like this, with spaces, and delimited by this two double quotes

How to display "" itself as a string

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.