Thread: Confused: What is wrong with void??

  1. #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"

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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)

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    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

  4. #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.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    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

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    it's also good practice to type return 0; at the end of main.

  7. #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.

  8. #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.

  9. #9
    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

  10. #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.

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    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

  12. #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....

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    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

  14. #14
    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

  15. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  3. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  4. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM