![]() |
|
#1
|
|||
|
|||
Function that returns smallest numberI 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.
|
|||
|
#2
|
|||
|
|||
|
Quote:
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:
With Microsoft Visual C++ I got a couple of errors, including Quote:
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:
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:
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
|
|||
|
|||
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:
Last edited by LuciWiz : 25-Mar-2006 at 18:53.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#4
|
|||
|
|||
|
Quote:
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:
Regards, Dave |
|
#5
|
|||
|
|||
ThanksThank you very much. I added the endl and it worked. I'm not sure exactly why, but it does work.
Thanks again, Tori |
|
#6
|
|||
|
|||
One more quick questionOkay 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:
Last edited by LuciWiz : 25-Mar-2006 at 18:55.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#7
|
|||
|
|||
|
Quote:
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:
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
|
||||
|
||||
|
Quote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#9
|
|||
|
|||
Won't compileOkay, I tried putting 'smallest' in as an int, but it won't compile. It says:
Quote:
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:
|
|
#10
|
|||
|
|||
|
Quote:
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:
Well, I don't see anything that looks like that. It could look something like: CPP / C++ / C Code:
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 GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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