Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Can I do this any other way.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Jun 24, 2007 11:35 pm    Post subject: Can I do this any other way. Reply with quote

Ok, I made two programs. A program that calculates an area of a circle (a=pi*r2). And another program that converts farenheit to celcius and vice versa ((c=(f-32)*5/9) and (f=(c*9)/5+32)). Here are the source codes.

Circle Area Calculator:

Code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()

{
    float r;
    float pi = 3.14;
    float a;
    char i;
   
    AreaCalc:
    cout<<"Please enter the radius of the circle.\n";
    cin>>r;
    a=(pi*r)*r;
    cout<<"The area of the circle is "<< a <<".\n";
    cout<<"\n";
    cout<<"If you would like to calculate the area of another circle, press 'i'.\n";
    cin>>i;
    if (i=='i') {
                goto AreaCalc;
    }
    else
    system("PAUSE");
    return EXIT_SUCCESS;
}


Temperature Converter:

Code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()

{
    float f,c;
    char i,a;
   
    Converter:
    cout<<"Press 'f' to convert farenheit to celcius and 'c' to convert celcius to \nfarenheit.\n";
    cin>>i;
    if (i=='f') {
       cout<<"\nPlease enter the temperature in farenheit.\n";
       cin>>f;
       c=(f-32)*5/9;
       cout<<"The temperature in celcius is "<< c <<".\n";
       cout<<"If you wish to convert another temperature press 'a'.\n";
       cin>>a;
       if (a=='a') {
          goto Converter;
       }
       else
       system("PAUSE");
       return EXIT_SUCCESS;
    }   
    else if (i=='c') {
       cout<<"\nPlease enter the temperature in celcius.\n";
       cin>>c;
       f=(c*9)/5+32;
       cout<<"The temperature in farenheit is "<< f <<".\n";
       cout<<"If you wish to convert another temperature press 'a'.\n";
       cin>>a;
       if (a=='a') {
          goto Converter;
       }
       else         
       system("PAUSE");
       return EXIT_SUCCESS;
    }
    else
         cout<<"\nInvalid input, your choices are 'f' and 'c'.\n";
         cout<<"\n";
    goto Converter;
}


Ok, now I learned from eagle358 about the: #include <cstdlib>, system("PAUSE"), and the return EXIT_SUCCESS.

Ok, my point is, if I don't use that the program crashes the second I input the radius, farenheit temperature, or celcius temperature. At first I tried these source codes.

Circle Area Calculator:

Code:

#include <iostream>

using namespace std;

int main()

{
    float r;
    float pi = 3.14;
    float a;
    char i;

    cout<<"Please enter the radius of the circle.\n";
    cin>>r;
    a=(pi*r)*r;
    cout<<"The area of the circle is "<< a <<".\n";
    cout<<"\n";
    cout<<"If you would like to find the area of another circle press 'i'.\n";
    cin>>i;
    if (i=='i') {
       goto AreaCalc;
    }
cin.get();     
}


Temperature Converter:

Code:

#include <iostream>

using namespace std;

int main()

{
    float f;
    float c;
    char a;
   
    Converter:
    cout<<"If you would like to convert farenheit to celcius press 'f'. If you
    would like to convert celcius to farenheit press 'c'.\n";
    cin>>a;
    if (a=='f') {
       cout<<"\nPlease enter the temperature in farenheit.\n";
       cin>>f;
       c=(f-32)*5/9;
       cout<<"The temperature in celcius is "<< c <<".\n";
       cout<<"If you would like to convert another temperater
       press 'a'.\n";
       cin>>a;
       if (a=='a') {
          goto Converter;
       }
       else
       cin.get();
    }
    if (a=='c') {
   cout<<"Please enter the temperature in celcius.\n";
   cin>>c;
   f=(c*9)/5+32;
   cout<<"The temperature in farenheit is "<< f <<".\n";
   cout<<"If you would like to convert another temperature please click 'a'.\n";
   cin>>a;
      if (a=='a') {
      goto Converter;
      }
   }
cin.get();
}


Both of those crash the second I put in my first or second input. And I can't find why it would crash. So I was forced to make them the other way (I don't like that way...), can anyone see if they can help me with whatever bug is in the second source codes.

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
assaf84
Expert Cheater
Reputation: 0

