| View previous topic :: View next topic |
| Author |
Message |
Negima I post too much
Reputation: 6
Joined: 22 May 2007 Posts: 2221
|
Posted: Sun Jul 06, 2008 11:45 pm Post subject: [C++] problem with code |
|
|
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Jul 06, 2008 11:52 pm Post subject: |
|
|
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
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Mon Jul 07, 2008 12:01 am Post subject: |
|
|
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Mon Jul 07, 2008 12:48 am Post subject: |
|
|
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 |
|
 |
Chaosis13 Master Cheater
Reputation: 0
Joined: 14 Aug 2007 Posts: 372
|
Posted: Mon Jul 07, 2008 7:36 am Post subject: |
|
|
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 |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Mon Jul 07, 2008 8:00 am Post subject: |
|
|
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 |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Mon Jul 07, 2008 8:38 am Post subject: |
|
|
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 |
|
 |
|