C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-12-2003, 08:05 PM   #1
Registered User
 
Machewy's Avatar
 
Join Date: Apr 2003
Posts: 42
Unhappy Confused: What is wrong with void??

Ok,

I have been confused for a while on this:

What is wrong with using void?
Why is wrong to use 'void main()'?
Is it okay to use 'void' for your functions?
For example:




PHP Code:
// Tempature converstions

#include <iostream.h>

void to_celsius(float f);
void to_fahrehiet(float c);

int main()
{
 
char quit;
 
char selection[100];
 
float temp;    
 
quit '/0';
 
cout<< " _________________________________________________ "<<endl;
 
cout<< " Welcome to 'Calculations For Lazies' (Version 1.0) "<<endl;
 
cout<< " ------------------------------------------------- "<<endl;
 
cout<< " ------------------------------------------------- "<<endl;
 
cout<<endl;
 
cout<< " Celsius/Farhenheit converter:"<<endl;
 
cout<< " ______________________________"<<endl;
 while (
quit != 'q')
 {
  
cout << "1- Fahrenhiet to Celsius "<<endl;
  
cout << "2- Celsius to Fahrenhiet "<<endl;
  
cout << "3- To quit "<<endl;
  
cout << '\n';
  
cout << "Please select an option number: ";
  
cin >> selection;
  
  switch (
selection100)
  {
   case 
1:
    
cout <<"Enter the Fahrenhiet temperature you would like to convert: ";
    
cin >> temp;
    
to_celsius(temp);
    break;
   case 
2:
    
cout <<"Enter the Celsius temperature you would like to convert: ";
    
cin >> temp;
    
to_fahrehiet(temp);
    break;
   case 
3:
    
quit 'q';
   default:
    
cout << '\n';
    
cout << '\n';
    break;
  }
 }
 return 
0;
}

void to_celsius(float f)
{
 
float conversion;
 
conversion = (32)/1.8;
 
cout <<f<<" Degrees Fahrenhiet = "<<conversion<<" Degrees Celsius."<<endl;
 
cout << '\n';
}

void to_fahrehiet(float c)
{
 
float conversion;
 
conversion 1.8 32;
 
cout <<c<<" Degrees Celsius = "<<conversion<<" Degrees Fahrenhiet."<<endl;
 
cout << '\n';

Is it okay to do what I just did?
__________________
"All things come to an end"
Machewy is offline   Reply With Quote
Old 04-12-2003, 08:10 PM   #2
Registered User
 
The Dog's Avatar
 
Join Date: May 2002
Location: Cape Town
Posts: 777
It's OK for your functions, but not for main(). main() returns an int, so you have to explicitly state that it returns an int. ISO requires main() to return an int.(if i'm not mistaken)
The Dog is offline   Reply With Quote
Old 04-12-2003, 08:27 PM   #3
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
Posts: 2,718
An explanation from Bjarne Stroustrup himself. (about void main( ) )

Yes, void is okay for functions, but it is better to return an int just so you can be sure the function didn't encounter any errors.
__________________
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
XSquared is offline   Reply With Quote
Old 04-12-2003, 08:41 PM   #4
The Pantless Man
 
CheesyMoo's Avatar
 
Join Date: Jan 2003
Posts: 262
So does Bjourne mean that you don't have to return a number after main()?

Look at this:
Code:
In C++ main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. For example:

	#include<iostream>

	int main()
	{
		std::cout << "This program returns the integer value 0\n";
	}
So... that goes against alot of things I've heard.
__________________
If you ever need a hug, just ask.
CheesyMoo is offline   Reply With Quote
Old 04-12-2003, 08:44 PM   #5
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
Posts: 2,718
Yep. That's what he means. You don't have to write return 0 at the end of main(), although most compilers will spit out an error if you don't.
__________________
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
XSquared is offline   Reply With Quote
Old 04-12-2003, 08:46 PM   #6
Registered User
 
Join Date: Nov 2002
Posts: 1,109
it's also good practice to type return 0; at the end of main.
alpha is offline   Reply With Quote
Old 04-14-2003, 11:39 AM   #7
Bios Raider
 
biosninja's Avatar
 
Join Date: Jul 2002
Location: South Africa
Posts: 765
Maybe you could ask Prelude?
__________________
The knack of flying is learning to throw yourself at the ground and miss.
biosninja is offline   Reply With Quote
Old 04-14-2003, 09:06 PM   #8
Registered User
 
