| 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 Aug 23, 2007 6:06 am Post subject: Here is my noob c++ calculation program that is a form test |
|
|
| Code: | #include <iostream>
#include <math.h>
using namespace std;
const double Pi = 3.14159;
double Radians(double A)
{
double R;
if(A>0)
{
R = Pi/180*A;
}
else
{
cout << "Number Was not positive\n";
}
return R;
}
int Radians(int A)
{
int R;
if(A>0)
{
R = A*A;
}
else
{
cout << "Number Was not positive\n";
}
return R;
}
int main(int D)
{
D=0;
for( D>0;; )
{
cout << "Input 0 or under to quit: ";
cin >> D;
double Answer;
int Power,Gravity;
cout << "Angle?\n";
cin >> Answer;
Answer= Radians(Answer);
cout << "Power in pixels?\n";
cin >> Power;
cout << "Gravity of the shot\n";
cin >> Gravity;
Answer= Radians(Power)*sin(2*Answer)/Gravity;
cout << "Shot distance traveled is: " << Answer<<endl;
system("PAUSE");
}
return 1;
}
|
Any ways i can improve it? This is a simple gunbound calculation program.
Oh yeah i should of removed that system("PAUSE");
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
goldengold Grandmaster Cheater Supreme
Reputation: -1
Joined: 11 Nov 2006 Posts: 1841 Location: -.-
|
Posted: Thu Aug 23, 2007 8:21 am Post subject: |
|
|
Zomg where are all the
//comment your work you fool
_________________
|
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Thu Aug 23, 2007 8:33 am Post subject: |
|
|
Indent your code properly:
| Code: |
# include < iostream >
# include < math.h >
using namespace std;
const double Pi = 3.14159;
double Radians (double A)
{
double R;
if (A > 0)
{
R = Pi / 180 * A;
}
else
{
cout << "Number Was not positive\n";
}
return R;
}
int Radians (int A)
{
int R;
if (A > 0)
{
R = A * A;
}
else
{
cout << "Number Was not positive\n";
}
return R;
}
int main (int D)
{
D = 0;
for (D > 0 ;;)
{
cout << "Input 0 or under to quit: ";
cin >> D;
double Answer;
int Power, Gravity;
cout << "Angle?\n";
cin >> Answer;
Answer = Radians (Answer);
cout << "Power in pixels?\n";
cin >> Power;
cout << "Gravity of the shot\n";
cin >> Gravity;
Answer = Radians (Power) * sin (2 * Answer) / Gravity;
cout << "Shot distance traveled is: " << Answer << endl;
system ("PAUSE");
}
return 1;
}
|
Don't use the argument your main function takes in as a variable for the program. The variables Answer, Power, and Gravity should be declared outside of the loop. Use getch/getchar/cin.get instead of system("PAUSE").
_________________
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Aug 23, 2007 8:42 am Post subject: |
|
|
return 1? o.O return 0...
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Aug 23, 2007 10:40 am Post subject: |
|
|
- Don't include math.h, if you need it, include cmath.
- Don't include the whole name space. If you don't want to use std:: always, do using std::cin, std::cout etc.
- Global variables aren't good, but const ones might be acceptable.
- You could calculate some digits of pi instead of defining it.
- In C++ you _MUST_ define prototypes. It isn't required in C, but in C++ it is!
- std::endl instead of '\n'.
( - there's unneeded { }, but no one tells you to take 'em out. )
- Everything related to "int D" is kinda fcked up. :P
-> don't define it in "int main()".
-> what happens if user doesn't input digits? This also applies to "int Power" and "int Gravity".
-> that "for" loop is ugly :< Why aren't you using "while" or "do...while"?
-> you could check if D>0 before calling those functions.
- Don't use "system("PAUSE")". Not all platforms support it.
- return 1 equals to return "EXIT_FAILURE", doesn't it? So, use "EXIT_SUCCESS" and "EXIT_FAILURE" instead of 0 or 1, and you will not get confused.
- Use proper formatting (tabs, spaces, etc).
- Commenting your code is also recommended.
Note: we're talking about C++, not C.
Eh.. What else? I'll tell you if I spot more failures after going to sauna.
PS. nothing personal :p
EDIT:
- You should also check the value of input against the limits. If it's too high, the program might do something unexpected.
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Thu Aug 23, 2007 10:15 pm Post subject: |
|
|
| Jani wrote: | - Don't include math.h, if you need it, include cmath.
- Don't include the whole name space. If you don't want to use std:: always, do using std::cin, std::cout etc.
- Global variables aren't good, but const ones might be acceptable.
- You could calculate some digits of pi instead of defining it.
- In C++ you _MUST_ define prototypes. It isn't required in C, but in C++ it is!
- std::endl instead of '\n'.
( - there's unneeded { }, but no one tells you to take 'em out. )
- Everything related to "int D" is kinda fcked up.
-> don't define it in "int main()".
-> what happens if user doesn't input digits? This also applies to "int Power" and "int Gravity".
-> that "for" loop is ugly :< Why aren't you using "while" or "do...while"?
-> you could check if D>0 before calling those functions.
- Don't use "system("PAUSE")". Not all platforms support it.
- return 1 equals to return "EXIT_FAILURE", doesn't it? So, use "EXIT_SUCCESS" and "EXIT_FAILURE" instead of 0 or 1, and you will not get confused.
- Use proper formatting (tabs, spaces, etc).
- Commenting your code is also recommended.
Note: we're talking about C++, not C.
Eh.. What else? I'll tell you if I spot more failures after going to sauna.
PS. nothing personal :p
EDIT:
- You should also check the value of input against the limits. If it's too high, the program might do something unexpected. |
Oh when i was doing this i was half asleep so thats why i did some stupid stuff like return 1 instead 0.
This should fix a few of those problems that i know how too if you would please revise my code for me so i can see how it should be done.
| Code: | # include <iostream>
# include <cmath>
const double Pi = 3.14159;
int D=1;
double Radians (double A)// This function calculates The angle
{
double R;
R = Pi / 180 * A;
return R;
}
int Radians (int A) //This function squares a number
{
int R;
R = A * A;
return R;
}
int main ()
{
double Answer,Power, Gravity; //This Loop gets all the variables and checks them
std::cout << "Input 0 or under to quit: "; // explaining the rules
std::cin >> D;
while (D>0){
std::cout << "If you input a number that is not postive"<<std::endl
<<"0 or over 99 for the angle or over 400 for the power the "
<<"program will close."<<std::endl;
std::cout << "Angle?: ";
std::cin >> Answer;
if (Answer<=0&Answer<100) continue;
Answer = Radians (Answer);
std::cout << "Power in pixels up too 400?"<<std::endl;
std::cin >> Power;
if (Power<=0&Power<400) continue;
std::cout << "Gravity of the shot"<<std::endl;
std::cin >> Gravity;
if (Gravity<=0) continue;
Answer = Radians (Power) * sin (2 * Answer) / Gravity; // The formula*
std::cout << "Shot distance traveled is: " << Answer << std::endl; }
;
return 0;
} |
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Aug 24, 2007 4:02 am Post subject: |
|
|
First of all. Did you even read my whole post? -_-
A quick analysis:
- Your formatting still fails.
- Global variables can cause problems. (int D=1.)
- You aren't prototyping your functions! It is a must in C++!
- That double&int Radians functions are kind of confusing, because of they've the same name, but they're not doing the same thing.
- int main() looks messy -> hard to understand.
- You still aren't checking if std::cin >> D is a number.
- "Answer<=0&Answer<100"
-> Where are the parentheses? Parentheses make your code easier to read.
-> & is an bitwise operator AND.
-> Your code gets really messy here. Using continue is confusing.
- "Answer = blablablba"
-> Use parentheses!
- You're still returning 0 instead of EXIT_SUCCESS;
I'll tell you if I spot more mistakes later.
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Fri Aug 24, 2007 7:58 am Post subject: |
|
|
| Jani wrote: | First of all. Did you even read my whole post? -_-
A quick analysis:
- Your formatting still fails.
- Global variables can cause problems. (int D=1.)
- You aren't prototyping your functions! It is a must in C++!
- That double&int Radians functions are kind of confusing, because of they've the same name, but they're not doing the same thing.
- int main() looks messy -> hard to understand.
- You still aren't checking if std::cin >> D is a number.
- "Answer<=0&Answer<100"
-> Where are the parentheses? Parentheses make your code easier to read.
-> & is an bitwise operator AND.
-> Your code gets really messy here. Using continue is confusing.
- "Answer = blablablba"
-> Use parentheses!
- You're still returning 0 instead of EXIT_SUCCESS;
I'll tell you if I spot more mistakes later. |
1. I can read my formatting fine and it is quite hard to remember all the coding such as std::cin with a system of tabs and i write it the way my compiler formats it.
2. I am fixing global variables.
3. Ok so it is mandatory in c++ so i will do it when i edit this post adding my late'st source that still fails at formatting
4. The double and int functions are to test overloading functions.
5. I original wrote the code for myself then thought to put it up on the forums to see what im doing wrong.
6. I do not know how i would go about checking if it is not a number.
7. You mean parentheses in the math?
8. I know & is and thats why i used it for (Power<=0&Power<400) as a example.
9. Using continue is the only way i could think of doing it with my limited c++ knowledge i only started up again yesterday or the day before.
10. In my newer source code i do use parenthesis for the math.
11. Ok im returning EXIT_SUCCESS; now
thanks for the reply.
void edit()
{
std::cout"I was wrong last one";
return EXIT_FAILURE;
}
_________________
Earthbound = 31337
Last edited by Losplagos on Fri Aug 24, 2007 8:23 am; edited 2 times in total |
|
| Back to top |
|
 |
zart Master Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 351 Location: russia
|
Posted: Fri Aug 24, 2007 8:14 am Post subject: |
|
|
| goldengold wrote: | Zomg where are all the
//comment your work you fool |
Not that I'd ever suggest people to NOT comment there code... but his is pretty self-commenting...
The only time i'd say you MUST comment is in asm... C/C++ you should be able to write intelligently enough that it doesn't need a plethora of comments
[quote = "oib111"]return 1? o.O return 0...[/quote]
While the normal format is return 0... It doesn't really matter... main just needs to return an integer - the system doesn't really care what - you might run into trouble if you start piping things but heck - you can not return anything and the compilers normally just whine about a warning - not an error
_________________
0x7A 0x61 0x72 0x74
TEAM RESURRECTiON |
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Fri Aug 24, 2007 8:27 am Post subject: |
|
|
| Code: | # include <iostream>
# include <cmath>
double Radians(double);
int Radians(int);
const double Pi = 3.14159;
double Radians (double A)// This function calculates The angle
{
double R;
R = Pi / 180 * A;
return R;
}
int Radians (int A) //This function squares a number
{
int R;
R = A * A;
return R;
}
int main ()
{
double Answer,Power, Gravity; //This Loop gets all the variables and checks them
int D;
int Z;
std::cout
<< "Input 0 or under to quit"
<< std::endl;
std::cin >> D;
while (D>0)
{
std::cout << "If you input a number that is not postive"<<std::endl
<<"0 or over 99 for the angle or over 400 for the power the " // explaining the rules
<<"program will not work."<<std::endl;
std::cout << "Angle?: ";
std::cin >> Answer; // The next comment full of code i need to improve any ideas?
/* if (Answer<=0&Answer<100) continue;
Answer = Radians (Answer);
std::cout
<< "Power in pixels up too 400?"
<<std::endl;
std::cin >> Power;
if (Power<=0&Power<400) continue;
std::cout
<< "Gravity of the shot"
<<std::endl;
std::cin >> Gravity;
Answer = Radians (Power) * (sin (2 * Answer) / Gravity); // The formula*
std::cout
<< "Shot distance traveled is: "
<< Answer
<< std::endl; */
}
return EXIT_SUCCESS;
} |
Ok there is my newer code after a few minutes of editing i still need to improve alot better get my nose to the grind stone on learning more c++.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Aug 24, 2007 8:36 am Post subject: |
|
|
I'm not going to say anything anymore. You don't even read my posts. Ok, one question: where are the prototypes?!?! :O
btw.. Your code keeps going more messy and messy.
|
|
| Back to top |
|
 |
zart Master Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 351 Location: russia
|
Posted: Fri Aug 24, 2007 8:39 am Post subject: |
|
|
losplagos
Ok here's just some nit-picking things...
Your declaring your functions prototypes, thats a good thing, but you don't need to unless the functions reside below your main function (int main). Normally if you have everything heaped into one file, it's easier to have the main function at the top for readability... Thats when you would have to declare the function prototypes. There is no need to do this if your main is the last function in the code.
Also this code;
| Code: |
int Radians (int A) //This function squares a number
{
int R;
R = A * A;
return R;
}
|
Is not needed... Instead of doing "Radians (Power)" to square the variable 'power' just do, Power^2. Thats the mathematical equivalent to raising a number to a certain power in c/cpp.
All for now... back to work..
@jani
He did prototype... Why both though with such small functions? Also i listed above why he doesn't need too...
_________________
0x7A 0x61 0x72 0x74
TEAM RESURRECTiON |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Aug 24, 2007 8:43 am Post subject: |
|
|
| zart wrote: | | Power^2. Thats the mathematical equivalent to raising a number to a certain power in c/cpp. | That's Power xor'd by 2.
| zart wrote: | | He is prototype... Why both though with such small functions? Also i listed above why he doesn't need too... | void function(); // This is prototype
int main()
{
blabla
}
void function() // and the definition comes after!
{
blabla
}
Or.. W/e. I think you should only prototype the header of the function there and leave the definition after.
|
|
| Back to top |
|
 |
