| View previous topic :: View next topic |
| Author |
Message |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Sun Mar 30, 2008 3:12 pm Post subject: Question about syntax preferences |
|
|
Using int main instead of void main
saying std::cout, etc instead of using namespace std
banishing system for cin.ignore or getch
I see people recommending these all the time in this section, but I don't understand why one's better than the other. |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun Mar 30, 2008 3:23 pm Post subject: Re: Question about syntax preferences |
|
|
| HalfPrime wrote: | | Using int main instead of void main |
Only use int main() if you plan on returning a value to a parent process, otherwise you might as well use void main().
| HalfPrime wrote: | | saying std::cout, etc instead of using namespace std |
It is really the same thing, when you declare a namespace that means that every class function coming afterwards is prefixed with that namespace. It is just a coding habit, either one is fine.
| HalfPrime wrote: | | banishing system for cin.ignore or getch |
Use std::cin.ignore(), getch() requires another header. |
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Sun Mar 30, 2008 10:06 pm Post subject: |
|
|
Ah, ok. So there's no real performance difference.
| Quote: | | Use std::cin.ignore(), getch() requires another header. |
Is there anything wrong with system("PAUSE")? |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Mon Mar 31, 2008 9:44 am Post subject: |
|
|
| HalfPrime wrote: | | Using int main instead of void main | void main isn't acceptable in the C99 standard. Altho the return value of the proggy is from the time of Adam, some platforms still use it. That's why you should always return something from the main function. Also, not all the compilers do know how to handle void main.
| HalfPrime wrote: | | saying std::cout, etc instead of using namespace std | Including the whole namespace destroys one useful thing of C++. The idea is that you could have n functions with same name, as long as they're in different namespaces.
| HalfPrime wrote: | | banishing system for cin.ignore or getch. | I'd not even recommend getch, because it is deprecated and _getch isn't supported on all compilers/platforms (it isn't portable). For the system thingy:
| HalfPrime wrote: | | Is there anything wrong with system("PAUSE")? | Not everyone codes proggies for Windows. And even if you were coding a proggy for Windows, it's not nice to call external stuff >_> |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Mar 31, 2008 9:50 pm Post subject: |
|
|
Jumping on with what everyone else is saying, and completely agreeing with Jani here:
In my personal opinion, every single function you code should always return a value. No matter how big or small the function is, or what it says. There will always be a reason why that function can fail. (I know a lot of people will disagree with this, but it is my opinion.) Nothing inside a computer is perfect, and something can cause an error in the most uncommon of places. I prefer to at least use a boolean return, or a hresult, for any function I write unless it needs a different return value.
For the namespace, as Jani said, there is no reason to include the entire namespace in your project. Just wasting resources doing that. Unless you really plan to use like.. every single piece of the namespace in a project, theres no reason for you to include it. Just take the time to type out the std::
System is a resource hog. I bash it because of my own tests as well as numerous articles I have read about how it works and what it does. This is one of my favored articles on the subject:
http://www.gidnetwork.com/b-61.html
You load so much more things into the memory of your program just by calling that 1 line. You are already using the iostream, take advantage of whats already given to you to accomplish the same thing with less of a footprint.
| Code: | std::cin.ignore();
std::cin.sync(); |
The same goes for getch() in this case. It's not a resource thing, but theres no reason to add more includes then what you need when you can pause the console already with the iostream header. _________________
- Retired. |
|
| Back to top |
|
 |
|