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

Dani AI

Generated

Short diagnosis tied to the thread: the compiler interprets escape sequences inside source-code string literals, while interactive keyboard input containing a backslash followed by n is just two characters. already pointed this out. For the formal list of recognized escape sequences see the C++ reference on escapes: C++ escape sequences.

Practical approaches, depending on the goal:

  • To read a whole line (including spaces) prefer std::getline instead of operator>>, as suggested.
  • To treat a typed \n (two characters) as a real newline, post-process the string and replace the two-character sequence with the actual '\n' character. The example below shows a compact unescape routine that converts common sequences (\n, \t, \\) while preserving others.
#include <iostream>
#include <string>

std::string unescape(const std::string &s) {
    std::string out;
    out.reserve(s.size());
    for (size_t i = 0; i < s.size(); ++i) {
        if (s[i] == '\\' && i + 1 < s.size()) {
            char c = s[++i];
            if (c == 'n') out.push_back('\n');
            else if (c == 't') out.push_back('\t');
            else if (c == '\\') out.push_back('\\');
            else { out.push_back('\\'); out.push_back(c); }
        } else out.push_back(s[i]);
    }
    return out;
}

int main() {
    std::string line;
    std::getline(std::cin, line);
    std::cout << unescape(line);
}

Extra notes: prefer std::string over fixed char arrays to avoid overruns. If users must enter multiple real lines interactively, choose a sentinel line or read until EOF. On Windows, trim a trailing '\r' if present. If the intent is to keep the literal \n inside source code, use raw string literals or escape the backslash ("\\n").

Recommended Answers

All 2 Replies

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.