Here is my code
excuse me if it seems a bit off
i've only been coding for a day :)
What I want it to do is in the comments at the top

/*======================================================================
IN THIS PROGRAM I WILL TRY AND SCRIPT A CONVENTIONAL CALCULATOR
WITH THE USER ENTERING THE NUMBERS THEY WISH TO MULTIPLY ADD ECT.
ALSO I WILL MAKE A CANCEL FUNCTION WHICH ERASES OR LOOPS THE PROGRAM.
JUST TO CLARIFY THIS PROGRAM IS COMPLETELY MADE UP AND WAS MADE BY ME
WITH NO HELP WHATSOEVER.
------------------------------------------------------------------------
31/08/2010 13:00 - STARTED
31/08/2010 15:28 - FINISHED
HAS SOME FLAWS IF YOU TYPE 'C' INTO WHERE IT ASKS FOR A NUMBER IT GOES CRAZY.
ALSO SOME FUNCTIONS ARE NOT COMPLETE SUCH AS THE CANCEL FUNCTION, I WANT 
IT TO CLOSE THE PROGRAM WHEN 'C' IS PRESSED.
ALSO THE FUGTURE MODULUS FUNCTION IS YET TO BE CREATED.
------------------------------------------------------------------------
31/08/2010 - 16:24 STARTED
31/08/2010 - 16:34 FINISHED
STILL CAN'T GET FUNCTIONS TO WORK SUCH AS MODULUS AND CANCEL.
------------------------------------------------------------------------
BY CHRIS FORGEARD
========================================================================*/

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


//I CHOSE DOUBLES JUST INCASE USER CHOOSES TO USE DECIMAL PLACES
double number;
double number_2;
char cancel = 'c';
double result_times;
double result_add;
double result_minus;
double result_divide;

int main()
{
    cout << "Welcome to the calculator!\n" << endl;
    
//=====================================================================
//THE CALCULATOR LOOP
    while (cancel == cancel)
    {
    cout << "Please begin by entering your first number\n" << endl;
    cin >> number;
    cout << "Now enter your second number please\n" << endl;
    cin >> number_2;

//---------------------------------------------------------------------
//OPTIONS    
    cout << "Now what would you like to do?\n" << endl;
    cout << "1: multiply" << endl;
    cout << "2: addition" << endl;
    cout << "3: minus" << endl;
    cout << "4: divide"<< endl;
    cout << "5: cancel" << endl;
    cout << "6: coming soon (modulus)\n" << endl;
//---------------------------------------------------------------------
//SETTING UP CHOICES   
    int choice;
    cout << "\n\nChoice: ";
    cin >> choice;

//---------------------------------------------------------------------
//THE SWITCH PROGRAM (DEPENDS ON CHOICE WHAT PATH PROGRAM TAKES)   
    switch (choice)
    {
    case 1:
         cout << "You have chose to multiply your numbers" << endl;
         result_times = number * number_2;
         cout << "Your answer is: " << result_times << endl;
         break;
    case 2:
         cout << "You have chose to add your numbers" << endl;
         result_add = number + number_2;
         cout << "Your answer is: " << result_add << endl;
         break;
    case 3:
         cout << "You have chose to take away your numbers" << endl;
         result_minus = number - number_2;
         cout << "Your answer is: " << result_minus << endl;
         break;
    case 4:
         cout << "You have chose to divide your numbers" << endl;
         result_divide = number / number_2;
         cout << "Your answer is: " << result_divide << endl;
         break;
    case 5: 
         cout << "You have chose to cancel!" << endl;
         break;
              
    default:
            cout << "I'm sorry but that option is not available!" << endl;
            break;
    } 
}

//END OF LOOP
//================================================================================

    return 0;
}

If you want variable choice to contain either a number or a chracter then you will have to change its data type to char instead of int. Then the cases would become

bool done = false
while( done == false)
{
  // display menu here
  char choice;
  cin >> choice;
  switch(choice)
  {
    case '1': // blabla break;
    case '2': // blabla break;
    case 'c': done = true; break;
  }
}

A couple other things. Take a look at your while loop. while (cancel == cancel) . cancel is always going to equal cancel. This should probably be changed to while (true) , or while (cancel == 'c') , or something like that. The other thing is that a modulus operation would be no different to do than any of the other functions you have. Just number % number_2 .

Edited 6 Years Ago by embooglement: n/a

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