Function problems

complete newbie to C++ in need of a kick in the right direction

Page 1 of 1

3 Replies - 406 Views - Last Post: 11 November 2010 - 03:49 AM Rate Topic: -----

#1 sion100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-November 10

Function problems

Posted 08 November 2010 - 08:09 AM

Folks,

I could do with a little help please

I have an assignment due and have had no time to study properly due to work commitments and was hoping one of you good people could point me in the right direction. Below is the code I have come up with to (or not as the case may be) to enter 3 numbers pass to a function then return the smallest number. I have the numbers entering ok and I think the sorting is good but will bow to superior knowledge on that one.
I am strugling and any help would be great
:helpsmilie:

//smallestnumber

#include<iostream>
#include<cstdlib>
using namespace std;
void smallestNumber(int,int&,int&);//declaring the function

int main()
{//To enter the numbers
	 int num1,num2,num3;
	cout<<"\n Insert a number "<<endl;
	cin>> num1;
		cout<<"\n Insert another number  "<<endl;
		cin>>num2;
		cout<<"\n Insert another number  "<<endl;
	cin>>num3;

		smallestNumber (num1,num2,num3);// calling the function
		{
	for(num1=0;num1<2;num1++);
		for(num2=num1+1;num2<3;num2++)
			if(num1>num2);
		}
	
	cout<< "\n The smallest number is "<< num1 <<endl;
	
			system("pause");
			return 0;
}//end main
//defining the function


Is This A Good Question/Topic? 0
  • +

Replies To: Function problems

#2 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Function problems

Posted 08 November 2010 - 08:25 AM

Did you even compile this code even once?

1. Your cdoe is badle messed up. You have defined your function inside the main function, which is not correct, seperate it out. Write is outside main function and the call it from within function main(). This is how you call a function with return type int.
main()
{
...//rest of the statments of your program
int x = smallestNumber(a,b,c);
....//rest of the statments of your program
}

int x is to recive the value returned by the function then you can simply print it.

2. In your function declaration what is & sign doing. You can simply pass them by value rather than by reference.
smallestNumber(int,int[s]&[/s],int[s]&[/s]);

3. You function should return something to the calling function, don't you think so. Otherwise how would the calling function know what value was the smallest number identified by your smallesNumber function. so its return type would be int rather than void.

4. Can you please explain in words what, in your opinion, these statements are doing? Your logic is completely incorrect. Work on it again.
for(num1=0;num1<2;num1++);
for(num2=num1+1;num2<3;num2++)
if(num1>num2);


5. A trivia: Never use sytem("pause"). It is considered to be a bad programming habbit. Check out my signatures to ind out why and to know the alternative for this.

Fix your code and post it here if you still have any problems.

This post has been edited by newclearner: 08 November 2010 - 08:30 AM

Was This Post Helpful? 3
  • +
  • -

#3 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Function problems

Posted 11 November 2010 - 03:37 AM

another dump and run case. ::sigh::
Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Function problems

Posted 11 November 2010 - 03:49 AM

View Postnewclearner, on 11 November 2010 - 01:07 PM, said:

another dump and run case. ::sigh::

Doesn't matter, posts are helpful for many here, not just the op :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1