473,791 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with a c++ calculator

Hi,
i am new in c++ and i need some help with a program
i need to do a universal unit converter. this calculator can only do :
+, *, /, and -.
it should read a string (the equation) and ask you to put the value of
that string. i wasn't able to do that, but i could do with
characters...
for ex :
if i write: CelsiusTemp * ConversionFacto r + base....it should ask me
to input the value of celsiustemp and ...
i was only able to do a+b/d+c and input the value of each one.
and it still didn't work well.
TY for help and any advice!
*************** *************** *************
the file:
#include <iostream>
#include <string>

using namespace::std;
using std::string;
int main()
{ string s;
//char *Exp;
int state = 0; //initial state
int site = 0;
int var1 = 0, var2 = 0, operation = 0;
char ch;

cout<<"Enter the equation you want to use: ";
getline(cin, s);
do{
for(int i=0;i<=s.size() ;i++)
{
ch=s[site];
site++;
if(ch==' '){
if(state==0)//looking for a variable
{
if(ch>='a' && ch<='z')//reading first character
{
cout<<"Enter value of "<<ch<<": ";
cin>>var1;
state=1;
}
else
{
cout<<"Error: No variable."<<end l;
state=-1;
}
}//end state 0
else if(state==1) // reading first operation:
{
if(ch=='*')
{
operation=1;
state=2;
}
else if(ch=='/')
{
operation=2;
state=2;
}
else if(ch=='-')
{
operation=3;
state=2;
}
else if(ch=='+')
{
operation=4;
state=2;
}
else
{
cout<<"Error: Unknown
Operation."<<en dl;
state=-1;
}
}//end reading first operation--End of state 1
else if(state==2)//reading second variable
{
if(ch>='a' && ch<='z')//reading 2nd character
{
cout<<"Enter value of "<<ch<<": ";
cin>>var2;
state=3;
}
else
{
cout<<"Error: No variable."<<end l;
state=-1;
}
}//end of state 2: reading second variable
else if(state==3)
{
if(operation==1 ) //*
{
var1=var1*var2;

}
else if(operation==2 )//(/)
{
var1=var1/var2;
}
else if(operation==3 )//-
{
var1=var1-var2;
}
else if(operation==4 )//(+)
{
var1=var1+var2;
}
else
{
cout<<"Error!"< <endl;
state=-1;
}
cout<<"The result so far is: "<<var1<<en dl;
state=2;

}//end of state 3
else //end of string
{
if(state==0)
{
state=2;
//var1=var1++;
cout<<"FINAL RESULT: "<<var1<<en dl;

}
else
{
return -1;
}
} //end
}
}//end of for loop
}while(ch!=0 && state!=-1);

}

Sep 23 '07 #1
2 2685
SORRY THE RIGHT PROGRAM IS THAT ONE:
I FORGOT TO ERASE IF(CH!=' ') BECAUSE IT SHOULD IGNORE SPACES OR BLANK
INLCUDED IN THE EQUATION
TY for help and any advice!
*************** *************** *************
the file:
#include <iostream>
#include <string>

using namespace::std;
using std::string;

int main()
{ string s;
//char *Exp;
int state = 0; //initial state
int site = 0;
int var1 = 0, var2 = 0, operation = 0;
char ch;

cout<<"Enter the equation you want to use: ";
getline(cin, s);

do{
for(int i=0;i<=s.size() ;i++)
{
ch=s[site];
site++;

if(state==0)//looking for a variable
{
if(ch>='a' && ch<='z')//reading first character
{
cout<<"Enter value of "<<ch<<": ";
cin>>var1;
state=1;
}
else
{
cout<<"Error: No variable."<<end l;
state=-1;
}
}//end state 0
else if(state==1) // reading first operation:
{
if(ch=='*')
{
operation=1;
state=2;
}
else if(ch=='/')
{
operation=2;
state=2;
}
else if(ch=='-')
{
operation=3;
state=2;
}
else if(ch=='+')
{
operation=4;
state=2;
}
else
{
cout<<"Error: Unknown
Operation."<<en dl;
state=-1;
}
}//end reading first operation--End of state 1
else if(state==2)//reading second variable
{
if(ch>='a' && ch<='z')//reading 2nd character
{
cout<<"Enter value of "<<ch<<": ";
cin>>var2;
state=3;
}
else
{
cout<<"Error: No variable."<<end l;
state=-1;
}
}//end of state 2: reading second variable
else if(state==3)
{
if(operation==1 ) //*
{
var1=var1*var2;

}
else if(operation==2 )//(/)
{
var1=var1/var2;
}
else if(operation==3 )//-
{
var1=var1-var2;
}
else if(operation==4 )//(+)
{
var1=var1+var2;
}
else
{
cout<<"Error!"< <endl;
state=-1;
}
cout<<"The result so far is: "<<var1<<en dl;

state=2;

}//end of state 3

else //end of string
{
if(state==0)
{
state=2;
//var1=var1++;
cout<<"FINAL RESULT: "<<var1<<en dl;

}
else
{
return -1;
}

} //end
}//end of for loop
}while(ch!=0 && state!=-1);
}

