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.,

char str[20] = “Programming is \nfun;
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.,

char str[20];
cin >> str;
cout << str;

Input:
Programming is \nfun
It is printing:
Programming is \nfun

Can any one solve this problem?
Thanks,
Amit

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

#include <iostream.h>
void main()
{
	char str[20];
	cin.getline(str, 20, '.');
	cout << str << endl;


}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.