-
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;
}
-
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
-
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;
}
-
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????
-
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 ;)
-
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!!!!!
-
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!!