why is return (EXIT_SUCCESS) bad??

Pages: 12
why is this bad?

why do people use brackets this way when they are more not newbie?

for (m=0 ; m< 1; m++) {

rather then putting the brackets down.

Why is system("pause") bad?

And do classes come first to learn or vectors? And what are vectors because my teacher told me it is just a word they use but it is not a specific topics.
not sure about the EXIT_SUCCESS question, but here's a link to read for the second question

http://www.cplusplus.com/forum/articles/11153/
there are some more questions :p
1) It's windows specific define so if someone not on windows tries to compile your code they'll have to mess with your code to fix it.

2) It's a style preference, do it however you like...within reason.

3) TheMeerkat got it.
And do classes come first to learn or vectors?
I don't really know, I would probably recommend just trying to do some projects then as you find gaps in your knowledge and your not able to continue, find out what you need to know and then go and find out how to do it. But maybe before you do that have a look at
http://www.cplusplus.com/doc/tutorial/
and make sure you at least kind of know how everything works. Like I said before, you will find out how to do things when you need to.

For some projects look at
http://www.cplusplus.com/forum/articles/12974/
http://projecteuler.net/
I'm not personally a fan of learning as you go along, because then you're likely to do something the wrong way because you never learned a better way to do it. I would read through a good book and then go to work on your own projects, but that's just me. As for number 2, it's way awesome xD. Also I put spaces after parentheses because I find it easier to read :/
man pages wrote:
The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE
The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-UNIX environments) than the use of 0 and some nonzero value like 1 or -1. In particular, VMS uses a different convention.


¿you do realize that your loop is not a loop?
Last edited on
yea i realize that :p. the last question remaining
biplav17, I do for loops that way because it makes more sense to me, and because it shortens the code considerably. I also bracket one-line loops on the same line as the conditional for the same reason.

Ex:
1
2
3
4
5
6
7
8
9
myIterator = myListObject.begin();
for(int i = 1; myIterator != myListObject.end(); ++i){ //Bracket here saves me one line.
    cout << i << ". ";
    (*myIterator)->printName();
    cin >> mySelection;
    myIterator++;
} //Bracket here helps me see the end of a subset, so the use of a line is appropriate.
myIterator = myListObject.begin();
advance(it, sel - 1); //Edited here. 
Last edited on
The bracket placement is very much a personal choice, or house style.

It's not so obvious in short code fragments, but I find spacer lines help me see the overall structure more clearly.

I would layout ciphermagi's code like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// get assorted input vaues

myIterator = myListObject.begin();

for(int i = 1; myIterator != myListObject.end(); ++i)
{
    cout << i << ". ";
    (*myIterator)->printName();

    cin >> mySelection;

    ++myIterator;
}

// walk iterator for some reason

myIterator = myListObject.begin();

for(int i = 0; i < mySelection; ++i)
{
    ++myIterator;
}


And I think it's a good idea to get into the habit of using post pre increment (in some cases it is more efficient), and to avoid un-debug-steppable code like

for(int i = 0; i < mySelection; ++i){myIterator++;}

Note I expressly forbid code like this when I have input to coding standards. Because debug breakpoints (for Visual C++'s debugger and gdb) require code to be on a distinct line.

Note : yes, I meant pre-incr, as used in the code!
Last edited on
@andy: I do compile the code on separate lines first, just to make sure I didn't miss anything, but then once I know it works, I condense it into a single line...only if the loop is, in fact, only one operation, though. I, too, use spacing to distinguish where something begins and ends, but spacing is very significant to me, as it represents the separation between declarations and definitions, or the area between discrete algorithms. In the example above, even though it was two different for loops, the purpose of the algorithm wasn't complete until the end of the second loop.

As far as walking the iterator, I use a function like this as part of a class, it walks the iterator in the class to a point where I want to perform an operation, after going through the whole list and printing to the screen some sort of representation of which item in the list they're using. Then the user can select '1' for the first option, etc, and when the function ends, the iterator (declared in the class scope, not the function scope) remains at the point where the operation is done. I do that because I use the same code, and don't want to repeat it, for locating an entry to delete, edit, print, etc.
I just stuck in a random comment to illustrate my layout style. I didn't look to closely at the content as the discussion was about style.

But wouldn't you normally use std::advance() in this kind of case?
Last edited on
That does, in fact, seem a viable solution. Would that had memorized the whole C++ library! Thanks for the suggestion. ;)

The example was edited; it seems cleaner not to re-invent the wheel.
Last edited on
I didn't read the other responses but here are my replies to the original questions:

why is this bad?


There is nothing bad about it. If anything, returning EXIT_SUCCESS would be slightly more portable. Not all platforms expect main to return 0 upon success.

why do people use brackets this way when they are more not newbie?

for (m=0 ; m< 1; m++) {

rather then putting the brackets down.


There is no correlation. Newbies and professionals alike all have preferences and they are split. The only thing that could be agreed upon is that you use whatever convention is already used in a existing module. Neither side wants to see them mixed, although the compiler certainly wouldn't mind.

Why is system("pause") bad?


In small private applications, homework assignments, etc., there is little wrong with it. The main concern is security and the concern becomes much more justified in production systems. Consider a user who acquires administrative access to the machine and replaces the "pause" command with his own program that does whatever it pleases.

And do classes come first to learn or vectors? And what are vectors because my teacher told me it is just a word they use but it is not a specific topics.


I would learn classes first but it's not that important.
And I think it's a good idea to get into the habit of using post increment (in some cases it is more efficient), and to avoid un-debug-steppable code like


You mean pre-increment:
1
2
++i; // pre-increment (preferred)
i++; // post-increment 
Yes! Thanks for the correction.

The right one was being used in the code, but not the comment. Prev. post now corrected.
Last edited on
Any conforming C++ implementation will take zero for a successful termination code out of main().
alright thanks you people
Last edited on
I have to disagree on adding blank lines to add structure. I'm not much of a white space fan, and I find it harder to read, as well as just looking bad. Anyhow, it's all personal preference.
Any conforming C++ implementation will take zero for a successful termination code out of main().


lol I dont understand professional explanations. I am a begineer.
Pages: 12