Thread: help ASAP w/program

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    help ASAP w/program

    Need program to output largest number of two. The main function must remain as is. I believe the getinput function is where the problem is. I can get program to run when I get the input from within the main function, but I can't get it to run with a separate getinput function (which unfortunately is how I am required to write it). Please help!! here is the program:

    #include <iostream.h>

    int getinput (int num1, int num2)
    {
    cout << "Enter two integers: ";
    cin >> num1 >> num2;
    return 0;
    }

    int getmax (int num1, int num2)
    {
    if (num1 > num2)
    return num1;
    else
    return num2;
    }

    int main ()
    {
    int num1, num2;
    getinput (num1, num2);
    cout << "The bigger number is: " << getmax(num1, num2) <<endl;

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    first: You need function definitions.

    example: int getinput(int num1, int num2); < -- notice the ";"

    these should be at the beginning before main, or before you use the function.

    ----------------------------
    second, i'd move the 2 function declarations you have to after the main function
    Last edited by _Will333_; 02-25-2002 at 08:19 PM.

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "


    - Bertrand Russell


  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    new code:
    Code:
    #include <iostream.h> 
    // using namespace std;
    
    void getinput (int& num1, int& num2);
    // pre & post: comments
    
    int getmax (int num1, int num2);
    // pre & post: comments
    
    int main () 
    { 
    int num1 = 0, num2 = 0; 
    getinput (num1, num2); 
    cout << "The bigger number is: " << getmax(num1, num2) <<endl; 
    
    return 0; 
    }
    
    void getinput(int& num1, int& num2) 
    { 
    cout << "Enter two integers: "; 
    cin >> num1 >> num2; 
    } 
    
    int getmax(int num1, int num2) 
    { 
    if (num1 > num2) 
    return num1; 
    else 
    return num2; 
    }
    Last edited by _Will333_; 02-25-2002 at 08:23 PM.

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "


    - Bertrand Russell


  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    still getting junk

    Thanks, tried this (many times). My output is -858993460
    I can't seem to figure this out. I used visual c++ and borland and get junk on both. Problem seems to be in getinput. Any other ideas????

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    ok try that one

    for your first getinput function the return command can only give back 1 variable. This means the 2 inputs you have cannot be put back to main (they're local to function) using a return method. You have to use a void function and the call-by-reference parameters so that the variables are changed within the main function as well.

    Sorry, haven't slept much, hope that makes remote sense

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "


    - Bertrand Russell


  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    void function

    I have trouble with void functions. I did try this, but didn't work. Could you give me an example I know you're tired, I appreciate this!!!!!

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    Got it!!

    I Got it, I Got it!!! Yeah. Thanks so much. Now I can go to bed myself. Here's what I did with getinput:

    void getinput (int &num1, int &num2)
    {

    cout << "Enter two integers: ";
    cin >> num1 >> num2;

    return;
    }

    This programming class is going to get the best of me. Goodnight!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help ASAP ,
    By Haytham in forum C Programming
    Replies: 3
    Last Post: 05-14-2007, 10:21 AM
  2. Need Help ASAP....
    By Maxwell in forum C++ Programming
    Replies: 16
    Last Post: 09-14-2005, 06:56 PM
  3. Count_nums (need help ASAP!)
    By legacye in forum C Programming
    Replies: 6
    Last Post: 11-22-2003, 06:32 PM
  4. Help Needed Asap With Searching An Array!
    By HelpMe++ in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2003, 04:44 AM