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 


[C++] problem with code

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

Joined: 22 May 2007
Posts: 2221

PostPosted: Sun Jul 06, 2008 11:45 pm    Post subject: [C++] problem with code Reply with quote

In theory this should never end but for some reason it will some times end when I go to do another fuction and some times it wont
the app closes after I multiply or add or w/e.



Code:
//NegiCalculator
//Made by negima of CEF

#include <iostream>

int main()
{
   int functionNum, x, y;
   std::cout << "Hello, and welcome to NegiCalculator\n";
   std::cout << "I can do 4 main mathmatical functions\n";
   std::cout << "These functions are...\n";
   std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
   std::cout << "Please type one of the function numbers (1-4) to begin\n";
   std::cin >> functionNum;

   if (functionNum == 2)
   {
      std::cout << "Please enter a number\n";
      std::cin >> x;
      std::cout << "Please enter another number to subtract from your first number\n";
      std::cin >> y;
       std::cout << x << " subtracted by " << y << " equals " << x-y << "\n";
      std::cout << "What would you like to do next?\n";
      std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
    functionNum = 0;
   std::cin >> functionNum;

   }
 

   if (functionNum == 3)
   {
      std::cout << "Please enter two numbers to multiply\n";
      std::cin >> x;
      std::cin >> y;
      std::cout << x << " Multiplyed by "<< y << " equals " << x*y << "\n";
      std::cout << "What would you like to do next?\n";
   std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
    functionNum = 0;
   std::cin >> functionNum;
   }

   if (functionNum == 4)
   {
      std::cout << "Please enter a number, then another number to divide it by\n";
      std::cin >> x;
      std::cin >> y;
      std::cout << x << " Divided by " << y << " equals " << x/y <<"\n";
            std::cout << "What would you like to do next?\n";
   std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
    functionNum = 0;
   std::cin >> functionNum;


   }
   
   {
   if (functionNum == 1)
      std::cout << "Please enter two numbers to add\n";
   std::cin >> x;
   std::cin >> y;
   std::cout << "The sum of " << x << " and " << y << " equals " << x+y << "\n";
      
   std::cout << "What would you like to do next?\n";
   std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
    functionNum = 0;
   std::cin >> functionNum;
   }
}
Back to top
View user's profile Send private message Visit poster's website
oib111
I post too much
Reputation: 0

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

PostPosted: Sun Jul 06, 2008 11:52 pm    Post subject: Reply with quote

It should end in theory. You don't loop anything. At the end you ask for another choice but then you just stop, you don't return anything. Try this:

Code:

//NegiCalculator
//Made by negima of CEF

#include <iostream>

int main()
{
   int functionNum, x, y;
   BOOL bExit = FALSE;
   std::cout << "Hello, and welcome to NegiCalculator\n";
   std::cout << "I can do 4 main mathmatical functions\n";
   std::cout << "These functions are...\n";
   std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
   std::cout << "5. Quit\n";
   do {
   std::cout << "Please type one of the function numbers (1-4) to begin\n";
   std::cin >> functionNum;

   if (functionNum == 2)
   {
      std::cout << "Please enter a number\n";
      std::cin >> x;
      std::cout << "Please enter another number to subtract from your first number\n";
      std::cin >> y;
       std::cout << x << " subtracted by " << y << " equals " << x-y << "\n";
      std::cout << "What would you like to do next?\n";
      std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
    functionNum = 0;
   std::cin >> functionNum;

   }
 

   if (functionNum == 3)
   {
      std::cout << "Please enter two numbers to multiply\n";
      std::cin >> x;
      std::cin >> y;
      std::cout << x << " Multiplyed by "<< y << " equals " << x*y << "\n";
      std::cout << "What would you like to do next?\n";
   std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
    functionNum = 0;
   std::cin >> functionNum;
   }

   if (functionNum == 4)
   {
      std::cout << "Please enter a number, then another number to divide it by\n";
      std::cin >> x;
      std::cin >> y;
      std::cout << x << " Divided by " << y << " equals " << x/y <<"\n";
            std::cout << "What would you like to do next?\n";
   std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
    functionNum = 0;
   std::cin >> functionNum;


   }
   if(functionNum == 5) {
      bExit = TRUE;
   {
   if (functionNum == 1)
      std::cout << "Please enter two numbers to add\n";
   std::cin >> x;
   std::cin >> y;
   std::cout << "The sum of " << x << " and " << y << " equals " << x+y << "\n";
     
   std::cout << "What would you like to do next?\n";
   std::cout << "1. Add\n";
   std::cout << "2. Subtract\n";
   std::cout << "3. Multiply\n";
   std::cout << "4. Divide\n";
    functionNum = 0;
   std::cin >> functionNum;
   }
   } while(bExit == FALSE);
   return 0;
}


I didn't feel like fixing your formatting or coding habits Rolling Eyes

_________________


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
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Mon Jul 07, 2008 12:01 am    Post subject: Reply with quote

I first recommend cleaning up your work; it's quite messy. Second, please, use a switch instead of those IF's!

To fix your problem, just loop back to the menu after you choose which function.

P.S. oib111, I spotted an error in your correction @ function 5.
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 Jul 07, 2008 12:48 am    Post subject: Reply with quote

lol...i forgot the ending brace...
_________________


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
Chaosis13
Master Cheater
Reputation: 0

Joined: 14 Aug 2007
Posts: 372

PostPosted: Mon Jul 07, 2008 7:36 am    Post subject: Reply with quote

Try using switch like he said. And add this code at the top, under the includes:

Code:
using namespace std;


You might also consider making functions for add, sub, multi, div, and other functions in the future...
Back to top
View user's profile Send private message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Mon Jul 07, 2008 8:00 am    Post subject: Reply with quote

I notice you didn't have a return 0 in your code. Instead of using if (something == number) you can use the case statement instead, less messier and lines of coding.
_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Mon Jul 07, 2008 8:38 am    Post subject: Reply with quote

If you want your program to loop, you have to put in some sort of loop (for/while). If you don't put a return statement, the compiler will automatically put one in.
_________________
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