Hello,
I am very new to all of this and I need a little help. I am supposed to create a simple calculator and here is what I have:

/* Specification:
This program is a simple calculator  */

#include "stdafx.h"
#include <iostream>

using namespace std;

namespace calculation{
     enum menuOptions {Add, Sub, Mult, Div, Mod, Exit};
}

int main (){
         //ask the user for numbers to be calculated
       int numOne = 0;
       int numTwo = 0;
       cout << endl << "Welcome to the Simple Calculator!\n";
       cout << endl << "Enter Your First Number: ";
       cin >> numOne;
       cout << "Enter Your Second Number: ";
       cin >> numTwo;
       cout << endl << endl;

        //ask the user for desired operation
        int operationSelection = 0;

        cout << "Choose a Calculation Operation:\n\n";
        cout << calculation::Add << " - Addition\n";
        cout << calculation::Sub << " - Subtraction\n";
        cout << calculation::Mult << " - Multiplication\n";
        cout << calculation::Div << " - Division\n";
        cout << calculation::Mod << " - Modulus\n";
        cout << calculation::Exit << " - Exit\n";
        cout << "Enter a number: ";
        cin >> operationSelection;

        if (operationSelection < 0 || operationSelection > 5){
                cout << endl << "Invalid choice - program terminated\n\n";
        }
      else {
            //perform calculations
                struct calcType {int add; int sub; int mult; int div; int mod;};
                calcType myCalc;
                myCalc.add = numOne + numTwo;
                myCalc.sub = numOne - numTwo;
	myCalc.mult = numOne * numTwo;
	myCalc.div = numOne / numTwo;
	myCalc.mod = numOne % numTwo;
             switch (operationSelection) {
                           case calculation::Exit:
                                    cout << endl << "You choose to exit the program\n\n"; break;
                           case calculation::Add:
                                    cout << endl 
                                        << numOne << " + " << numTwo << " = " 
 		        << myCalc.add << "\n\n"; break;
                          case calculation::Sub:
                                    cout << endl
                                           << numOne << " - " << numTwo << " = "
		           << myCalc.sub << "\n\n"; break;
                          case calculation::Mult:
                                    cout << endl
                                          << numOne << " x " << numTwo << " = "
                                          << myCalc.mult << "\n\n"; break;
                          case calculation::Div:
                                    cout << endl
                                          << numOne << " / " << numTwo << " = "
                                          << myCalc.div << "\n\n"; break;
	         case calculation::Mod:
                                    cout << endl
                                         << numOne << " % " << numTwo << " = "
                                         << myCalc.mod << "\n\n"; break;
                                     
                          default:
                                    cout << endl << "Not a valid choice!\n\n";
                 }

                cout << endl << endl << "end of program\n\n";
         }

return 0;

}

Now I know that it does not produce a decimal answer and I do not know how to get it to do that ( ex. 4 / 8 = .5) it only produces a zero unless it's a whole number. Please help . . and also if there is anything else wrong please mention . . I am at a loss here.

Thanks!

Make the div element of your structure of type double. Then cast one (or both of numOne and numTwo to double before performing the calculation. struct calcType {int add; int sub; int mult; double div; int mod;}; myCalc.div = (double)numOne/(double)numTwo;

Edited 7 Years Ago by jonsca: n/a

Make the div element of your structure of type double. Then cast one (or both of numOne and numTwo to double before performing the calculation. struct calcType {int add; int sub; int mult; double div; int mod;}; myCalc.div = (double)numOne/(double)numTwo;

Thank you . . extremely helpful :)

This article has been dead for over six months. Start a new discussion instead.