zart Master Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 351 Location: russia
|
Posted: Fri Aug 24, 2007 8:45 am Post subject: |
|
|
| Jani wrote: | | zart wrote: | | Power^2. Thats the mathematical equivalent to raising a number to a certain power in c/cpp. | That's Power xor'd by 2.
| zart wrote: | | He is prototype... Why both though with such small functions? Also i listed above why he doesn't need too... | void function(); // This is prototype
int main()
{
blabla
}
void function // and the definition comes after!
{
blabla
} |
From his code;
# include <iostream>
# include <cmath>
double Radians(double);
int Radians(int);
const double Pi = 3.14159;
double Radians (double A)// This function calculates The angle
{
double R;
R = Pi / 180 * A;
return R;
}
int Radians (int A) //This function squares a number
{
int R;
R = A * A;
return R;
}
int main ()
{
in bold, the prototypes
in italics the function definitions... the definitions are ABOVE the main function, and they don't need to be prototyped because of this...
_________________
0x7A 0x61 0x72 0x74
TEAM RESURRECTiON |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Aug 24, 2007 8:46 am Post subject: |
|
|
| zart wrote: | | the definitions are ABOVE the main function, and they don't need to be prototyped because of this... | Oops! :P My bad. Sorry. I didn't read it properly.
EDIT: Ok.. I'm bored. I wrote some examples. variable_class.h is only needed by Losplagos_calc_objects_advanced.cpp. Have fun ! :) Tell me my mistakes and I'll learn. I didn't recheck 'em, so there might be some errors. (But I tested 'em and they do compile and the answer is the same.)
|
|
| Back to top |
|
 |
|