944,047 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2518
  • C++ RSS
Jun 7th, 2005
0

Problem with "\n"

Expand Post »
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.,
C++ Syntax (Toggle Plain Text)
  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.,
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 14
Solved Threads: 3
Light Poster
amt_muk is offline Offline
48 posts
since May 2005
Jun 7th, 2005
0

Re: Problem with "\n"

one of the way is cin.getline but you must specify the delimiter (maybe '.')

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. void main()
  3. {
  4. char str[20];
  5. cin.getline(str, 20, '.');
  6. cout << str << endl;
  7.  
  8.  
  9. }
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Jun 7th, 2005
0

Re: Problem with "\n"

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ sequential files.
Next Thread in C++ Forum Timeline: C++ question(Decimal places)very weird





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC