| View previous topic :: View next topic |
| Author |
Message |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Thu May 17, 2007 11:51 pm Post subject: My test number comparing program |
|
|
| Code: | #include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszARgs[])
{
int iNum1, iNum2;
cout << " Enter the first number: ";
cin >> iNum1;
cout <<"Enter the second number: ";
cin >> iNum2;
if (iNum1 < iNum2)
{
cout << "The second number is bigger than the first" << endl;
}
else
{
cout << "The first number is bigger than the second" << endl;
}
system("PAUSE");
return 0;
} |
Ok anyone see anything wrong with this code and how I can improve it I just learned a little more c++.
Oh yeah and here is the program.
Note that it only compares positive non decimal numbers since there stored as int.
_________________
Earthbound = 31337
Last edited by Losplagos on Thu May 17, 2007 11:53 pm; edited 1 time in total |
|
| Back to top |
|
 |
tigerite How do I cheat?
Reputation: 0
Joined: 16 May 2007 Posts: 9
|
Posted: Thu May 17, 2007 11:53 pm Post subject: |
|
|
Seems fine. You could reduce the statement
| Code: |
if (iNum1 < iNum2)
{
cout << "The second number is bigger than the first" << endl;
}
else
{
cout << "The first number is bigger than the second" << endl;
}
|
into the single statement
| Code: |
cout << "The " + (iNum1 < iNum2 ? "second" : "first") + " number is bigger than the " + (iNum1 < iNum2 ? "first" : "second") << endl;
|
and reduce some repitition.
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Thu May 17, 2007 11:56 pm Post subject: |
|
|
| tigerite wrote: | Seems fine. You could reduce the statement
| Code: |
if (iNum1 < iNum2)
{
cout << "The second number is bigger than the first" << endl;
}
else
{
cout << "The first number is bigger than the second" << endl;
}
|
into the single statement
| Code: |
cout << "The " + (iNum1 < iNum2 ? "second" : "first") + " number is bigger than the " + (iNum1 < iNum2 ? "first" : "second") << endl;
|
and reduce some repitition. |
I do it like this so it is easyer for me to read when or if I mess up on some of the code. Im making it a habit to write things out all the way when pratical so I can read it easyer when i have to correct errors.
Also anyone got any ideas what i can do next combining math / loops / and basics cout and cin commands.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Fri May 18, 2007 2:50 pm Post subject: |
|
|
Input: 5 and 5
Your output: The first number is bigger than the second
You forgot the case when they are equal.
Also, since you declared the vars without the unsigned modifier, they could be compared if they are negative.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Fri May 18, 2007 6:44 pm Post subject: |
|
|
Here is my code for it now which is in my main c++ programs post now making this thread useless.
| Code: |
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int formula()
{
double d1, d2,dA;
cout << "Enter the first postive number you would like to multiply: "; cin >> d1;
cout << "Enter the postive number you would like to multiply the first by: "; cin >> d2;
if(d1+d2 <= 0 )
{
cout << "This is not a postive number";
system("PAUSE");
}
else
{
dA = d1*d2;
return dA;
}
}
int main()
{
cout << "The answer to the problem is " << formula() << endl;
system("PAUSE");
}
|
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Fri May 18, 2007 9:08 pm Post subject: |
|
|
The point of functions is to make your code more "universal" in a sense. For this to happen, the function must either take in inputs or read in from a global value.
Your code could be modified as:
| Code: |
# include <cstdio>
# include <cstdlib>
# include <iostream>
using namespace std;
int formula (double d1, double d2)
{
return d1 * d2;
}
int main ()
{
int ret;
cout << "Enter the first postive number you would like to multiply: ";
cin >> d1;
cout << "Enter the postive number you would like to multiply the first by: ";
cin >> d2;
if (d1 + d2 <= 0) {
cout << "This is not a postive number";
ret = 1;
} else {
cout << "The answer to the problem is " << formula (d1,d2) << endl;
ret = 0;
}
getchar();
return ret;
}
|
Now, your formula function could be reused if you wished to perform the same operation elsewhere in your program (eg, you wanted to get the product of the two and the product of the numbers each increased by one. The could be done by simply passing in d1+1 and d2+1 as the parameters for your function instead of d1 and d2).
Sometimes you may have specialized procedures that are only going to be called once and always does the same thing, but are they will probably there only for organization purposes. In this case however, it's much more organized if you simply make the function only doing calculations.
Also, your line testing for d1 + d2 <= 0 only tests if the sum of the two numbers are non-negative. So if the inputs are -9 and 10, the code would continue.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Sat May 19, 2007 12:53 am Post subject: |
|
|
| DeltaFlyer wrote: | The point of functions is to make your code more "universal" in a sense. For this to happen, the function must either take in inputs or read in from a global value.
Your code could be modified as:
| Code: |
# include <cstdio>
# include <cstdlib>
# include <iostream>
using namespace std;
int formula (double d1, double d2)
{
return d1 * d2;
}
int main ()
{
int ret;
cout << "Enter the first postive number you would like to multiply: ";
cin >> d1;
cout << "Enter the postive number you would like to multiply the first by: ";
cin >> d2;
if (d1 + d2 <= 0) {
cout << "This is not a postive number";
ret = 1;
} else {
cout << "The answer to the problem is " << formula (d1,d2) << endl;
ret = 0;
}
getchar();
return ret;
}
|
Now, your formula function could be reused if you wished to perform the same operation elsewhere in your program (eg, you wanted to get the product of the two and the product of the numbers each increased by one. The could be done by simply passing in d1+1 and d2+1 as the parameters for your function instead of d1 and d2).
Sometimes you may have specialized procedures that are only going to be called once and always does the same thing, but are they will probably there only for organization purposes. In this case however, it's much more organized if you simply make the function only doing calculations.
Also, your line testing for d1 + d2 <= 0 only tests if the sum of the two numbers are non-negative. So if the inputs are -9 and 10, the code would continue. |
Thanks for replying. And I planned on expanding the program when i added the function. Also the | Code: | | if (d1 + d2 <= 0) { |
Was the only way i could think of comparing if there postive or negative without nested if commands.
Also could'nt i do intformula() in a include.
I just got into pointers...
Also i changed the program from a simple comparer and outputs which one is bigger to a multiplication program.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Sat May 19, 2007 10:53 am Post subject: |
|
|
Did you not learn booleans?
The "if" statement evaluates a boolean expression (true or false). So the statement could be as simple as "if (true) {". That doesn't do much however, since it's hard coded.
You want to check to see if both numbers are non-negative, so you could simply do the compare twice:
To link them, you could simple use the boolean "AND" operator - &&.
| Code: |
if (d1 >= 0 && d2 >= 0) {
|
The AND operator returns true only when both operands are true. So the if statement would only run if both values are non-negative.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
| Back to top |
|
 |
TheSorc3r3r I post too much
Reputation: 0
Joined: 06 Sep 2006 Posts: 2404
|
Posted: Sat May 19, 2007 4:41 pm Post subject: |
|
|
Or you could do:
_________________
Don't laugh, I'm still learning photoshop! |
|
| Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Sat May 19, 2007 4:50 pm Post subject: |
|
|
Well, to figure out how the numbers relate to each other...(< or > or =)
You could use a switch statement. So..
switch w.e int, then make cases of < or > or =..
You could also use an exception handler like...catch(...) to deal with the negative or decimal numbers.
Or you could just use a couple of if statements.
If int is less than 0 || int is decimal.
|
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Sat May 19, 2007 7:48 pm Post subject: |
|
|
| TheSorc3r3r wrote: | Or you could do:
|
and how would that work? It doesn't even make logical sense.
NOT FALSE returns true. Since FALSE in the C/++ languages is represented by 0, your statement would run only if !(d1 + d2) produced a non-zero. Taking away the NOT operator, the statement now depends on d1+d2 being zero to run. So that statement actually tests if the two numbers are the negatives of each other.
Just to make sure I wasn't horribly wrong somewhere, I made this:
| Code: |
int _tmain(int argc, _TCHAR* argv[])
{
int d1,d2;
for (;;){
cin>>d1>>d2;
if (!(d1+d2))
cout<<"NEG";
else
cout<<"POS";
}
return 0;
} |
POS was printed whether I entered 2,3 or 2,-3. NEG was printed when I entered 2,-2.
| smartz993 wrote: | Well, to figure out how the numbers relate to each other...(< or > or =)
You could use a switch statement. So..
switch w.e int, then make cases of < or > or =..
You could also use an exception handler like...catch(...) to deal with the negative or decimal numbers.
Or you could just use a couple of if statements.
If int is less than 0 || int is decimal. |
Have you ever used a switch statement? The switch is used for cases with definite values that you can implement when coding. To implement a check for positive or negative with switch, you would have to list all the positive numbers and negative numbers, one by one. That is not physically possible. Not to mention that fractions might become involved.
How are you planning on making it throwing an exception?
"If int is less than 0 OR int is decimal"... WHAT? That doesn't even make sense.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
| Back to top |
|
 |
TheSorc3r3r I post too much
Reputation: 0
Joined: 06 Sep 2006 Posts: 2404
|
Posted: Sat May 19, 2007 9:12 pm Post subject: |
|
|
| DeltaFlyer wrote: | | TheSorc3r3r wrote: | Or you could do:
|
and how would that work? It doesn't even make logical sense. |
I was thinking along the lines of testing if they're both zero, but you're right.
_________________
Don't laugh, I'm still learning photoshop! |
|
| Back to top |
|
 |
nox Expert Cheater
Reputation: 0
Joined: 09 Apr 2007 Posts: 227 Location: brooklyn
|
Posted: Tue May 22, 2007 12:53 pm Post subject: |
|
|
| using capital letters in variables is a bad idea, especially if you're going to mix
|
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Tue May 22, 2007 1:05 pm Post subject: |
|
|
| nox wrote: | | using capital letters in variables is a bad idea, especially if you're going to mix |
http://en.wikipedia.org/wiki/CamelCase#Coding_style
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
| Back to top |
|
 |
nox Expert Cheater
Reputation: 0
Joined: 09 Apr 2007 Posts: 227 Location: brooklyn
|
Posted: Tue May 22, 2007 2:08 pm Post subject: |
|
|
what was posted wasn't even camelcase to begin with, camelcase uses full words without spaces to improve clarity, "iNum1/2" is not camelcase.
btw what you linked me to says nothing of using c/c++, only java and some other junk.
the variables names are unclear and misleading, had the program been more advanced than it is - no doubt there'd be confusion
|
|
| Back to top |
|
 |
|