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=quotie nt++;
}
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..
1 2934 DeMan 1,806
Top Contributor
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Paul |
last post by:
I want to make a simple calculator program but dont know where to get
started. This is not GUI but a simple terminal program.
It would get input like this
Enter number:
5
+
10
|
by: firstcustomer |
last post by:
Hi,
Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that
someone will be able to point me to a ready-made solution to my
problem!
A friend of mine (honest!) is wanting to have on his site, a Javascript
Calculator for working out the cost of what they want, for example:
1 widget and 2 widglets = £5.00
|
by: TexasNewbie |
last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number.
//GUI Calculator Program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
|
by: colinNeedsJavaHelp |
last post by:
I am attempting to program a very basic calculator. The program simply needs to prompt the use to input the computation i.e. 2 * 5
The program needs to compute this and display the result as "The result is: 10"
I think my problem comes when I need to tell java to compute the users input. I do not know what function or method I need to use. If anyone can help I would appreciate it.
Thanks
This is what I have so far:
|
by: Bl00dFox |
last post by:
Hi
I am making a simple program to calculate interest. At the beginning when the user has to pick 1 or 2 (to select simple or compound interest respectively), if the user enters a letter (eg, a) the program goes haywire (the main repeats over and over again).
Is there a way to limit the user in entering only numbers, not letters or characters? Here is the code:
// Interest calculator, by Bl00dFox
#include <iostream>
| |
by: gzeng |
last post by:
Hi Everybdy:
I am a beginner in C#. I'd like to write a simple online calculator in C#. This calculator will ask a user to input two numbers and then add them and display the result. So, it has two text boxes for a user to input two numbers. It also has a button for the user to get the result after adding these two numbers from input. Can somebody write such a program in C#? I think the program is small.
How do I make it online so that...
|
by: Cowbie |
last post by:
Hello everyone,
I'm very new to PHP and I'm keen to learn how it all works. I've been looking for help all day and reading tutorials etc which is how I came accross this forum - so here's hoping for some answers :)
First off I'll tell you what I'm doing. I'm making a calculator of some form which will do some simple calculations on the numbers entered into a form. For example, adding, multiplying etc. At the moment just to get me going I...
|
by: kimiraikkonen |
last post by:
Hi there,
I want to begin understanding how class libraries are written under
VB.NET and how can i call them under my executable project. For
example think an arithmetic calculator includes only plus, minus,
division, multiplying functions.
Yes it may not be necessary but how can i seperate each arithmetic
funtion into per class library and call them under my executable
project?
|
by: NoviceJava |
last post by:
I'm new to JAVA and need some help writing a simple calculator program.
Here are the instructions:
-expects espression with 2 operands together with either +, -, *, or / operator
-espects only positive input
-assumes all real #'s
-assumes operands and operators in expression are separated with spaces
here is what I have so far:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |