neeed help with weird calculator program problem

ok this calculator is almost done. Just when i added sum stuff wat happened was i couldnt do division anymore. the calculator is not supposed to do infintes but even 6/3 was wierd please look at code. Also am i learning C++ right and wat is left to learn next after swtich stements. AND one more thing. can i put a code that if there is a infinite decimal to round it. And again another question wat are memory leeks cause i friends said that i should be learning java instead cause c++ has leaks and will crash my system. Woo htas it heeres my calkculator code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() 
{
     char loop = 'y';
     char operand;
     float f1;
     float f2;
     float f3;
     system("TITLE Calculator V3.52");
     system("COLOR 2");
     
     do 
     {     
           system("CLS");
           cout <<" Welcome to Calculator Program V3.52" << endl;
           cout << "Mind that this program cannot calculate infinite decimals"<< endl;
           cout << " Please type in your first integer or decimal: "; 
           cin >> f1;
           cout << " Now select a operation you wish to complete(-,+,*, or /): ";
           cin >> operand;
           cout << " Finally, type in your last integer or decimal: ";
           cin >> f2;
           cout << endl << endl;
           
           switch(operand)
           {              
                          case '+' :
                               f3 = f1 + f2;
                               cout << "The answer is " << f3 << endl;
                               cout << f1 << "+" << f2 << "=" << f3 << endl;
                          break;
                          
                          case '-' :
                               f3 = f1 - f2;
                               cout << "The answer is " << f3 << endl;
                               cout << f1 << "-" << f2 << "=" << f3 << endl;
                          break;
                          
                          case '*' :
                               f3 = f1 * f2;
                               cout << "The answer is " << f3 << endl;
                               cout << f1 << "*" << f2 << "=" << f3 << endl;
                          break;
                          
                          case '/' :
                               if( f2 = 0){
                                   cout << "That is a invalid operation" << endl;
                                   }
                               else{
                                    f3 = f1 / f2;
                                    cout << "The answer is " << f3 << endl;
                                    cout << f1 << "/" << f2 << "=" << f3 << endl;
                                    }
                          break;
                          
                          default :
                               cout << "That is a invalid operation" << endl;
                          break;
           }
           cout << "Would you like to start again? [y/n]: ";
           cin >> loop;
            
     } while(loop == 'y' || loop == 'Y');
     system("PAUSE");
     return 0;
}
First of all, memory leaks are when you create something dynamically, and you don't free the memory that it uses.

C++ doesn't have memory leaks. C++ does allow programmers to create them though:
1
2
3
4
5
6
7
8
9
int main (void)
{
  int x = 4;
  int n = x;
  int* p = new int[n]; //create an array of integers dynamically

  //don't have anymore code, but we need to delete the array to prevent memory leaks!
  delete[] p;
}


If you were to run that code several times, the OS should properly free the memory. However, if you don't include the delete[] line, it creates a leak because that memory is still allocated and it isn't available to any other program. Too many of those leaks, and it can make your computer really slow. Unless your program uses dynamic allocation, you shouldn't need to worry about memory leaks.

Java has no such thing because it uses a garbage collector. When a variable is no longer needed, the memory for it is automatically freed, making it available to other programs.

However, there are garbage collectors available for C++. Your friend probably has no idea that garbage collectors can be used with C++, and that is why he recommended Java. He wasn't wrong, but he wasn't completely correct either.

Infinite decimals should be handled by your hardware and the operating system already. You don't need to worry about that too much.

The problem you are dealing with is rounding error. Try using double instead of float. That may fix your problem.
OK!. Thanks that soolves everthing. If i got this straight memory leaks are only for arrays. But i still cant get my calc to work. 6/2 isnt infinite but the output is...
6/2=6 (which is wrong)
Memory leaks aren't only for arrays, but they are for pointers. The difference between a normal (statically allocated) array and a dynamically allocated array is the fact that the statically allocated array has a constant amount of memory taken up. With a dynamically allocated array, that isn't the case. As with all things that are dynamically allocated, things must be managed. The following code has nothing to do with arrays, but a memory leak is still introduced:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class example
{
  private:
    int data;
  public:
    example (void) :data(0) {}
    example (int d) :data(d) {}
};

int main (void)
{
  example* e = new example;
  //end of the program - notice there is no delete operator
}

There is no delete operator, so a memory leak is created. As you can see, memory leaks are created when you dynamically allocate something, but you don't free the memory.

As for your calculator, I'll look at it in a bit, but someone else will probably help before I get a chance to do that.
Iahev nomidea wat any of this means. I ahvnt learned about arrays and pointers, but i do know actionscript so i have an idea. I dont know what the dif beetween dynamioc and static is
The problem is in the case for division.
1
2
3
4
                          case '/' :
                               if( f2 = 0){
                                   cout << "That is a invalid operation" << endl;
                                   }

Where you have f2 = 0 this assigns 0 to f2 and evaluates to false - so the code contines and divided f1 by 0, resulting in infinity.
Simply change the '=' (assignment) to '==' (comparison).
It's a good idea to turn warnings on in the compiler. Had you done so, you would have caught the problem a long time ago.

For GCC (if you're using that), -Wall turns on all warnings.
Topic archived. No new replies allowed.