c++ Stuck making a calculator

April 23, 2010 at 13:18:46
Specs: Windows Vista
I'm making a simple calculator in C++ and my problem is this: It always does addition first and anything after addition has to go through the previous operations first, e.g, i want to divide, but first i have to do an addition, subtraction and multiplication operation first before the divide option appears, i'll post the code, if anyone can edit the code to make it skip the other 3 that would be great thanks, here's the code:

// Simple calculator.cpp : main project file.

#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;

int main()
{
const int add = 1;
const int subtract = 2;
const int multiply = 3;
const int divide = 4;
char whichoperation;
int firstnumber;
int secondnumber;
int resultadd;
int resultminus;
int resultmultiply;
int resultdivide;

cout<< "Please enter whether you would like to multiply, divide, subtract, or add, 1 for add, 2 for divide, 3 for multiply and 4 for divide:\n";
cin>> whichoperation;
if (whichoperation = add) {
cout<< "(Addition) Please enter the first number:\n";
cin>> firstnumber;
cout<< "Now please enter the number to add to it:\n";
cin>> secondnumber;
resultadd = firstnumber + secondnumber;
cout<< "\n The result is: " << resultadd << ".\n";
}
if (whichoperation = subtract) {
cout<< "(Subtraction) Please enter the first number:\n";
cin>> firstnumber;
cout<< "Now please enter the number to subtract from it:\n";
cin>> secondnumber;
resultminus = firstnumber - secondnumber;
cout<< "\n The result is: " << resultminus << ".\n";
}
if (whichoperation = multiply) {
cout<< "(Multiply) Please enter the first number:\n";
cin>> firstnumber;
cout<< "Now please enter the number to multiply it by:\n";
cin>> secondnumber;
resultadd = firstnumber * secondnumber;
cout<< "\n The result is: " << resultmultiply << ".\n";
}
if (whichoperation = divide) {
cout<< "Please enter the first number:\n";
cin>> firstnumber;
cout<< "Now please enter the number to divide the first number by:\n";
cin>> secondnumber;
resultadd = firstnumber / secondnumber;
cout<< "\n The result is: " << resultdivide << ".\n";
}
return 0;
}

There is no spoon, I couldn't be bothered to upload one.


See More: c++ Stuck making a calculator

Report •


#1
April 23, 2010 at 19:42:51
i'm really lousy at C, but i think you're trying to compare apples and oranges (integers and characters that is):
const int add = 1;
...
char whichoperation;
cin>> whichoperation;
if (whichoperation = add) {
you could make whichop into a integer and it might fix it.
there's a few other minor errors, but i'll let you fish them out of the pond. :-)

Report •

#2
April 24, 2010 at 04:30:08
If your if statements, you're not doing a comparison ==, you're doing an assignment =.

Report •

#3
April 24, 2010 at 08:52:32
It didnt fix it when i changed the whichop to an int, should i use gotos?

There is no spoon, I couldn't be bothered to upload one.


Report •

Related Solutions

#4
April 24, 2010 at 11:42:07
have you made the repairs to the ifs, as Razor pointed out?
If (xx==yy) {
(one of those fish still swimming around in there)

Report •

#5
April 27, 2010 at 03:20:49
use a switch case like this

switch(whichoperator)
{
case 1: // add--
break;
case 2: //subtract--
break;
.
.
.
default: cout<<"wrong choice";
}

use single quotes '1','2' if u r using char as the switch parameter.


Report •

#6
April 27, 2010 at 09:44:11
Your program really only needs three ints or perhaps an array with three elements; a char for the operation, and a char[] to read in the input from the user... the latter will help prevent stream errors from bad data.

#include <cstring> // for sscanf
...
int main(void) {
...
   char usrinp[20] = { 0 };  
   int data[3] = { 0 };
   char op;

   cout << "Enter an operation (eg: 2 * 5): ";
   cin.getline(usrinp, sizeof usrinp);

    if((sscanf(usrinp, "%d %c %d", &data[0], &op, &data[1])) != 3) {
      cout << "Error processing operation" << endl;
    } else {
       if(op == '/' && data[1] == 0) { 
          // div by zero error
       } else {
          // calculate and display answer
       }
    }   
...

However, if you wish to read numeric data directly, then always check the status of the fail bit on cin... otherwise, your program may take on a life of its own... especially when streams fail inside of loops. :)

cin >> int_varible;
if(cin.fail()) {
   cin.clear();
   cin.ignore(1);
   cout << "bad data" << endl;
}

HTH


Report •


Ask Question