Thread: Not sure how to ask this

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    Not sure how to ask this

    Basically the program I'm trying to do lets you input something like 2*2 on a console application and get an answer. The * could be a -, +, or / and it's suppose to do the caculation according to what you type in.

    Something like

    cout << "Enter Equasion: "
    cin >> num1 >> operator >> num2

    And you'd put in the numbers and what you wanted to do with the numbers, be it add or subtract or whatever.

    The operator is defined as a char while num1 and num2 are int, but it doesn't work right. How would you code this to work properly? What I get now is just typing in my equasion and getting the option to exit the console.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    This exact code is given as an example in Stroustrup's "The C++ programming language." Unfortunately, it has been a long time since I read it, and I don't remember the details exactly, except that it was relatively short, and he used the STL map to do most of the work.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Otherwise, you could accept the user input equation as a string. (validate the string). parse the string into tokens (substrings). convert the first and third tokens into numerical types. map the second tken to a funtion call using a switch or a map or whatever passing tokens 1 and 3 as parameters.

    Needless to say this can get reasonably involved for someone just starting out. It's easier to have user input each token into discreet variables, as opposed to a discreet equation, and bypass the first three or four steps above.
    You're only born perfect.

  4. #4
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    wasn't as brilljant as I thought

    I'll write you a program... can you wait like 20 minutes?... 1 sec.
    cause I think I have a brilljant idea

    hmm doesn;t seem to be as brilljant..2 seconds...
    Last edited by MystWind; 03-09-2005 at 12:48 PM. Reason: wasn't as brilljant as I thought
    PLay MystWind beta , within two years

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Oh sure, I've got all day really.

    The main problem is that our teacher is out of town for three days and just left us with this asignment. We're all sort of just going "Eh?" at the problem. I don't think she realised this was something we were unfamiliar with.

  6. #6
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Lightbulb

    WOOOT ! I did it ! check this out :

    Code:
    #include <iostream>
    #include <conio.h> // for the getch();
    using namespace std;
    
    int main (int)
    
    {
        
    
        int x ;
        char y ;
        int a ;
        for ( int z=0 ; z<100 ; z++ ) {
        cout << "\n\nInsert first number";    
        cin >> x ;
        cout << "\n\nwhat do you want to do with it ?\n\n";
        cin >> y ;
        cout << "\n\nInsert second number";
        cin >> a ;
        if ( y == '*' ) {
             cout << x*a;
             }
             else if ( y == '+' ) {
                  cout << x+a ;
                  }
                  else if ( y== '-' ) {
                       cout <<"\n\n" << x-a << \n\n ;
                       }
                  }
    getch();
    }
    I now compile it a bit then post again !
    PLay MystWind beta , within two years

  7. #7
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Lightbulb calculator

    ok heres the final version ! :


    Code:
    #include <iostream>
    #include <conio.h> // for the getch();
    using namespace std;
    
    int main (int)
    
    {
        
    
        float x ;
        char y ;
        float a ;
        for ( int z=0 ; z<100 ; z++ ) {
        cout << "\n\n" ;
        cin >> x ;
    
        cin >> y ;
    
        cin >> a ;
        if ( y == '*' ) {
             cout <<  "awnser = " << x*a;
             }
             else if ( y == '+' ) {
                  cout  <<  "awnser = " << x+a ;
                  }
                  else if ( y== '-' ) {
                       cout <<"\n\n" <<  "awnser = " <<x-a << "\n\n" ;
                       }
                       else if ( y == ':' ) {
                                cout  <<  "awnser = " << x/a ;
                                }
                                else if ( y == '/' ) {
                                     cout << "awnser = " << x/a ;
                                }
    
                  
                  }
     getch();
    }
    I'm very proub of myself first time I wrote something like this : its all in the mind :P
    Last edited by MystWind; 03-09-2005 at 01:25 PM. Reason: added '/'
    PLay MystWind beta , within two years

  8. #8
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    p.s how do you get a teacher ? ehm , you mean in c++ right ?
    I want a teacher too
    PLay MystWind beta , within two years

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    MystWind, your main() should return a value.
    When no one helps you out. Call google();

  10. #10
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Red face

    ah maybe , the main thing it works isn't it . he asked hwo to do it so I showed the method , how good the rest of the program is doesn't matter I think... grm always comment when you try to do something good.
    PLay MystWind beta , within two years

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Strictly speaking, that isn't necessary (it returns 0 implicitly) - but stylistically, most people prefer to have it return 0 explicity. Just as long as it isn't void main
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    No, what i meant is that your main() should end with
    Code:
    return 0;
    otherwise you will get a warning
    Code:
    arning C4508: 'main' : function should return a value; 'void' return type assumed
    When no one helps you out. Call google();

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>but it doesn't work right.
    >>What I get now is just typing in my equasion and getting the option to exit the console.
    I gather that it doesn't work as you expect, but that's about all I can understand out of it. What do you mean 'getting the option' to exit the console? Do you mean, the moment you type in the equation it quits, or what? Can you show some code?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    otherwise you will get a warning
    Code:
    arning C4508: 'main' : function should return a value; 'void' return type assumed
    I don't get a warning : compiler = dev-c++ .
    PLay MystWind beta , within two years

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    maybe you dont have warning messages turned on. I dont know but on Visual C++, you get a warning error.
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed