| View previous topic :: View next topic |
| Author |
Message |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 10:45 pm Post subject: [C++]My first real program |
|
|
heres a program that i wrote today. ive been learning C++ since yesterday and my first program was "hello world" but i didnt feel that was worth sharing. I thought of writing it when i was working on my geometry homework a few hours ago. tell me of any errors that you see and suggestions to fix them.
| Code: | // This program will calculate the measure of the third angle of a triangle based on the measure of the first and second angles.
#include <iostream>
using namespace std;
// function prototype
void line();
int main()
{
// declare and initialize variables
float angle1 = 0.0;
float angle2 = 0.0;
float middleman = 0.0;
float angle3 = 0.0;
int response = 0;
//Introduction
cout << "This program will calculate the measure of the third angle of a triangle based on the measure of the first and second angles." << endl;
//loop if desired
loop:
//enter angle1 and angle2
line();
cout << "Enter the value of the first angle: ";
cin >> angle1;
line();
cout << "Enter the value of the second angle: ";
cin >> angle2;
//calculate the third angle
middleman = angle1 + angle2;
angle3 = 180 - middleman;
//display results
line();
cout << "Result of the calculation..." << endl;
line();
if (angle3 > 0 && angle3 < 181)
{
cout << "The measure of the third angle: " << angle3 << " degrees" << endl;
line();
}
else
{
cout << "Those angles do not form a triangle" << endl;
line();
}
//decide to loop or exit
cout << "Would you like to calculate another angle?" << endl << "1. Yes" << endl << "2. No" << endl;
line();
cout << "Your response: ";
cin >> response;
while (response == 1)
{
//enter angle1 and angle2
line();
cout << "Enter the value of the first angle: ";
cin >> angle1;
line();
cout << "Enter the value of the second angle: ";
cin >> angle2;
//calculate the third angle
middleman = angle1 + angle2;
angle3 = 180 - middleman;
//display results
line();
cout << "Result of the calculation..." << endl;
line();
if (angle3 > 0 && angle3 < 181)
{
cout << "The measure of the third angle: " << angle3 << " degrees" << endl;
line();
}
else
{
cout << "Those angles do not form a triangle" << endl;
line();
}
//decide to loop or exit
cout << "Would you like to calculate another angle?" << endl << "1. Yes" << endl << "2. No" << endl;
line();
cout << "Your response: ";
cin >> response;
}
line();
while (response == 2)
{
cout << "Press the Enter key to exit";
cin.ignore();
cin.get();
return 0;
}
}
//function definition
void line()
{
cout << "--------------------------";
cout << endl;
} // end of display line function |
ill upload the .exe just in case you dont want to compile it... _________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Jan 28, 2008 10:49 pm Post subject: |
|
|
Just so you know, if you put a function above any other function that calls it you do not need to define the prototype at the top of the file. Such as yours:
| Code: |
void line();
int main()
{
// code here
}
void line()
{
// code here
} |
Instead you can write just this:
| Code: | void line()
{
// code here
}
int main()
{
// code here
} |
_________________
- Retired. |
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 10:54 pm Post subject: |
|
|
alright thanks, anything else you see that could be fixed? _________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Tue Jan 29, 2008 1:05 am Post subject: |
|
|
| Wiccaan wrote: | Just so you know, if you put a function above any other function that calls it you do not need to define the prototype at the top of the file. Such as yours:
| Code: |
void line();
int main()
{
// code here
}
void line()
{
// code here
} |
Instead you can write just this:
| Code: | void line()
{
// code here
}
int main()
{
// code here
} |
|
Wow wiccaan im suprised you did'nt say more. I will more than likely reply with some code that will be bad probably. I will quote his code and then after some sleep or something i will try to clean it up a little but it will probably still not be up to par.
| Guernica? wrote: |
| Code: | // This program will calculate the measure of the third angle of a triangle based on the measure of the first and second angles.
#include <iostream>
using namespace std;
// function prototype
void line();
int main()
{
// declare and initialize variables
float angle1 = 0.0;
float angle2 = 0.0;
float middleman = 0.0;
float angle3 = 0.0;
int response = 0;
//Introduction
cout << "This program will calculate the measure of the third angle of a triangle based on the measure of the first and second angles." << endl;
//loop if desired
loop:
//enter angle1 and angle2
line();
cout << "Enter the value of the first angle: ";
cin >> angle1;
line();
cout << "Enter the value of the second angle: ";
cin >> angle2;
//calculate the third angle
middleman = angle1 + angle2;
angle3 = 180 - middleman;
//display results
line();
cout << "Result of the calculation..." << endl;
line();
if (angle3 > 0 && angle3 < 181)
{
cout << "The measure of the third angle: " << angle3 << " degrees" << endl;
line();
}
else
{
cout << "Those angles do not form a triangle" << endl;
line();
}
//decide to loop or exit
cout << "Would you like to calculate another angle?" << endl << "1. Yes" << endl << "2. No" << endl;
line();
cout << "Your response: ";
cin >> response;
while (response == 1)
{
//enter angle1 and angle2
line();
cout << "Enter the value of the first angle: ";
cin >> angle1;
line();
cout << "Enter the value of the second angle: ";
cin >> angle2;
//calculate the third angle
middleman = angle1 + angle2;
angle3 = 180 - middleman;
//display results
line();
cout << "Result of the calculation..." << endl;
line();
if (angle3 > 0 && angle3 < 181)
{
cout << "The measure of the third angle: " << angle3 << " degrees" << endl;
line();
}
else
{
cout << "Those angles do not form a triangle" << endl;
line();
}
//decide to loop or exit
cout << "Would you like to calculate another angle?" << endl << "1. Yes" << endl << "2. No" << endl;
line();
cout << "Your response: ";
cin >> response;
}
line();
while (response == 2)
{
cout << "Press the Enter key to exit";
cin.ignore();
cin.get();
return 0;
}
}
//function definition
void line()
{
cout << "--------------------------";
cout << endl;
} |
|
Edit: Sleepy brains are bad for coding. I will still post up "cleaned up" verision of this code. _________________
Earthbound = 31337 |
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Tue Jan 29, 2008 1:20 am Post subject: |
|
|
thanks. ill take a look at what you changed. _________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Tue Jan 29, 2008 4:01 am Post subject: |
|
|
This is not very good but it should be a improvement over your first code you posted.
| Code: | /*This program will calculate the measure of the third angle of a triangle based
on the measure of the first and second angles. This was put together very
quickly. And will more than likely have some errors that should not be there*/
#include <iostream>
float input(); // Simple function that checks for invalid input
int input(int min, int max); // checks choice for invalid input
float math(float first, float second); // This function does the math for me
int main()
{
//Initialize variables
bool loop = true; // true = 1 and false = 0
int choice = 0; // to control the switch to exit the loop
std::cout << "Welcome this calculates the third angle of a triangle\n";
// The std:: is like saying use cout from namespace std
while(loop==true)
{
std::cout << "Enter 1 to continue 0 to exit: ";
choice = input(0,1); //Says run input and put the return
// value in choice. and saves me some typing
// by just putting the min / max instead of typing out
// min = 0; max = 1; you can expand on this also ^_^
switch(choice)
{
case 0:
std::cout << "~Crys~ I hade a good run";
loop = false;
break;
case 1:
//Declaring and initializing
//If i really was worried about memory
//i would actually make these pointers
float math1 = 0.0, math2 = 0.0;
std::cout << "Enter the first angle: ";
math1 = input();
std::cout << "Enter the second angle: ";
math2 = input();
math(math1,math2);
break;
}
}
}
int input(int min, int max) //Overloaded input function takes int / min / max
{
int returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue < min|| returnvalue > max)
{
std::cout << "Bad input try again: ";
std::cin.sync();
std::cin.clear();
std::cin >> returnvalue;
}
return returnvalue;
}
float input()
{
float returnvalue;
std::cin >> returnvalue;
while(std::cin.fail())
{
std::cout << "Bad input try again: ";
std::cin.sync();
std::cin.clear();
std::cin >> returnvalue;
}
return returnvalue;
}
float math(float first, float second)
{
if(first-second > 181 || first-second < 0)
{
std::cout << "Those angles do not form a triangle.";
return EXIT_SUCCESS;
}
else
{
std::cout << "The third angle of the triangle is: "
<< 180-(first-second) << std::endl;
return EXIT_SUCCESS;
}
} |
I hope i commented it enough since im almost falling down asleep right now. Amazing that little program can take 400K - 1600K of ram. _________________
Earthbound = 31337
Last edited by Losplagos on Tue Jan 29, 2008 4:18 am; edited 1 time in total |
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Jan 29, 2008 4:10 am Post subject: |
|
|
that's your first program ? to me it looks like i need to study 1 year C++ to make something like it -.-
nice btw, if you just leanred C++ yesterday then you're going to be alright.. |
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Tue Jan 29, 2008 4:29 am Post subject: |
|
|
Here is the original code with some crap taken out.
| Code: | // This program will calculate the measure of the third angle of a triangle based on the measure of the first and second angles.
#include <iostream>
using namespace std;
// function prototype
void line();
int main()
{
// declare and initialize variables
float angle1 = 0.0;
float angle2 = 0.0;
float middleman = 0.0;
float angle3 = 0.0;
int response = 0;
//Introduction
cout << "This program will calculate the measure of the third angle of a triangle based on the measure of the first and second angles." << endl;
cout << "Would you like to calculate another angle?" << endl << "1. Yes" << endl << "2. No" << endl;
line();
cout << "Your response: ";
cin >> response;
while (response == 1)
{
//enter angle1 and angle2
line();
cout << "Enter the value of the first angle: ";
cin >> angle1;
line();
cout << "Enter the value of the second angle: ";
cin >> angle2;
//calculate the third angle
middleman = angle1 + angle2;
angle3 = 180 - middleman;
//display results
line();
cout << "Result of the calculation..." << endl;
line();
if (angle3 > 0 && angle3 < 181)
{
cout << "The measure of the third angle: " << angle3 << " degrees" << endl;
line();
}
else
{
cout << "Those angles do not form a triangle" << endl;
line();
}
//decide to loop or exit
cout << "Would you like to calculate another angle?" << endl << "1. Yes" << endl << "2. No" << endl;
line();
cout << "Your response: ";
cin >> response;
}
cout << "Press the Enter key to exit";
cin.ignore();
cin.get();
return EXIT_SUCCESS;
}
//function definition
void line()
{
cout << "--------------------------";
cout << endl;
} // end of display line function
|
I love having a input() function. I might think about making a header file for common functions i use. _________________
Earthbound = 31337 |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jan 29, 2008 8:04 am Post subject: |
|
|
| @Losplagos: (speaking of your own code)you've improved your code hella lot since last time! Glancing thru your code and I noticed that the quality of the code is very good. Greets! |
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Tue Jan 29, 2008 9:00 am Post subject: |
|
|
@Jani thankyou before I just did'nt approach learn c++ right so my code was horrible.
And back on topic as some extra advice to guernica three things are your best friends at this point.
1, Comments
2, Functions
3,A GOOD NIGHTS SLEEP ^_^ _________________
Earthbound = 31337 |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Tue Jan 29, 2008 10:50 am Post subject: |
|
|
| Losplagos wrote: | Wow wiccaan im suprised you did'nt say more. I will more than likely reply with some code that will be bad probably. I will quote his code and then after some sleep or something i will try to clean it up a little but it will probably still not be up to par.
|
I would have, but it was late and I went to bed after checking the forums. _________________
- Retired. |
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Tue Jan 29, 2008 4:29 pm Post subject: |
|
|
thanks for all the suggestions guys. ill take them into consideration.
@rot1
yeah but you probably understood what you were doing. im asking a questions here like ever 5 seconds. _________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Jan 30, 2008 8:37 am Post subject: |
|
|
| guernica wrote: | @rot1
yeah but you probably understood what you were doing. im asking a questions here like ever 5 seconds. |
Heh, you'd be amazed... _________________
- Retired. |
|
| Back to top |
|
 |
|