Function program

Started by
8 comments, last by jpetrie 16 years, 11 months ago

#include <iostream.h>
#include <stdlib.h>
typedef unsigned short USHORT;
USHORT function(USHORT numone, USHORT numtwo);
int main()
{
USHORT numone, numtwo,result;
cout << "Please Input Two numbers\n\n";
cout << "Number 1\n\n";
cin >> numone;
cout << "Number 2\n\n";
cin >> numtwo;
result = function(numone, numtwo);
cout << result;
      system("PAUSE");
      return 0;
}

USHORT function (USHORT one, USHORT two)
{
if (two =0)
return (two*-1);
else
return (one/two);
}

This code compiles fine but I'm getting a error when I run it and make number 2 = 0 The function of this program should be to take the two unsigned short int and return the result of dividing the first num by the 2nd num and if the second number is 0 it should return -1 Any idea whats up? Thanks will
Advertisement
The expression inside the if is an assignment, not a comparison. Use == for comparison, =< is for assignment. The result of an assignment is the value assigned; in other worse, the result of the expression two = 0 is 0, which when converted to a boolean value, is false. Thus, the else branch is always taken, and since you've assigned zero to the variable two, you divide by zero, causing all kinds of bad things.

You should learn to set breakpoints and use the debugger that came with your IDE (which IDE are you using?). It would allow you to solve this problem rather easily on your own, if you weren't able to spot it by reading over the code carefully (accidentally using a single = for comparison is a common beginner typo).

PS: Don't use system("PAUSE") to keep the window open. Choose "Start Without Debugging" (or use its keystroke, Control-F5 by default I think) to keep the window open without inserting disgusting hacks into your code.
Two problems.
In function "function" the line
if (two =0) should be if (two==0) as you want to compare two to 0.
second the compiler will probably give you a warning because you are trying to return a negative number and your function returns unsigned shorts.
to return -1 what kind of Variable would I Need to declare?
not an unsigned one.
Just a short or an int.
One that is signed. SHORT is probably a good equivalent typedef for the signed variant of USHORT in your case (short is the standard signed short integer type).

Oh, and don't #include <iostream.h> either, that's a deprecated old-style header. #include <iostream> (without the .h suffix) and #include <cstdlib> are the modern, proper forms of both of your include directives.
I'm using bloodshed just to let you know


EDIT

btw

new source

#include <iostream.h>#include <stdlib.h>int function(int numone, int numtwo);int main(){int numone, numtwo,result;cout << "Please Input Two numbers\n\n";cout << "Number 1\n\n";cin >> numone;cout << "Number 2\n\n";cin >> numtwo;result = function(numone, numtwo);if (result > 0){cout << "Number one Divded by Number 2 is " ;cout << result;}else{cout << "ERROR NUMBER 2 = 0 THATS A NONO";}cout <<"\n\n\n";      system("PAUSE");      return 0;}int function (int one, int two){if (two == 0)return -1;elsereturn (one/two);}


When I enter 10 for number 2 I get a no no

why is this ?


Will
Depends on what you enter for num1. Note that you do an integer division. Since integer cannot carry after comma digits the resulting digits will be cut off.

So 8/10 will result in 0.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

here is my new code, nothing much diff I tried floats that did not work so now im back to ints with a few different if 's and such

#include <iostream.h>#include <stdlib.h>int function(int numone, int numtwo);int main(){int numone, numtwo,result;cout << "Please Input Two numbers\n\n";cout << "Number 1\n\n";cin >> numone;cout << "Number 2\n\n";cin >> numtwo;result = function(numone, numtwo);if (result = -1){cout << "ERROR NUMBER 2 = 0 THATS A NONO";}else{cout << "Number one Divded by Number 2 is " ;cout << result;}cout <<"\n\n\n";      system("PAUSE");      return 0;}int function (int one, int two){if (two == 0)return -1;elsereturn (one/two);}



Any Help to make this program function correctly would be appreciated.

Thanks

WIll
Quote:
I'm using bloodshed just to let you know

Then I suggest you get rid of it; Dev-C++ is an all-around terrible IDE. It will cause you more trouble in the long run. Visual C++ Express is, all-around, a better option.

My comment about system("PAUSE") still applies, though. Dev-C++ has a similar option that would keep the window open (or at least, it did the last time I used it -- which was about seven years ago).

Now, that said, your next step in learning what to fix is to learn to ask better questions. Instead of saying "it doesn't work, here's my code," explain why it doesn't work (what did you expect to see, and what you actually saw) and under what conditions. It's helpful to provide a brief overview of the steps you've taken to diagnose and/or fix the problem (for example, why did floats "not work," because they're still the correct thing to be using here, so you must have done something else wrong).

"It doesn't work, here's my code," does not scale very well and you are quickly approaching the point where nobody will help you because you're not putting enough effort into the problem yourself. As I suggested before, if you install a decent IDE, you can use a decent debugger to help you find these problems rather easily.

Your new code has made the same silly mistake with respect to using = instead of ==. You have not fixed the integer division issue (it will truncate, as the other poster said; use floats). You have not fixed anything I pointed out before, either. In addition, try to ask better questions, and try not to change code around randomly trying to "see if that fixes it." Understand the problem, then apply a logical solution. Randomly changing things is a shotgun solution that does not lead to understanding of the problem and tends to lead to code that is broken in more subtle ways that just happen to look correct.

This topic is closed to new replies.

Advertisement