| View previous topic :: View next topic |
| Author |
Message |
SkinnyQ Master Cheater
Reputation: 0
Joined: 16 Jun 2006 Posts: 411
|
Posted: Tue Sep 04, 2007 12:06 pm Post subject: My First 2 programs in C++ |
|
|
Hi,
started C++ today.
My first programs are:
Miles to Kilometers convertor
Fahrenait to Celsius convertor.
Flame me for trying?  |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Tue Sep 04, 2007 12:13 pm Post subject: |
|
|
Nice. My only suggestion is to make functions for Farenheight to Celcius and vice versa and the same with your Miles to Kilometer thing. Then change your second if statement in both programs to if else statements. Next add an else statement that will say that they didn't select a valid option and then call your starting function that asks for their choice. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
SkinnyQ Master Cheater
Reputation: 0
Joined: 16 Jun 2006 Posts: 411
|
Posted: Tue Sep 04, 2007 12:16 pm Post subject: |
|
|
| oib111 wrote: | | Nice. My only suggestion is to make functions for Farenheight to Celcius and vice versa and the same with your Miles to Kilometer thing. Then change your second if statement in both programs to if else statements. Next add an else statement that will say that they didn't select a valid option and then call your starting function that asks for their choice. |
I understand. I would try to make another function\s.
Tho what do you mean by the vice versa.
EDIT: Here, I tryed to do what you said with the miles to km convertor. |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Tue Sep 04, 2007 12:29 pm Post subject: |
|
|
Vice versa. With the order or meaning reversed; conversely. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
SkinnyQ Master Cheater
Reputation: 0
Joined: 16 Jun 2006 Posts: 411
|
Posted: Tue Sep 04, 2007 12:35 pm Post subject: |
|
|
| oib111 wrote: | | Vice versa. With the order or meaning reversed; conversely. |
It does vice versa, I mean- converts Miles to kms and kms to miles.
And please tell me hows the new source code. |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Tue Sep 04, 2007 1:43 pm Post subject: |
|
|
I mean do a function for kms to mi and mi to kms.
Edit:
I edited it. I put in a infinite while loop. I changed your function name to invalid, put in some new lines here and there and deleted out conio.h, you really didn't need it. So heres the source + .exe file.
| Code: |
#include <iostream>
using namespace std;
int main()
{
while(1) {
int choice;
void invalid();
double miles, kilometers;
cout << "This Program Converts Miles to Kilometers.\n";
cout << "Press the option you want.\n\n";
cout << "1 - Converting Kilometers to miles.\n";
cout << "2 - Converting Miles to kilometers.\n";
cout << "3 - Quit\n\n";
cin >> choice;
if(choice == 1)
{
cout << "\nHow many kilometers: \n\n";
cin >> kilometers;
miles = kilometers * 0.62;
cout <<"\n\n"<< kilometers << " Kilometers " << "is\n\n" << miles << " Miles.\n\n";
}
else if(choice == 2)
{
cout << "\nHow many miles: \n\n";
cin >> miles;
kilometers = miles * 1.61;
cout <<"\n\n"<< miles << " Miles " << "is\n\n" << kilometers << " Kilometers.\n\n";
}
else if(choice == 3)
{
return 0;
}
else
{
invalid();
}
}
return 0;
}
void invalid()
{
cout << "\nYou entered an invaild value.\n";
cout << "Please Try again.\n\n";
main();
}
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Tue Sep 04, 2007 3:42 pm Post subject: |
|
|
oib111, that is horrid.
1) for(;;) NOT while(1)!
2) Do not define variables in a loop!
3) That invalid function is useless and superfluous!
4) That chain of if/else statements would look much better as a swich.
5) Function looping is fun, but after a while the program will crash!
| Code: | void invalid()
{
cout << "\nYou entered an invaild value.\n";
cout << "Please Try again.\n\n";
main(); <------- FAIL!
} |
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Sep 04, 2007 3:45 pm Post subject: |
|
|
in my far - cel converter (win32 version) i used functions
look at my source _________________
|
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Tue Sep 04, 2007 3:50 pm Post subject: |
|
|
| oib111 wrote: | I mean do a function for kms to mi and mi to kms.
Edit:
I edited it. I put in a infinite while loop. I changed your function name to invalid, put in some new lines here and there and deleted out conio.h, you really didn't need it. So heres the source + .exe file. |
By doing what you did there, the loop serves no purpose at all since it would never reach its end. Making the "invalid" function is pointless in this case as you're only calling it once. Declare the function prototypes outside of other functions. Declare the variables outside of loops.
The big if-else structure could be replaced with a more readable switch-case structure. Instead of writing cout every time you want to start a new line to make your code more readable, you could simply remove the second cout and the ending semicolon on the previous line.
Edit: I type slow... Flyte got it first. _________________
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: Tue Sep 04, 2007 4:23 pm Post subject: |
|
|
Lol, I did this in like 5 seconds. I didn't think about what's best or whats not. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
TheSorc3r3r I post too much
Reputation: 0
Joined: 06 Sep 2006 Posts: 2404
|
Posted: Wed Sep 05, 2007 7:59 am Post subject: |
|
|
| Flyte wrote: | 1) for(; NOT while(1)! |
who cares? _________________
Don't laugh, I'm still learning photoshop! |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Wed Sep 05, 2007 8:05 am Post subject: |
|
|
| TheSorc3r3r wrote: | | who cares? |
while(1) translates roughly to this
| Code: |
TestLoop:
mov eax, 1
cmp eax, 1
jne OutOfLoop
InLoop:
...
jmp TestLoop
|
for(;;) translates roughly to this
| Code: |
InLoop:
...
jmp InLoop
|
You see how much instructions you save? You are creating an unnecessary compare for something that is not logically sound. Ignore the fact that optimizing compilers will realize that 1 is equal to TRUE and will not do a compare- regardless of how it performs on the machine level it is always a pain to anyone reading your code: why did he compare 1 against 1? Is this developer clinically retarded? Should I bother to continue his work? |
|
| Back to top |
|
 |
zart Master Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 351 Location: russia
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Wed Sep 05, 2007 8:18 am Post subject: |
|
|
| It's not so much about speed as it is readability. It's like putting if(2 < 5) before every statement you want to execute unconditionally. |
|
| Back to top |
|
 |
zart Master Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 351 Location: russia
|
|
| Back to top |
|
 |
|