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
Solved

Special character variables in C++

Posted on 2011-03-04
10
561 Views
Last Modified: 2012-06-21
Hello,
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

0
Comment
Question by:Mathilda10
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 7

Assisted Solution

by:JimBeveridge
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.
0
 
LVL 86

Accepted Solution

by:
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 http://msdn.microsoft.com/en-us/library/6aw8xdf2.aspx ("C++ Character Constants") and there the table "C++ Reserved or Nongraphic Characters" in particular.
0
 
LVL 9

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.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 11

Expert Comment

by:DeepuAbrahamK
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";

http://msdn.microsoft.com/en-us/library/h21280bw%28v=vs.80%29.aspx
0
 
LVL 86

Expert Comment

by:jkr
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.
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
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.
0
 

Author Closing Comment

by:Mathilda10
ID: 35042053
Thank you so much for your help! The code is now working perfectly.
0
 
LVL 7

Expert Comment

by:JimBeveridge
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."
0
 

Author Comment

by:Mathilda10
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?
0
 
LVL 7

Expert Comment

by:JimBeveridge
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

839 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