Problem with "\n"

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 48
Reputation: amt_muk is an unknown quantity at this point 
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Problem with "\n"

 
0
  #1
Jun 7th, 2005
Hi All,
I am facing a problem with new line character (\n). When I am initializing a string variable with a string having a new line character, e.g.,
  1. char str[20] = “Programming is \nfun�;
  2. cout << str;
It is printing:
Programming is
fun

But on the other hand when I am taking the same string as an input and trying to print it, i.e.,
  1. char str[20];
  2. cin >> str;
  3. cout << str;
Input:
Programming is \nfun
It is printing:
Programming is \nfun

Can any one solve this problem?
Thanks,
Amit
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Problem with "\n"

 
0
  #2
Jun 7th, 2005
one of the way is cin.getline but you must specify the delimiter (maybe '.')

  1. #include <iostream.h>
  2. void main()
  3. {
  4. char str[20];
  5. cin.getline(str, 20, '.');
  6. cout << str << endl;
  7.  
  8.  
  9. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Problem with "\n"

 
0
  #3
Jun 7th, 2005
In source code, the sequence \n means a newline -- the compiler does the translation. In an interactive program, the sequence \n means the character \ followed by the character n. Hitting the enter key in an interactive program puts the newline character in the input stream. Some of the input functions are delimited by whitespace, and a newline is whitespace, so using them may ignore the newline.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC