[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Special character variables in C++

Posted on 2011-03-04
10
Medium Priority
?
573 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
[X]
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
10 Comments
 
LVL 7

Assisted Solution

by:JimBeveridge
JimBeveridge earned 400 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 1600 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
Industry Leaders: 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

by:Deepu Abraham
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:Deepu Abraham
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

Industry Leaders: 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!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

649 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