c++ calculator..help plz

Discussion in 'General Computer Support' started by bolok, Sep 23, 2007.

  1. bolok

    bolok

    Joined:
    Sep 23, 2007
    Messages:
    1
    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);



    }
    bolok, Sep 23, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alkzy

    Re: plz help!!! plz plz plz plzplzplz help the noob

    alkzy, Oct 31, 2004, in forum: Microsoft Certification
    Replies:
    0
    Views:
    552
    alkzy
    Oct 31, 2004
  2. Nick
    Replies:
    0
    Views:
    440
  3. BIGEYE

    Casio Algebra 2.0 Calculator Help

    BIGEYE, Oct 1, 2004, in forum: Computer Support
    Replies:
    3
    Views:
    600
    BIGEYE
    Oct 2, 2004
  4. Rollson
    Replies:
    1
    Views:
    380
    Beauregard T. Shagnasty
    Jun 19, 2006
  5. vijaygubba
    Replies:
    0
    Views:
    1,443
    vijaygubba
    Feb 4, 2008
Loading...

Share This Page