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

help with a c++ calculator

P: n/a
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 * ConversionFactor + 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."<<endl;
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."<<endl;
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."<<endl;
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<<endl;
state=2;

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

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

}

Sep 23 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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."<<endl;
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."<<endl;
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."<<endl;
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<<endl;

state=2;

}//end of state 3

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

}
else
{
return -1;
}

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

Sep 23 '07 #2

P: n/a
<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 * ConversionFactor + 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 discussion thread is closed

Replies have been disabled for this discussion.