By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Simple Calculator - EDS

P: 26
Hello...
We were asked to create a simple calculator program in our C++ subject by using loops only. i have a problem in creating a loop in the multiplication and division operation so please can anyone help me on this please. and also during the operation selection, if ill enter a character it wont go back to the main program. by the way, my compiler is Dev-C++.
I need help badly..here's my code below...


#include <iostream.h>
#include <stdlib.h>
#include <dos.h>

int main()
{
Begin:
system("CLS");
int options;
float fnum, snum, i, sum, diff, product, quotient;
cout << "Simple Calculator\n\n"; /*Operation Option*/
cout << "1 - Addition" << endl;
cout << "2 - Subtraction" << endl;
cout << "3 - Multiplication" << endl;
cout << "4 - Division" << endl;
cout << "5 - Quit Program\n\n";
cout << "Please select your operation: ";
cin >> options;

switch (options) {

/*Addition*/
case 1: cout << "You selected the ADDITION OPERATION.\n\n";
cout << "Enter the first number: ";
cin >> fnum;
cout << "Enter the second number: ";
cin >> snum;
for(i=fnum; i>=0; i--) {
sum=snum++;
}
cout << "Answer is " << sum << endl;
system("PAUSE");
goto Begin;
break;

/*Subtraction*/
case 2: cout << "You selected the SUBTACTION OPERATION.\n\n";
cout << "Enter the first number: ";
cin >> fnum;
cout << "Enter the second number: ";
cin >> snum;
for(i=snum; i>=0; i--) {
diff=fnum--;
}
cout << "Answer is " << diff << endl;
system("PAUSE");
goto Begin;
break;

/*Multiplication*/
case 3: cout << "You selected the MULTIPLICATION OPERATION.\n\n";
cout << "Enter the first number: ";
cin >> fnum;
cout << "Enter the second number: ";
cin >> snum;
for(i=snum; i<=fnum; i++) {
product=fnum++;
}
cout << "Answer is " << product << endl;
system("PAUSE");
goto Begin;
break;

/*Division*/
case 4: cout << "You selected the DIVISION OPERATION.\n\n";
cout << "Enter the first number: ";
cin >> fnum;
cout << "Enter the second number: ";
cin >> snum;
for(i=0; i<=fnum; i++) {
for(i=0; i<snum; i++)
quotient=quotient++;
}
cout << "Answer is " << quotient << endl;
system("PAUSE");
goto Begin;
break;

/*Exit Program*/
case 5: exit(0);

default: cout << "\n\nPlease enter the correct option." ;
system("PAUSE");
goto Begin;
break;

}
system("PAUSE");
return 0;
}


im still working out of this program but as of now i need help badly.
i appreciate your help and thank you very much.
im looking forward on it..
Feb 25 '07 #1
Share this Question
Share on Google+
1 Reply


DeMan
100+
P: 1,806
it may help to think about multiplication as follows:

a x 4 = (0+ a + a +a +a)

THus we can reduce it to an addition problem, where for any (a x b) we need to add a to 0 b times. Try implementing a loop to do this. Post again if your stuck.

Division feels a bit more tedious (but is equally simple) [I am assuming this is integer division, so 11/2=5 not 5.5].

a / b is the number of times we can subtract b from a without going below 0.

This again will need a nested loop (given you have to increment/decrement values by 1). Think about how you would implement this and post again if you run into difficulty.

I assume you are not allowed to create methods, otherwise you could create addition and subtraction, and reuse them for multiplication and divisio.
Feb 25 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.