string help

This is a discussion on string help within the C++ Programming forums, part of the General Programming Boards category; im trying to get the user to type in an equation, and then have the app display the answer. i ...

  1. #1
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114

    string help

    im trying to get the user to type in an equation, and then have the app display the answer.

    i have the users input as int input[256].

    when i cin>> input;

    it displays exactly what the user typed in, which i was expecting. is there a wat i can make the program solve the equation instead of repeating the equation the user typed in?

    Code:
         void calculator()
         {
              cout<<"\n\n------------------------------------------------\n\n";
              cout<<"DOS OS CALCULATOR\n\n";
              cout<<"----------------------------------------------------\n\n";
              calculatorcont();
              }
              
              void calculatorcont()
              {
              char input[256];
              int choice;
              cout<<"\n\nPlease enter the equation you wish to solve:";
              cin>>input;
              cout<< "The answer is:" << input ;
              cout<<"\n\n Would you like to solve another equation?\n\n1.)Yes\n2.)No";
              cout<<"\n\nSelection:";
              cin>> choice;
              switch (choice){
                     case 1:
                          calculatorcont();
                          break;
                     case 2:
                          desktop();
                          break;
                     default:
                     cout<<"INTERNAL ERROR [696E76616C696420656E74727900]";
                        cin.get();
                        cin.get();
                        return;      
                     
          }
          }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is there a wat i can make the program solve the equation instead of repeating the equation the user typed in?
    Yes, but you have to write it yourself. That includes breaking the string into tokens, parsing the tokens, validating the expression, and performing the calculations internally before returning the final result.

    The job is much simpler if you require postfix expressions because that saves you all of the ugly parsing steps that infix expressions need, but it still isn't a trivial task. Give it a shot, it's a fun exercise.
    My best code is written with the delete key.

  3. #3
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    im off to try to make that now, ill get back to the boards once i complete or fail to complete this task

    by the way, i do ask a lot of questions, but im not in a c++ class. the class im taking in school is visual basic right now, c++ is the next trimester, but i thought id get a head start by doing the cprog tuts, so far im up to classes

  4. #4
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    i got my calculator
    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21