473,770 Members | 5,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

coding a simple calculator

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
=
the result is 15.

Iw would keep adding or subtracting... until the user enters '='

I want to loop this so that if the person would enter 'q' it would
quit.

Iam thinking maybe of a while loop in the beginning like this

while (scanf("%d", &num) == 1)

but after this I dont know where to go.

Dont post the whole code just need a little help getting started.

BTW: Iam still learning C from a book called C Primer Plus and I just
finished a chapter about if else switch() break. So I dont know much
but I think its enough for this simple project.
Nov 14 '05 #1
3 5212
* Xm******@verizo n.net (Paul) wrote:
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
=
the result is 15.

Iw would keep adding or subtracting... until the user enters '='

I want to loop this so that if the person would enter 'q' it would
quit.

Iam thinking maybe of a while loop in the beginning like this

while (scanf("%d", &num) == 1)

but after this I dont know where to go.


Read a line - check if it is a number or a known operater and put it
on a stack or refuse the entry.

If the user enters '=', then go through the stack and pop the elements
from the stack. May be this is not the correct way to do it and you
have to do some operations on the stack already when a operator is
entered.
hth, mabu

--
Are you Anonymous? Where? ... I don't think so ...

[ devnull{at}chao sfactory{dot}or g | http://www.chaosfactory.org/ ]
Nov 14 '05 #2
Paul writes:
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
=
the result is 15.

Iw would keep adding or subtracting... until the user enters '='

I want to loop this so that if the person would enter 'q' it would
quit.

Iam thinking maybe of a while loop in the beginning like this

while (scanf("%d", &num) == 1)

but after this I dont know where to go.


What you describe is an adding machine, not a calculator. It recognizes
integer numbers, +, - and =. You might assume an error free input to get
started. That is not a good assumption for a usable thing but I don't think
that is your goal, anyway. I think you could have an inner loop that has
(partial pseudocode):

do
get number
get operator
if (+ or -)
add to accumulator
else
display result note: assumed '='
set done flag to true
while not done

This does one series of adds and then quits, so it isn't quite what you
asked for. You could enclose this in a menu to get the 'q' thingy.
Providing for user errors will require that you parse the input yourself,
IOW the user inputs a string of characters and *you* do the conversion to
numbers etc.

If you decide to later do an actual calculator, I suggest you look into
reverse Polish notation (RPN). An RPN calculator is easier to code than the
usual (algebraic) calculator. K&R has a lengthy (for them) calculator
example.
Nov 14 '05 #3

"Paul" <Xm******@veriz on.net> wrote in message
I want to make a simple calculator program but dont know where > to get started. This is not GUI but a simple terminal program.
I am thinking maybe of a while loop in the beginning like this

while (scanf("%d", &num) == 1)

but after this I dont know where to go.

The first thing is that you want to do all your calulations in double. This
is no harder than using ints.

You just keep a running total, as a handheld calculator does.

Your user has got to enter numbers and operations alternately.

int main(void)
{
double x = 0; /* this is your accumulator */
char op = '+'; /* hold the operation, '+', '*' etc */
/* set as a dummy + to start */
char buff[1024]; /* your input buffer */

/* print out a message saying how to use the program */
/* run this do while loop until you get a q */
do
{
fgets(buff, 1024, stdin);

/* if op is 0 we are expecting an operation */
if(op == 0)
{
/* read a character from the input, which should be +-/*= or q*/
/* set op, or print x if you have = */
}
/* if op is non-zero we are expecting a long */
else
{
double y;

/* read in y. Take action if not a number */
if(sscanf(buff, "%lf", &y) != 1)
{
/* check for 'q' here */
printf("Must have a number\n");
/* continue */
}
/* do the arithmetic */
switch(op)
{
case '+':
case '-':
case '/':
case '*':
default:
/* default should never happen */
}
op = 0;
}
}
while( !strchr(buff, 'q'))

return 0;
}
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
6341
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
19
4031
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.*;
15
45144
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:
1
2942
by: Synapse | last post by:
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...
1
1713
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>
1
3333
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...
5
2187
Cowbie
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...
3
7297
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?
6
3491
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:
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
jinu1996
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...
1
10008
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,...
0
9873
tracyyun
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...
0
8891
agi2029
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...
0
6682
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();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.