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 


[Need Help] Calling back to "main()" in C++
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
M3KillU
Grandmaster Cheater
Reputation: 0

Joined: 06 Apr 2007
Posts: 793
Location: California, USA

PostPosted: Fri Nov 09, 2007 6:09 pm    Post subject: [Need Help] Calling back to "main()" in C++ Reply with quote

Hey, I'm just starting to learn C++ and I was wondering how you would call back to main...
One more thing, you may or may not recognize
Code:
#include <vcl.h>

My compiler: CodeGear RAD Studio C++ 2007 - Made by Borland
automatically inserts that when you want to make something in command prompt (or maybe anything).


I am making a calculator (Im Command Prompt) and I need to be able to call back to main after using an if comparison...

Here is the script:
Code:

//---------------------------------------------------------------------------
#include <iostream>
#include <vcl.h>
#pragma hdrstop
#pragma argsused
//---------------------------------------------------------------------------
using namespace std;
int Choice, a, b, c;

int Add(int num1, int num2)
{
   return (num1 + num2);
}

//----------------------------------

int Sub(int num3, int num4)
{
   return (num3 - num4);
}
 
//----------------------------------

int Mult(int num5, int num6)
{
   return (num5 * num6);
}

//----------------------------------

int Div(int num7, int num8)
{
   return (num7 / num8);
}

