A
ali.sobh
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);
}
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);
}