| | |
Problem with "\n"
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2005
Posts: 48
Reputation:
Solved Threads: 3
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.,
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.,
Input:
Programming is \nfun
It is printing:
Programming is \nfun
Can any one solve this problem?
Thanks,
Amit
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)
char str[20] = “Programming is \nfun�; cout << str;
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)
char str[20]; cin >> str; cout << str;
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 '.')
C++ Syntax (Toggle Plain Text)
#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.
"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
![]() |
Similar Threads
- Problem with Hotmail...login says "Page Not Found" (Web Browsers)
- google "keyword" question (Search Engine Optimization)
- Netscape 7.1, Hotmail login problem - cookies 'disabled" (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: C++ sequential files.
- Next Thread: C++ question(Decimal places)very weird
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