int main()
{
   cout << "What do you want to do? Do you want to multiply, divide, subtract, or add?\n";
   cout << "Well.. please type in a number...\n";
   cout << "Add =\t\t" "1" << endl << "Subtract = \t" "2" << endl << "Multiply =\t" "3" << endl << "Divide =\t" "4\n\n";
   cin >> Choice;
   cout << endl << endl;

   //------------------------If Comparisons-------------------------------------

   if (Choice == 1) {
      cout << "You chose to Add!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "+ \n";
      cin >> b;
      c=Add(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice == 2) {
      cout << "You chose to Subtract!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "- \n";
      cin >> b;
      c=Sub(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice == 3) {
      cout << "You chose to Multiply!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "x \n";
      cin >> b;
      c=Mult(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice == 4) {
      cout << "You chose to Divide!\n\n";
      cout << "Enter a number,  press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "(divided by) \n";
      cin >> b;
      c=Div(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice > 4) {
      cout << "That doesn't work.\n";
      cout << "Pick a number between 1 and 4\n";
   }
   if (Choice < 1) {
      cout << "That doesn't work.\n";
      cout << "Pick a number between 1 and 4\n";
   }
   system("PAUSE");
   return 0;
}


I need to call back to main here:
Code:


   if (Choice > 4) {
      cout << "That doesn't work.\n";
      cout << "Pick a number between 1 and 4\n";
                //Call to main() here
   }
   if (Choice < 1) {
      cout << "That doesn't work.\n";
      cout << "Pick a number between 1 and 4\n";
           //Call to main() here


If that doesn't work... I'm going to try turning all of the code in main... to... "void Calc()"

then setting main below that as:
Code:

main()
{
   Calc();
   system("PAUSE");
   return 0;
}


The original main will be the exact same thing (if it works) and will just be called "void Calc()"

So... main question is just... can i call back to main? if so... how?

----Oh... and I only know basic C++ at the moment.

Thanks, hope you can help me.

_________________
This is where my siggy goes!


Last edited by M3KillU on Fri Nov 09, 2007 6:19 pm; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

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

PostPosted: Fri Nov 09, 2007 6:18 pm    Post subject: Reply with quote

You could make it a loop.
_________________


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
M3KillU
Grandmaster Cheater
Reputation: 0

Joined: 06 Apr 2007
Posts: 793
Location: California, USA

PostPosted: Fri Nov 09, 2007 6:20 pm    Post subject: Reply with quote

How do I do that?
_________________
This is where my siggy goes!
Back to top
View user's profile Send private message MSN Messenger
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Fri Nov 09, 2007 6:21 pm    Post subject: Reply with quote

While/For/Goto/Timer. (From best to lame coding... I dunno if timers are lamer than goto's Rolling Eyes )
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Fri Nov 09, 2007 6:23 pm    Post subject: Reply with quote

Dont Forget the Do While Loop, thats always good Smile

goto's sooo lammemee

_________________
Back to top
View user's profile Send private message
M3KillU
Grandmaster Cheater
Reputation: 0

Joined: 06 Apr 2007
Posts: 793
Location: California, USA

PostPosted: Fri Nov 09, 2007 6:24 pm    Post subject: Reply with quote

Symbol... I'm glad you could help but... Where would I put that? lol

I'm only barely learning C++

_________________
This is where my siggy goes!
Back to top
View user's profile Send private message MSN Messenger
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Fri Nov 09, 2007 6:25 pm    Post subject: Reply with quote

Code:

for (int i = 0; i < 1; i ++)
{
        //Do some stuff

        :
        :
        :

        //Uh oh, we need to leave
        break;
        //Or if you want it to loop still after this ( even though you won't need to in this, it's just for future references )
        //continue;
}
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Fri Nov 09, 2007 6:25 pm    Post subject: Reply with quote

Code:
#include <iostream>
using namespace std;

int main()
{
   int      iNum   = 0;
   bool   bLoop   = true;

   while( bLoop )
   {
      cout << "Enter A Number: ";
      cin >> iNum;

      switch( iNum )
      {
      case 1:
         cout << "You entered 1" << endl;
         break;
      case 2:
         cout << "You entered 2" << endl;
         break;
      case 3:
         cout << "You entered 3" << endl;
         break;
      case 4:
         bLoop = false;
         break;
      }
   }

   cin.ignore();
   cin.sync();
   return 0;
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
M3KillU
Grandmaster Cheater
Reputation: 0

Joined: 06 Apr 2007
Posts: 793
Location: California, USA

PostPosted: Fri Nov 09, 2007 6:30 pm    Post subject: Reply with quote

What do:
bLoop
while
switch
case
break
cin.ignore()
cin.sync()

....
do?

_________________
This is where my siggy goes!
Back to top
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Fri Nov 09, 2007 6:33 pm    Post subject: Reply with quote

M3KillU wrote:
What do:
bLoop
while
switch
case
break
cin.ignore()
cin.sync()

....
do?


If you don't know what While, Switch, Case, and Break are you should go back and learn the basics before you start doing anything as they will be use A LOT in any program you create.

Check out this site for most of the basics, google the rest that you come across:
http://www.cplusplus.com/doc/tutorial/

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
M3KillU
Grandmaster Cheater
Reputation: 0

Joined: 06 Apr 2007
Posts: 793
Location: California, USA

PostPosted: Fri Nov 09, 2007 6:34 pm    Post subject: Reply with quote

I'm still learning C++
I just thought there would be an easier way to call... oh well... Looks like it's back to the book (Sam's Teach Yourself C++ in 21 Days)

EDIT:
I made another script.

Code:
//---------------------------------------------------------------------------
#include <iostream>
#include <vcl.h>
#pragma hdrstop
#pragma argsused
//---------------------------------------------------------------------------
using namespace std;
int Choice, a, b, c;

int Add(int num1, int num2)
{
   return (num1 + num2);
}

//----------------------------------

int Sub(int num3, int num4)
{
   return (num3 - num4);
}
 
//----------------------------------

int Mult(int num5, int num6)
{
   return (num5 * num6);
}

//----------------------------------

int Div(int num7, int num8)
{
   return (num7 / num8);
}

void Calc()
{
   cout << "You messed up... let's try again...\n\n";
   cout << "What do you want to do? Do you want to multiply, divide, subtract, or add?\n";
   cout << "Well.. please type in a number...\n";
   cout << "Add =\t\t" "1" << endl << "Subtract = \t" "2" << endl << "Multiply =\t" "3" << endl << "Divide =\t" "4\n\n";
   cin >> Choice;
   cout << endl << endl;

   //------------------------If Comparisons-------------------------------------

   if (Choice == 1) {
      cout << "You chose to Add!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "+ \n";
      cin >> b;
      c=Add(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice == 2) {
      cout << "You chose to Subtract!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "- \n";
      cin >> b;
      c=Sub(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice == 3) {
      cout << "You chose to Multiply!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "x \n";
      cin >> b;
      c=Mult(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice == 4) {
      cout << "You chose to Divide!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "(divided by) \n";
      cin >> b;
      c=Div(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice > 4) {
      cout << "That doesn't work.\n";
      Calc();
   }
   if (Choice < 1) {
      cout << "That doesn't work.\n";
      Calc();
   }
}

int main()
{
   cout << "What do you want to do? Do you want to multiply, divide, subtract, or add?\n";
   cout << "Well.. please type in a number...\n";
   cout << "Add =\t\t" "1" << endl << "Subtract = \t" "2" << endl << "Multiply =\t" "3" << endl << "Divide =\t" "4\n\n";
   cin >> Choice;
   cout << endl << endl;

   //------------------------If Comparisons-------------------------------------

   if (Choice == 1) {
      cout << "You chose to Add!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "+ \n";
      cin >> b;
      c=Add(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice == 2) {
      cout << "You chose to Subtract!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "- \n";
      cin >> b;
      c=Sub(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice == 3) {
      cout << "You chose to Multiply!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "x \n";
      cin >> b;
      c=Mult(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice == 4) {
      cout << "You chose to Divide!\n\n";
      cout << "Enter a number, press \"Enter\", then enter another. Push \"Enter\" when you are finished: \n";
      cin >> a;
      cout << "(divided by) \n";
      cin >> b;
      c=Div(a,b);
      cout << "Your answer is: " << c << endl << endl;
   }
   if (Choice > 4) {
      cout << "That doesn't work.\n\n\n";
      Calc();
   }
   if (Choice < 1) {
      cout << "That doesn't work.\n\n\n";
      Calc();
   }
   system("PAUSE");
   return 0;
}
//---------------------------------------------------------------------------


It works.

_________________
This is where my siggy goes!
Back to top
View user's profile Send private message MSN Messenger
Hackerdevelopment
Advanced Cheater
Reputation: 0

Joined: 11 Oct 2007
Posts: 55

PostPosted: Fri Nov 09, 2007 8:15 pm    Post subject: Reply with quote

Wow! It must b hard to make a calculator.
_________________
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Fri Nov 09, 2007 8:18 pm    Post subject: Reply with quote

M3KillU wrote:
I'm still learning C++
I just thought there would be an easier way to call... oh well... Looks like it's back to the book (Sam's Teach Yourself C++ in 21 Days)

EDIT:
I made another script.

Code:
...


Just a note, you name your parameters in sequential order like it's needed to be like that. Meaning you have things like:

Code:
int Add(int num1, int num2){...}
int Sub(int num3, int num4){...}


You don't need to name them like 1 2 3 4 and so on. A parameter is function wide only. Only that function has access to that parameter.

You can safely setup your functions like:

Code:
int Add(int num1, int num2){...}
int Sub(int num1, int num2){...}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
M3KillU
Grandmaster Cheater
Reputation: 0

Joined: 06 Apr 2007
Posts: 793
Location: California, USA

PostPosted: Tue Nov 13, 2007 5:03 pm    Post subject: Reply with quote

Oh, that is sweet! I thought I would have some lame error if I tried that. Thanks for the info.

Hackerdevelopment wrote:
Wow! It must b hard to make a calculator.

It's not that hard. Just took a little bit of if comparisons, and some basic C++ knowledge. This is in command prompt... I want to make an actual window, but, I don't have enough knowledge for that, I also don't know how to program the buttons that you can make in the compiler I have.

Thanks for the help everyone

_________________
This is where my siggy goes!
Back to top
View user's profile Send private message MSN Messenger
TheSorc3r3r
I post too much
Reputation: 0

Joined: 06 Sep 2006
Posts: 2404

PostPosted: Tue Nov 13, 2007 8:25 pm    Post subject: Reply with quote

Umm.. if you want to call main:

main();

Lol

btw, if you want to program GUIs, look at www.functionx.com/win32

_________________


Don't laugh, I'm still learning photoshop!
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
Goto page 1, 2  Next
Page 1 of 2

 
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