ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
There was a thread I'd seen a while ago about not putting the newline (the '\n') at the end of the string. According to it, the command prompt would "overwrite" the last message your program prints. Make sure you have the newline and try again.
Last edited by Dark_Helmet; 04-30-2003 at 12:40 AM.
Distribution: RedHat 9.0 / Slackware 9.0/ FreeBSD 4.8 / Solaris 8 x86 / Mandrake 9.0
I looked at the code and I didn't put the newline in, so I inserted the newline and it worked. Exactly like you said!
But this raises the problem if I don't want to put a new line in how will I get output?
for example in c++
cout >> "Enter something: ";
cin >> something; //this will be on the same line
cout >> "Here's what you entered: " + something;
if I were to insert a newline all the time it would appear:
On a different note, in C can I use
cout >> etc?
or is it only printf();
On another note, is it worth learning C for linux or should I just learn C++? (I already know how to program in C++ for windows)
Distribution: RH 6.2, Gen2, Knoppix, 98,2000 + various
If you print something to the screen, and you want it
to be there when you exit the program, then put a newline
at the end. You have complete control whether you want
a newline or not. I have a program with a counter that
keeps printing a number to the same place. if programs
automatically put in a newline whether i wanted one or
not, it would be less versatile, not more.
i think you should be getting mad at bash for having
a carriage return, but not a newline, rather than C for
making you put a newline if you want one.
when i took pascal and c classes 10 years ago, they
taught us "Hello World!\n"
Actually i can't remember the pascal commands.
I remember being frustrated with C back then too.
With a tool like C, where you have so much control,
there's so much more stuff you can mess up.
Ahhh, Pascal, the good old days I forget what you need in the uses statement for this, but:
write(<text>); <= No newline
writeln(<text>); <= With newline
Having to add a newline to the last output of a program isn't a terrible injustice. Assume the shell didn't do anything to put a newline (which, I think should be the way things are). Then your hello world program would have looked like:
Or, on my system:
Hello World![user@myhost user]$
You're mashing the last output with the prompt without the newline. I've just got an unnatural hatred for doing that... it just looks really bad to me.
About your other questions...
No, cout is strictly a C++ construct. You can't use it in standard C. However, you can use standard C within C++ programs. Perhaps it's a subtle point, but if you use any C++ constructs, your code is C++ . For standard C, all I am aware of (since I've had little reason to search for anything else) are the printf(), scanf(), fprintf(), fscanf(), and getch() functions. Those should be just about all you need
Use of cout does not automatically generate a newline at the end of the statement. You still have to manually insert them. For example:
cout << "This\n";
cout << " is an ";
cout << "example\n";
is an example
Or for your prompt:
cout << "Enter a number: ";
cin >> user_input;
cout << "This is what you entered: " << user_input
Enter a number: 6
This is what you entered: 6
The newline after the prompt is added when the user presses Enter.
The vast majority of code written for Linux will be in standard C. When you say you know C++ from Windows, be careful. From personal experience, Borland compilers offer a lot of non-standard function call to make life easier (can't remember any off-hand). Those functions won't port over to a Linux compile unless you code up a version of it yourself. I was amazed to see how dependent I was on them.
Distribution: RH 6.2, Gen2, Knoppix, 98,2000 + various
with EVERYTHING microsoft will make their own
standard, which is incompatible with the real standard,
which means it's not a standard.
they don't want their software interoperating with other
software, because that makes other software easier
to use. It is to their benefit to make things not work
together with anything except their own stuff. expect
it in advance.