Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2012, 01:25 AM   PM User | #1
va396053
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
va396053 is an unknown quantity at this point
Calculator Problem

Assignment:
Write a program, which will act as a simple four-function calculator. That is it will read a number, read an operator, read another number, then do the operation. The calculator works with integers and uses four functions: +, -, *, and /. After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation. If the user enters a C the result is cleared and then the user starts entering a new number. If the user enters an X, the calculator is turned off. The various input values (i.e. numbers, operators, commands) will be followed by the ENTER key. Your program should prompt the user on what the user is to do. The commands C and X may be entered in place of an operator

This is what I did. I am not sure whether it is right or not. Can anyone help me with this?
Thanks

#include <iostream>

using namespace std;

#include <stdlib.h>

void main()
{
bool InvalidOp;
int Number1;
int Number2;
char op;

cout << "Please enter a number and press enter: ";
cin >> Number1;

cout << "Please enter another number and press enter: ";
cin >> Number2;

cout << "Please enter a a valid operator, 'c' to clear, or 'x' to turn off and press ENTER : ";
cin >> op;
do {
cout << "Enter an operator [+-*/ccXx]: ";
cin >> Op;
InvalidOp = false;
switch (op)
{
case '+':
cout << "The result is: " << Number1 + Number2 << endl;
break;

case '-':
cout << "The result is: " << Number1 - Number2 << endl;
break;

case '*':
cout << "The result is: " << Number1 * Number2 << endl;
break;

case '/':
cout << "The result is: " << Number1 / Number2 << endl;
break;

case 'C':
case 'c':
break;

case 'X':
case 'x':
cout << "Bye" << endl;
exit (0);

default:
cout << "Invalid operator" << endl;
InvalidOp = true;
}
} while (InvalidOp);

}
va396053 is offline   Reply With Quote
Old 09-25-2012, 01:41 AM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
http://www.anaesthetist.com/mnm/javascript/calc.htm
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:27 PM.


Advertisement
Log in to turn off these ads.