Jump to content

help with smallest number, using a passing function. 1 error!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Here is what i have for this program right now, i have 1 compiling error, but im still not sure if it will run.

#include <iostream> 

using namespace std;


int smallest();


int main()

{

	int num1;

	int num2;


	cout << "Please enter 2 numbers to see which is smaller." << endl;

	cin >> num1 >> num2;


	cout << "The smaller number is " << smallest() << endl;



	return 0;


}


int smallest();

{

	int num1;

	int num2;


	if(num1 < num2); 

	return num1;

	else 

		return num2;


}


question: do i need to declare those int num's again under the (int function) or is it only in int main?


this is the error its giving me
error C2447: '{' : missing function header (old-style formal list?)

its highlighing the { just below the int smallest();

there must be some kind of noob mistake on my part :(
thanks for any help
i appreciate it

#2
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Hi there Hawk1,

Your "smallest()" function does not include any arguments, therefore cannot compute anything! Furthermore, when you call smallest() from main(), you need to pass your two integers to smallest().

... //Some code here.

cout << "The smaller number is " << smallest(num1, num2) << endl;

... //Some code here.

int smallest(int a, int b)
{
    int min = a < b ? a : b;
    return min;
}

I've included the shorthand version of an "if-else" assignment, something for you to look up if you wish (your function would work fine as well, mine is simply a refactored version of yours for learning purposes). It's very simple, and very clean. Cheers!

#3
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
int smallest(); remove the ";" from this line thats whats causing the error
since u read in the values for num1 and num2 two in main u need to pass a copy of the values to smallest eg int smallest(int num1,int num2)//u can read up on passing variables to function.

#4
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Whoops, sorry, didn't even see that extra ";". You will need to remove that as well, as solartic conveniently pointed out.

Good luck.

#5
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
ok, so ill work through the changes, and i should have something like this right?
#include <iostream> 

using namespace std;


int smallest(int num1, int num2)


int main()

{

	int num1;

	int num2;


	cout << "Please enter 2 numbers to see which is smaller." << endl;

	cin >>num1 >> num2;


	cout << "The smaller number is " << smallest(int num1, int num2) << endl;



	return 0;


}





int smallest(int num1, int num2)

{

    int min = num1 < num2 ? num1 : num2;

    return min;

}

i dont have c++ on this computer so i dont know if there will be problems or not yet.

thanks for the help so far

#6
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
You're almost there, there is one error, which I will show in comments, and there are some spacing issues too (for readability):

#include <iostream> 
using namespace std;

int smallest(int num1, int num2)

int main()
{
	int num1;
	int num2;

	cout << "Please enter 2 numbers to see which is smaller: " << endl;
	cin >>num1 >> num2;

        // Your error was on the following line, calling smallest():
	cout << "\nThe smaller number is: " << smallest(num1, num2) << ".\n" << endl;

	return 0;
}

int smallest(int num1, int num2)
{
    int min = num1 < num2 ? num1 : num2;
    return min;
}

As you can see, you never need to initialize the same variable twice in a given function. You should do some research on the basics of C and/or C++ (they're very closely related), and I'm sure you'll catch on to the basics quickly.

Cheers.