sunslayer commented on a Page, First c++, calculator  -  Mar 07, 2010

this should have been posted in the C++ forums

int argc, char *argv[]

is unneeded as you do not use them at all and i don't think your using the cmd prompt, also

var x string;

var is an invalid data type and x and string should be reversed i.e., string x. however since your only using it to store +-*/ using char would be more efficient.

instead of using a loop you can use do... while, and in your switch statement you need to add a break after each statement or it will continue until the end or it reaches a break

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char x,a;
    double num1,num2;
    do
    {
        system("cls");
        cout << "instructions - type: " << endl;
        cout << "+ to add" << "- to subtract" << endl << "* to multiply" << endl;
        cout << "/ to divide" << endl << "\n\n";
        cin >> x;
        cout << "First Number: ";
        cin >> num1;
        cout <<"Second Number: ";
        cin >> num2;
        switch (x) {
          case '+':
               cout << num1 + num2 << endl;
               break;
          case '-':
               cout << num1 - num2 << endl ;
               break;
          case '*':
               cout << num1 * num2 << endl ;
               break;
          case '/':
               cout << num1 / num2 << endl;
               break;
          default:
               cout << "wrong input!\n";
        }
        cout << "Go again? (y/n) ";
        cin >> a;
    }   while(a=='y');
   return 0;
}
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.