0

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?

5
Contributors
9
Replies
11
Views
8 Years
Discussion Span
Last Post by Comatose
0

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 (").
0

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;
0

this one?..

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

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;
}
1

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.

Votes + Comments
yep..sorry..
0

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 "/" ?

1

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.

Votes + Comments
Beat me to it!
0

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

std::string x = "//hello world";
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.