Special character variables in C++

Posted on 2011-03-04
Last Modified: 2012-06-21
I have been trying to figure out how to correctly handle character variables " and ' in c++.
I know where the problem is in my code, it is in the line
cout<<"Enter your height in feet and inches (F' II" ") :  ";
The program now (after trial and errors) compiles and runs but I would like the user to be represented with the line
Enter your height in feet and inches (F' II"):
My current code displays the line as
Enter your height in feet and inches (F' II):
I have been spending a lot of time with this simple program, and am not able to get it correct. Can anyone help me?
Thank you.
#include <iostream>

using namespace std;

int main()
int inches, feet;

char feetCh;
char InchesCh;

cout<<"Enter your height in feet and inches (F' II" ") :  ";
cin>>feet>>feetCh>>inches>>InchesCh  ;

cout<< "Your height is " <<feet<< " ft " << inches << " inches " <<endl;

return 0;

Open in new window

Question by:Mathilda10
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2

Assisted Solution

JimBeveridge earned 100 total points
ID: 35039315
Put a backslash before the double quote. For example:

cout<<"Enter your height in feet and inches (F' II\" ") :  ";

Make sure you use backslash, not forward slash.
LVL 86

Accepted Solution

jkr earned 400 total points
ID: 35039320
You need to escape the quote characters with a backslash, i.e.
cout<<"Enter your height in feet and inches (F\' II\" ") :  ";

Open in new window

See also ("C++ Character Constants") and there the table "C++ Reserved or Nongraphic Characters" in particular.

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 35039397
You can use std::string to get the ft and inch and then can easily disply it. also if you want to get ft and inch individually, it's become easy using std::string.
Don't forget to use \'' where needed.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Expert Comment

ID: 35041608
An escape sequence begins with a \ (backslash) followed by an alphanumeric character

\a /*alert (bell)*/
\b /*backspace*/
\f /*formfeed*/
\n /*newline*/
\r /*carriage return*/
\t /*horizontal tab*/
\v /*vertical tab*/
\\ /*backslash*/
\? /*question mark*/
\' /*single quote*/
\" /*double quote*/
\000 /*octal number*/
\xhh /*hexadecimal number*/

cout << "\nThis is\t a string\n\t\t with \"many\" escape sequences!\n";
LVL 86

Expert Comment

ID: 35041639
DeepuAbrahamK, would you mind reading previous post to find out what otheres have already contributed before you do so? Thank you doing that in the future. Also, it does not really help if someone jumps on the bandwagon when seeing a word like 'escaping' and then contributes a bunch of stuff that was neither requested in the original question nor needed clarification.
LVL 11

Expert Comment

ID: 35041893
I am sorry about that the page was not refreshed for a while. Looks like I am facing some problem with my internet connection. My apologies.

Author Closing Comment

ID: 35042053
Thank you so much for your help! The code is now working perfectly.

Expert Comment

ID: 35044334
I am not certain why you gave me such a small number of points. My solution was first and it was correct. jkr's answer is wrong. It's not necessary to escape single quotes in a string surrounded with double quotes. It doesn't hurt to do so, but it's wrong to say that you "need to."

Author Comment

ID: 35045748
Hi JimBeveridge,
I am truly sorry for giving you only 100 points, I just now tested your code, and it works also. Like you said, it is not necesssary to escape single quotes. I don't know how I can change the number of points. Any advice?

Expert Comment

ID: 35047185
I don't believe that you can change the points on this question. The simplest solution is to click on "Ask a related question", title the new question something like "For Jim", then I'll answer it and you can accept it.

Here is the rule for escaping characters. For string constants, such as "Hello", you need to escape any double quotes, as I showed earlier. For single-character constants, such 'A', then you need to escape single quotes. For example, '\''.

The other escape characters, such as \n and \r, work a little differently. When you do \n, you don't get the letter n, you get a newline character.

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question