About return code and others

Hi folks,


This is my first test.

$ cat hello.cpp
// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!" << "\n";
cout << "I'm a C++ program" << "\n";
return 0;
}
- end -


satimis@vm0:~$ ./a.out hello.cpp
Hello World!
I'm a C++ program
satimis@vm0:~$


If I want the output as:

satimis@vm0:~$ ./a.out hello.cpp

Hello World!
I'm a C++ program

satimis@vm0:~$


A blank line above and below the printout. How shall I edit my codes? TIA


Furthermore what other value can I use on the "return" code other than 0 ?


B.R.
satimis
Changethe line cout << "I'm a C++ program" << "\n"; to cout << "I'm a C++ program" << "\n\n";

You can reutrn any value(as far as i'm aware). The return code is used to tell the operating whether the program exited successfully. Basically a return value of 0 means yes it did and any other value means no it didn't.
Use "\n" to insert a newline wherever you want. You might have copied your code from somewhere and not known what it meant, since it's your first program.

This will work:
1
2
3
4
5
6
int main ()
{
cout << "\n" << "Hello World!" << "\n";
cout << "I'm a C++ program" << "\n";
return 0;
}


You also don't have to separate things with "<<":
1
2
3
4
5
6
int main ()
{
cout << "\nHello World! \n";
cout << "I'm a C++ program \n";
return 0;
}


But that's a style choice.

Also, a more C++ way to do it is with "endl" instead of "n"
 
cout << endl << "Hello World!" << endl;


You can return whatever code you want! Usually 0 is used for success, and all other numbers indicate an error. You can check this value from your shell to see how your program did. I forget the command but you can google it.
Also, a more C++ way to do it is with "endl" instead of "n"


I don't believe saying that it is "a more C++ way to do it" with << endl instead of "\n" .

endl can cause undefined behavior as far as i know, so the better practice in this case would be to use the \newline instead of endl.
endl can cause undefined behavior as far as i know
where did you hear that? how exactly can endl cause undefined behavior? can you give an example?
Last edited on
Hi folks,


Thanks for your advice.

When shall I use other value on "return code"? TIA


Hi aroussos,

> You can check this value from your shell to see
> how your program did. I forget the command
> but you can google it.

Whether your meant "ps" command

$ ./a.out hello.cpp

Hello World!
I'm a C++ program

- end -

$ ps aux | grep hello
satimis 4180 0.0 0.0 3116 728 pts/0 S+ 03:24 0:00 grep hello


Thanks


B.R.
satimis
A belated welcome, satimis,

As part of an unwritten convention, people use other return codes than 0 for the main function when something's gone wrong. I've also known of some programs returning void, but more recently g++ will return an error if you try to have a main function that returns anything other than int...

-Albtross

Last edited on
Hi Albatross,

Your advice noted. Thanks

satimis
Topic archived. No new replies allowed.