Solved

Special character variables in C++

Posted on 2011-03-04
10
559 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 8

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now