I have a title that is read in from the keyboard. I need to put it in quotation marks but i dont know how to do that...for instance, my program asks the user to put in a movie title and they input the title. Then i have to display the title to the screen, but I want to put quotation marks around it. How can this be done?

Recommended Answers

All 9 Replies

I have a title that is read in from the keyboard. I need to put it in quotation marks but i dont know how to do that...for instance, my program asks the user to put in a movie title and they input the title. Then i have to display the title to the screen, but I want to put quotation marks around it. How can this be done?

To tell the program to interpret the quotes literally rather than as the end of a string, you must "escape" its special meaning with the escape/backslash symbol: \

#include <iostream>
using namespace std;

int main ()
{
    cout << "Use the escape character \\ to show quotes (\").";
    return 0;
}

Result:

Use the escape character \ to show quotes (").

whatever im doing isn't working....here's my code for that section:

cout << fixed << setprecision(2);
    cout <<endl;
    cout << "Movie Name:          "        << setw(22)  << movieTitle << endl;

this one?..

...
cout << "\"Movie Name:          "        << setw(22)  << movieTitle<<"\"" << endl;
...
cout << "Movie Name:         \" "        << setw(22)  << movieTitle <<"\""<< endl;
...

whatever im doing isn't working....here's my code for that section:

cout << fixed << setprecision(2);
    cout <<endl;
    cout << "Movie Name:          "        << setw(22)  << movieTitle << endl;

Not sure what you are trying to do with setw, but regardless, use the escape character:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main ()
{
    string movieTitle = "Jaws";
    cout << "Movie Name: \"" << movieTitle << "\"";
    return 0;
}

this one?..

...
cout << "\\Movie Name:          "        << setw(22)  << movieTitle<<"\\" << endl;
...
cout << "Movie Name:         \\ "        << setw(22)  << movieTitle <<"\\"<< endl;
...

You are escaping the escape character, not the quotes here.

commented: yep..sorry.. +1

You are escaping the escape character, not the quotes here.

yep..sorry..fixed..thank u..;)

To tell the program to interpret the quotes literally rather than as the end of a string, you must "escape" its special meaning with the escape/backslash symbol: \

#include <iostream>
using namespace std;

int main ()
{
    cout << "Use the escape character \\ to show quotes (\").";
    return 0;
}

Result:

Use the escape character \ to show quotes (").

wouldnt the "\\" after "character" make the rest a comment? or is it different because it is "\" and not "/" ?

wouldnt the "\\" after "character" make the rest a comment? or is it different because it is "\" and not "/" ?

First answer: when in doubt, try it and see! It's the best way to learn. Second answer: yes, '/' and '\' are different characters. // denotes a comment, \\ doesn't.

commented: Beat me to it! +10

// is a comment, not \\, but even so, if it were in quotes it wouldn't matter. This is ok:

std::string x = "//hello world";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.