Solved

Special character variables in C++

Posted on 2011-03-04
10
558 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
Comment Utility
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
Comment Utility
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)
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
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
Comment Utility
Thank you so much for your help! The code is now working perfectly.
0
 
LVL 7

Expert Comment

by:JimBeveridge
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

763 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

6 Experts available now in Live!

Get 1:1 Help Now