confusing code

This is a discussion on confusing code within the C++ Programming forums, part of the General Programming Boards category; Ok, i have this problem where i have to return the largest and smallest of 3 numbers using a funcion ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    15

    confusing code

    Ok, i have this problem where i have to return the largest and smallest of 3 numbers using a funcion with 5 parameters. Here is what I got so far. It doesn't compile right for some reason, where is my mistake? Thanks in advance for any help.

    Code:
    #include <iostream.h>
    void maxmin(int a, int b, int c, int&max, int&min)
         ma=a;
         mi=b;
      return;
    }
    void main()
    {
        int max, min;
    maxmin(10,5,6,max,min);
    cout<<max<<min;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: confusing code

    Originally posted by brutal
    Ok, i have this problem where i have to return the largest and smallest of 3 numbers using a funcion with 5 parameters. Here is what I got so far. It doesn't compile right for some reason, where is my mistake? Thanks in advance for any help.

    Code:
    #include <iostream.h>
    void maxmin(int a, int b, int c, int&max, int&min)
         ma=a;
         mi=b;
      return;
    }
    void main()
    {
        int max, min;
    maxmin(10,5,6,max,min);
    cout<<max<<min;
    }
    In your function, ma and mi are undeclared identifiers. Also you do not have an open brace at the beginning of the function. Try re-writing like this. Also never use void main!
    Code:
    #include <iostream.h>
    
    void maxmin( int a, int b, int c, int &max, int &min )
    {
      max = a;
      min = b;
    }
    
    int main( void )
    {
      int max, min;
      maxmin(  10, 5, 6, max, min );
      cout << max << min;
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    In your function, ma and mi are undeclared identifiers. Also you do not have an open brace at the beginning of the function. Try re-writing like this. Also never use void main!
    hehe a reason to why u shouldn't use it would be a nice thing to say instead of saying don't do this don't do that etc.
    Scorpion-ice
    Imperium et Respectus

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    int main( ) returns a value of the programmers definiton to the OS. The action of void main( ) is undefined, undefined == bad
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >hehe a reason to why u shouldn't use it would be a nice thing to say instead of saying don't do this don't do that etc.
    Since it's wrong there's little need for a reason until later on. But since you asked, main has a special tag, that tag declares it as returning int, if you do not match the tag with your definition then the program may not even compile. This may work on one system but break on another, not a good way to write code.

    Also, if you declare main as void it will pass an undefined value to the calling function (usually the operating system) which may then be used for something. As we all (should!) know, using an undefined value is very bad, so if you use void main you shouldn't be surprised if anything happens. By anything I mean that your computer could work as normal, it could crash, wipe your hard drive, spit floppy disks at you, send nasty email to your boss, or sing Tony Bennett's greatest hits.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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