Join Date: Aug 2002
Posts: 87
Just to put it simple it is best to return an int from main to tell the Operating System wherter or not the program ended succesfully or with some kinda error:

return 0; // To OS - No Problem
return 1; // To OS - Problem

this can be useful for the OS in some ways, havent quite figured out how though .

In the case of user written functions it is OK to return void but it is better to return either bool or int to indicate succes or failure of the function. This is mostly to be able to do good error-checking. It is understandeble that for some functions error-checking does is hardly unlikely to occur but when consider that you must assume the user of your program is the biggest idiot on the face of this earth as well a Murphy's Law (evrything that can go wrong, will go wrong) it is best to include as much as possible error-checking.
DirX is offline   Reply With Quote
Old 04-14-2003, 10:32 PM   #9
Xei
Registered User
 
Xei's Avatar
 
Join Date: May 2002
Posts: 719
I use void main() to test things in the console. Not for anything else.
__________________
"What are you after - the vague post of the week award?" - Salem
IPv6 Ready.
Travel the world, meet interesting people...kill them.
Trying to fix or change something, only guaruntees and perpetuates its existence.
I don't know about angels, but it is fear that gives men wings.
The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

E-Mail Xei
Xei is offline   Reply With Quote
Old 04-14-2003, 10:45 PM   #10
Registered User
 
Join Date: Aug 2002
Posts: 87
using void main() 'should' not give any problems with current compilers. but it is not standard and is not good pratice. For small programs it should not be to terrible but once you get into big programs and it crashes for some reason (not enough memory etc) it is good to let the OS know something is wrong.
DirX is offline   Reply With Quote
Old 04-14-2003, 10:47 PM   #11
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
Posts: 2,718
>>it is good to let the OS know something is wrong.
Of course, if you're programming for a Windows console, its usually Windows that caused the error. And besides, Windows has enough errors to deal with on its own.
__________________
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
XSquared is offline   Reply With Quote
Old 04-14-2003, 10:50 PM   #12
Registered User
 
Join Date: Aug 2002
Posts: 87
.... WHy are people always so negative about Windows =] If you think about it, it is a great OS and WinXP and Win2k have minimized the amount of crashes etc greatly....
DirX is offline   Reply With Quote
Old 04-14-2003, 10:59 PM   #13
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
Posts: 2,718
Actually, in 4 months of running XP Pro, I've gotten more BSODs than in 2 years of 98SE. :P
__________________
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
XSquared is offline   Reply With Quote
Old 04-14-2003, 11:31 PM   #14
Xei
Registered User
 
Xei's Avatar
 
Join Date: May 2002
Posts: 719
Wierd. Windows just handles Exceptions differently. A new practice I am learning, Structured Exception Handling, will allow me to control such crashes from my application, and hopefully recover from the error. Many of you likely already use it But I was very happy when I learnt about it, I still am! It's wonderful!
__________________
"What are you after - the vague post of the week award?" - Salem
IPv6 Ready.
Travel the world, meet interesting people...kill them.
Trying to fix or change something, only guaruntees and perpetuates its existence.
I don't know about angels, but it is fear that gives men wings.
The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

E-Mail Xei
Xei is offline   Reply With Quote
Old 04-15-2003, 04:13 AM   #15
Grammar Police
 
HybridM's Avatar
 
Join Date: Jan 2003
Posts: 355
BSODs

You'll probably find that all your blue screens are either yours, or your hardware's fault. XP is just as, if not more stable than 2k, and is CERTAINLY more stable than 9x.

The problem with void main() is that so many tuts etc. still use it! I'm still learning C++, and the first mains i encountered were all integers, so i never questioned it. In fact this site's tutorials were my first brush with c++, unfortunately that included using c-style headers for a while.

Last edited by HybridM; 04-15-2003 at 04:16 AM.
HybridM is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting a swf file in a windows application face_master Windows Programming 12 05-03-2009 11:29 AM
My Rpg system(Text) kevinawad C++ Programming 56 06-25-2008 07:04 PM
i cannot see the mouse arrow peachchentao C Programming 6 12-10-2006 04:14 AM
need help with handelling multiple source files DarkMortar C++ Programming 38 05-26-2006 10:46 PM
oh me oh my hash maps up the wazoo DarkDays C++ Programming 5 11-30-2001 12:54 PM


All times are GMT -6. The time now is 06:27 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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