Joined: 03 Oct 2006
Posts: 238

PostPosted: Sun Jun 24, 2007 11:54 pm    Post subject: Reply with quote

You declared an "int" function that doesn't return anything (main) . You should add "return 1;" or something at the end.

And, why the hell did u make so many variables?? You only had to make r and i (the area calculator)... For the pi u could use #define .

And another advice: DON'T USE GOTO, UNLESS THERE IS NO OTHER WAY. And usually (if not always) there is another way.
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Mon Jun 25, 2007 8:20 am    Post subject: Reply with quote

Ok, sorry, I don't know any other ways. So I used goto. I used variables for input and the real thing. I was to lazy to make a constant, plus even if I did (which I should have) I wouldn't do it the define. I would do it const float pi 3.14. And you aren't helping me. My question was how could I make my other programs not crash (the last 2 source codes) if possible.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
UnLmtD
Grandmaster Cheater
Reputation: 0

Joined: 13 Mar 2007
Posts: 894
Location: Canada

PostPosted: Mon Jun 25, 2007 8:32 am    Post subject: Re: Can I do this any other way. Reply with quote

oib111 wrote:


Code:

#include <iostream>

using namespace std;

int main()

{
    float r;
    float pi = 3.14;
    float a;
    char i;

    cout<<"Please enter the radius of the circle.\n";
    cin>>r;
    a=(pi*r)*r;
    cout<<"The area of the circle is "<< a <<".\n";
    cout<<"\n";
    cout<<"If you would like to find the area of another circle press 'i'.\n";
    cin>>i;
    if (i=='i') {
       goto AreaCalc;
    }
cin.get();     
}



Well, that code won't compile because of this: goto AreaCalc; You didn't defined the label, (you should use while loops) It will work otherwise...

Didn't look at the other one.

EDIT: Tested the 2nd code... everything went fine... I don't know if its your compiler but this
Code:
cout<<"If you would like to convert farenheit to celcius press 'f'. If you
    would like to convert celcius to farenheit press 'c'.\n";


Is going to give you hell a lot of errors, it needs to be in a line.

_________________
Back to top
View user's profile Send private message
TheSorc3r3r
I post too much
Reputation: 0

Joined: 06 Sep 2006
Posts: 2404

PostPosted: Mon Jun 25, 2007 11:21 am    Post subject: Reply with quote

Circle Area Calculator:

Code:
#include <iostream>
using namespace std;

#define PI 3.14
#define area(radius) PI*radius*radius

int main()
{
float r = 0; char c = '';
printf("Radius of circle: ");
scanf(%f, &r); // check the format there
printf("\nArea of circle: %f\nAgain (y/n): \n", area(r));
scanf(%c, &c);
if (c == 'y') main();
cin.get();
return 0;
}


Farenheit to Celcius (and vice versa):

Code:
#include <iostream>
using namespace std;

#define ftoc(t) (t-32)*5/9
#define ctof(t) t*9/5+32

int main()
{
float t = 0; char f = '', c = '', string[64]; memset(&string, 0, sizeof(string));
printf("Enter temperature: ");
scanf(%f, &t);
printf("\nWas that temperature in Farenheit or Celcius (f/c): ");
scanf(%c, &f);
string = (f == 'c' ? "Farenheit" : "Celcius"); // dunno if that's legal
printf("\nTemperature in %s is: %f", string, (f == 'c' ? ctof(t) : ftoc(t)));
printf("\nAgain (y/n): ");
scanf(%c, &c);
if (c == 'y') main();
cin.get();
return 0;
}


Untested and has no error handling.

@assaf84, what's wrong with goto?

_________________


Don't laugh, I'm still learning photoshop!
Back to top
View user's profile Send private message
appalsap
Moderator
Reputation: 0

Joined: 27 Apr 2006
Posts: 6753
Location: Pakistan

PostPosted: Mon Jun 25, 2007 11:31 am    Post subject: Reply with quote

The two times where gotos are appropriate:

1. Really long switch statement and you want to go to different cases.
2. If you want to break out of multiple chained IFs

Otherwise you can just make a loop and use continue/break, easier to read and helps the brain follow the logic.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites