Thread: C++ Calculator Help

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    C++ Calculator Help

    Hello I'm new to C++ and was working on making a simple calculator... What I want it to have is at least 1 loop, switch statement, 1 array with [10]... i don't need a menu just something that you can type the operation and number to add,sub,mult, and divide... i'd also like it to keep displaying the current value...........any help would be apreciated...

    Thanks!!

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Heres code i have so far..

    Heres the basic code i have so far....

    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
      int a,b,res;
      char op;
      cout<<"Please Enter A Number and +,-,*,or/ then another Number-> ";
      while(cin>>a>>op>>b){
        switch(op){
        case'+':res = a  + b;
          break;
        case'-':res = a  - b;
          break;
        case'*':res = a * b;
          break;
        case'/':if(a == 0 || b ==0)
    	cout<<"Error: division by zero: ";
          else
    	res = a / b;
          break;
        default: cout<<"Error: "<<op<<" is not accepted\n";
        }
        cout<<res<<": ";
      }
      return 0;
    }

    Its just not what i want though... I want an array[10] and my switch and loop to be right... which i dont' think it is..

    Thanks!

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    It's only division by zero if b == 0.

Popular pages Recent additions subscribe to a feed