Sep 23 '07 #2
<al******@gmail .comwrote:

i am new in c++ and i need some help with a program
i need to do a universal unit converter. this calculator can only do :
+, *, /, and -.
it should read a string (the equation) and ask you to put the value of
that string. i wasn't able to do that, but i could do with
characters...
for ex :
if i write: CelsiusTemp * ConversionFacto r + base....it should ask me
to input the value of celsiustemp and ...
i was only able to do a+b/d+c and input the value of each one.
and it still didn't work well.
TY for help and any advice!
*************** *************** *************
the file:
#include <iostream>
#include <string>

using namespace::std;
using std::string;
int main()
{ string s;
//char *Exp;
int state = 0; //initial state
int site = 0;
int var1 = 0, var2 = 0, operation = 0;
char ch;

cout<<"Enter the equation you want to use: ";
getline(cin, s);
do{
for(int i=0;i<=s.size() ;i++)
{
ch=s[site];
site++;
if(ch==' '){
if(state==0)//looking for a variable
{
if(ch>='a' && ch<='z')//reading first character
The only way I can get here is if ch == ' '. Why ask questions to which the
answers are already known?
-----
I can't figure out what your goal is, but I think you might be trying to do
something pretty difficult.
Should the word "celsisus" be an acceptable input to your completed program?
If so, it's pretty hard, and you should set your sights a little lower..

I suggest you learn about the switch statement and also about functions.
Functions give you the opportunity to modularize your code so an ordinary
mortal could see what the intent of the code is.


Sep 24 '07 #3

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

Similar topics

3
2571
by: Sean McCourt | last post by:
Hi I am doing a JavaScript course and learning from the recommed book (JavaScript 3rd Edition by Don Gosslin) Below is one of the exercises from the book. I get this error message when I try to use the calculator. "document.Calculate.Input is null or not an object" Can someone please tell me why this is?
4
1933
by: mwh | last post by:
Hi. If you remember, I posted Expressons Help. Now I am making a calculator with javascript. I can't get this to work: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <title>Calculator</title> <script language="Javascript"> <!-- Begin Hiding var total = 0
6
7308
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... #include <stdio.h> #include <stdlib.h>
13
3261
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. Here is the code: #include <iostream>
24
6342
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
0
1032
by: biltz | last post by:
i want function prototype of calculator..i made the calculator source code and now i want to insert dis source code into other program... as there is a function prototype of maths table..that is void table(int ) so is ther function prototype of calculator???? plzz help its urgent i wanna sho this program to my teacher because this is the assignment and teacher is not goin to help me...plzz for god sake!!!!!
22
5826
by: jamestuck21 | last post by:
Hi, I'm trying to work out a binary division problem 1100 / 101010101010111 Here is what I have so far but I'm not sure if I'm doing it correctly and I'm suppose to continue the division until there is only a remainder left
4
1131
by: mak1084 | last post by:
import javax.swing.*; import java.awt.event.*; public class Calculator extends JFrame { public Calculator() { JButton jBtn1=new JButton("1");
0
1513
by: Guilherme Polo | last post by:
On Wed, Sep 3, 2008 at 8:57 PM, Kevin McKinley <kem1723@yahoo.comwrote: Come on.. "help on lines 384-403", that is not a good way to look for help. You are supposed to post some minimal code that demonstrates the problem. Anyway, this demonstrates what you are getting (independent of python version): import Tkinter
3
2898
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of the last operator entered /* Digit entered as integer value i * Updates the value of input accordingly to (input * 10) + i */
0
9666
marktang
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...
0
9512
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
10419
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
10201
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
10147
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
9023
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
6770
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
5424
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...
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.