GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Nov-2004, 16:09
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road

Function that returns smallest number


I have to write a function that returns the smallest of two numbers provided as input. Below is what I have so far, but it won't compile. Any help would be appriciated.
Attached Files
File Type: txt Homework_4-1.txt (942 Bytes, 32 views)
  #2  
Old 02-Nov-2004, 18:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Tori
I have to write a function that returns the smallest of two numbers provided as input. Below is what I have so far, but it won't compile. Any help would be appriciated.

I would rather have you ask specific questions (that is, show us the error messages you got when trying to compile). It sometimes helps for you to tell us what Operating System and compiler you are using.

However....

When I compiled your program with Borland bcc32, I got an error message saying:

Quote:
Error E2141 zz.cpp 19: Declaration syntax error

With Microsoft Visual C++ I got a couple of errors, including

Quote:
zz.cpp(19) : error C2146: syntax error : missing ';' before identifier 'main'

Other compilers might give other messages, but all would probably point to line 19. Some, like Borland's may not be too helpful if that's the first time you have seen such a thing.

Here's line 19:

CPP / C++ / C Code:
int main()

Now, there's nothing wrong with this line, so now what? Well look at lines before line 19.

In particular, the previous line is blank, and the one before that is

CPP / C++ / C Code:
void one(int,int)

This is your function prototype, and is missing a semicolon at the end.

Put in the semicolon and try again.

If you get new error messages from the compiler, try to see what it's trying to tell you. If you don't understand them, then post them here (with your current program source), and maybe someone can help.

Regards,

Dave
  #3  
Old 04-Nov-2004, 15:31
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road

It won't return the smallest number!


This is the code I wrote to return the smallest number. I thought that even if the function is void, it would still print the smallest number. How do I get it to print the smallest of two numbers?

CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void one(int,int);

int main()
{
	int x;	// one integer entered
	int y;	// one integer entered	

	cout << "Enter an integer\n";
	cin >> x;
	
	cout << "Enter another integer\n";
	cin >> y;

	one(x,y);
	
	return 0;    // indicate the program ended successfully
}

void one(int x, int y)
{
	
	if(x < y)	// test for x being less than y	
		cout << "The smallest number is:" << x << endl;

	if (y < x) 	// test for y being less than x
		cout << "The smallest number is:" << y << endl;
}
Last edited by LuciWiz : 25-Mar-2006 at 18:53. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #4  
Old 04-Nov-2004, 15:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Tori
This is the code I wrote to return the smallest number. I thought that even if the function is void, it would still print the smallest number. How do I get it to print the smallest of two numbers?

What happens when you run the program? What inputs have you used to test.

It works for me if I enter 1 2 or -100 100 or lots of other things. What does it do for you?

Does is ask for your inputs? If not, maybe your system works differently. (By the way, what Operating System, what Compiler are you using?)

I have heard that some systems won't print out anything when you use "\n" with cout. If your program doesn't ask for user inputs, you might try endl instead. Something like.

CPP / C++ / C Code:
cout << "Enter an integer: " << endl;

Regards,

Dave
  #5  
Old 04-Nov-2004, 16:52
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road

Thanks


Thank you very much. I added the endl and it worked. I'm not sure exactly why, but it does work.

Thanks again,
Tori
  #6  
Old 04-Nov-2004, 16:59
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road
Question

One more quick question


Okay I put my function into a program. In that program you enter the number of integers you wish to enter, then those integers are compaired to see which is the smallest. When I run the program, it will only do even numbers because of how my function is step up. How do I get it to do odd numbers? (by the way, I am using the compiler in PuTTY)

CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void one(int,int);

int main()
{
	int z; // total number of integer
	int x;	// one integer entered
	int y;	// one integer entered
	int i;	//for control statement in the for loop
	
	cout << "Enter total number of integers\n";
	cin >> z;

	for (i=0;i<z;i++)	//you must not put a semicolon after this for loop
{
	cout << "Enter an integer: " << endl;
	cin >> x;
	
	cout << "Enter an integer: " << endl;
	cin >> y;

	one(x,y);
}	

	return 0;    // indicate the program ended successfully
}

void one(int x, int y)
{
	
	if(x < y)	// test for x being less than y	
		cout << "The smallest number is:" << x << endl;

	if (y < x) 	// test for y being less than x
		cout << "The smallest number is:" << y << endl;
}
Last edited by LuciWiz : 25-Mar-2006 at 18:55. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #7  
Old 04-Nov-2004, 17:29
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Tori
Okay I put my function into a program. In that program you enter the number of integers you wish to enter, then those integers are compaired to see which is the smallest. When I run the program, it will only do even numbers because of how my function is step up. How do I get it to do odd numbers? (by the way, I am using the compiler in PuTTY)

Your function compares two integers to see which is smaller. The program asks for integers, two at a time and calls the function to report which is smaller. (By the way, what if the two are equal? Your program doesn't give any output --- is this OK?)


Now, if you want to ask the user for a number of integers and then report which is the smallest, one way do it is with a loop, so the program looks something like:

Code:
declare an int variable, say "smallest" (this will keep track of the smallest user input) Ask the user how many integers to use. Suppose the user enters 5, so you are going to get a total of five integers. Get the first user input. and assign its value to "smallest" Go through this loop four times: (to get the remaining four user inputs) get the next integer. if that integer less than "smallest", set "smallest" to that value Now, when the program exits the loop, the variable "smallest" contains the value of the smallest integer that the user has entered. Print it out.

When you post code, it is more readable if you use code tags.
Look here: http://www.gidforums.com/t-689.html

(That's the link at the top of the page).

Regards,

Dave
  #8  
Old 04-Nov-2004, 23:02
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by davekw7x
When you post code, it is more readable if you use code tags.
Look here: http://www.gidforums.com/t-689.html

(That's the link at the top of the page).

Regards,

Dave
Also, code tags are only useful if your code is formatted in the first place. So read this tutorial too.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #9  
Old 05-Nov-2004, 12:35
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road
Question

Won't compile


Okay, I tried putting 'smallest' in as an int, but it won't compile. It says:

Quote:
Homework_4-10.cpp: In function `void one(int)':
Homework_4-10.cpp:42: error: `smallest' undeclared (first use this function)
Homework_4-10.cpp:42: error: (Each undeclared identifier is reported only once
for each function it appears in.)
Homework_4-10.cpp:43: error: syntax error before `if'

I am not really sure what is wrong. I did declare 'smallest' so I am not sure why it says 'undeclared'. Do I need to declared 'smallest' inside the 'one' function? Here is the code.

CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void one(int);

int main()
{
	int z; // total number of integer
	int x;	// one integer entered
	int i;	//for control statement in the for loop
	int smallest;	// store the smallest variable
	
	cout << "Enter total number of integers\n";
	cin >> z;

	for (i=0;i<z;i++)	//for loop
{
	cout << "Enter an integer: " << endl;
	cin >> x;
	
	one(x);
}	
	return 0;    // indicate the program ended successfully
}

void one(int x)
{
	smallest = 1000000
	if(x < smallest)	// test for x being less than smallest	
		cin >> smallest = x
}
  #10  
Old 05-Nov-2004, 13:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Tori
Okay, I tried putting 'smallest' in as an int, but it won't compile. It says:



I am not really sure what is wrong. I did declare 'smallest' so I am not sure why it says 'undeclared'. Do I need to declared 'smallest' inside the 'one' function? Here is the code.


What are you trying to accomplish with your function one()?

I think that I have lost track of what you are supposed to be doing. In your first post you said:

Quote:
Originally Posted by Tori
I have to write a function that returns the smallest of two numbers provided as input


Well, I don't see anything that looks like that.

It could look something like:

CPP / C++ / C Code:
int find_smaller(int x, int y)
{
  int small;
  if (x <= y) {
   small = x;
  }
  else {
    small = y;
  }
  return small;
}

Now, I'm not saying that you should use this, but it does meet the requirements of your first post.

Now you can use this function in a loop that compares input numbers to get the smallest value of all of the user inputs.

Do you really need a function to find the smaller of two integers? If it was part of your assignment, this is a way to start.

If you aren't specifically required to implement a function that returns the smaller of two integers, then you can merely write code that does the stuff indicated in my previous post. "Normally" functions are used to encapsulate a group of instructions that performs a particular task. When the task is simple enough to be executed with a simple C expression (deciding whether one number is less than another), it usually doesn't clarify things much to make a separate function. If the point is to teach you about writing and using functions, the above example may get you started.

Regards,

Dave

p.s. Thanks for using the code tags. I am constantly amazed by people who repeatedly ignore our requests for doing this. It makes the world a little prettier, don't you think?
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: (function) undeclared -first use of this function crystalattice C++ Forum 6 01-Nov-2004 05:36
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 21:25
PHP function to determine whether a number is odd or even? da_bomb50 MySQL / PHP Forum 9 10-Aug-2004 10:53
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 05:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:49.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.