The addition problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CyBerFirEZ
    New Member
    • Aug 2007
    • 48

    #1

    The addition problem

    Hi, I have really searched my brain for a solution but i cant seem to find one, and I really need some code to help me.

    I can do something like:
    a + b = r;
    cout << r;

    but that is easy. What I want is that when the user goes onto an addition function, they can type the integers separated by commas ie.

    1,3,6,23,2

    and this would do 1+3+6+23+2, and display the output. The user should be allowed to enter as many numbers as they want.

    How do I do this?

    Any help would be appreciated.

    Thnx :-)
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by CyBerFirEZ
    Hi, I have really searched my brain for a solution but i cant seem to find one, and I really need some code to help me.

    I can do something like:
    a + b = r;
    cout << r;
    Won't work, the association is on the wrong side.
    Originally posted by CyBerFirEZ
    but that is easy. What I want is that when the user goes onto an addition function, they can type the integers separated by commas ie.

    1,3,6,23,2

    and this would do 1+3+6+23+2, and display the output. The user should be allowed to enter as many numbers as they want.

    How do I do this?

    Any help would be appreciated.

    Thnx :-)
    How would you go about starting this? I'm going to push you here because you tend to ask for code. How would you start to develop an algorithm for this?

    Hint: if you put the input in a loop, you could have an extra variable that did nothing but count...
    Last edited by sicarie; Aug 14 '07, 07:30 PM. Reason: Ok, I'll give some hints

    Comment

    • CyBerFirEZ
      New Member
      • Aug 2007
      • 48

      #3
      This is my code for my math console application so far:

      [code=cpp]
      // math.cpp : main project file.

      #include "stdafx.h"

      #include <iostream>

      #include <string>

      #include <sstream>

      using namespace std;


      int main()
      {

      /* ... snipped a==1 and a==2 ... */

      if ( a == 3)
      {
      string b;
      int c;
      cout << "Parameter 3 has been called. This is the addition function. \n";
      cout << "USAGE: no1 + no2 + no3 + no4+...... | A default error will result in TOTAL to equal 0. Leave spaces between addition symbol and integers. \n";
      getline (cin, b);
      stringstream(b) >> c;
      cout << "TOTAL:" << c << endl;
      }

      } while (a != 1);
      return 0;
      }

      [/code]
      Can anyone see any problems with it?
      Last edited by sicarie; Aug 15 '07, 01:27 PM. Reason: Code tags, snipping full code.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Please read the Posting Guidelines thoroughly.

        They ask that you use code tags, ask specific questions (and not "what is wrong with this"), etc... Please follow the rules of the site.

        Have you run your code through a compiler? I would imagine that would tell you if anything is wrong with it very quickly.

        Comment

        • CyBerFirEZ
          New Member
          • Aug 2007
          • 48

          #5
          Well, ok sorry about that.
          My compiler sees no errors (ENU Visual Studio C++ express 2005).
          But when I use option 3, it nevers waits for user input on the getline bit (around line 54), and says TOTAL: 1202464

          Everything else works fine, so where am I going wrong with option 3?

          Comment

          • TRScheel
            Recognized Expert Contributor
            • Apr 2007
            • 638

            #6
            I feel that your answer will lie in what the value of b is after the getline call.

            Comment

            • CyBerFirEZ
              New Member
              • Aug 2007
              • 48

              #7
              and the answer is?
              ?????

              Comment

              • CyBerFirEZ
                New Member
                • Aug 2007
                • 48

                #8
                now total is 1241320

                Comment

                • TRScheel
                  Recognized Expert Contributor
                  • Apr 2007
                  • 638

                  #9
                  Originally posted by CyBerFirEZ
                  now total is 1241320
                  My guess is that b is a memory address, not a value

                  Comment

                  • CyBerFirEZ
                    New Member
                    • Aug 2007
                    • 48

                    #10
                    so how to I fix it, how do I modify my code dude?

                    Comment

                    • CyBerFirEZ
                      New Member
                      • Aug 2007
                      • 48

                      #11
                      HELP!!!! How do I fix this problem, I want it to figure the string the user enters. Like the user enters:
                      1 + 2 + 3

                      And it shows TOTAL:

                      6

                      Instead its showing that weird random number. How do I fix it?!!

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by CyBerFirEZ
                        HELP!!!! How do I fix this problem, I want it to figure the string the user enters. Like the user enters:
                        1 + 2 + 3

                        And it shows TOTAL:

                        6

                        Instead its showing that weird random number. How do I fix it?!!
                        Even if the line "1 + 2 + 3" were read correctly there's nothing in your code
                        that performs the conversion of the possible operands, nor the addition of them.
                        Things don't just happen automagically you know.

                        kind regards,

                        Jos

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Originally posted by JosAH
                          Even if the line "1 + 2 + 3" were read correctly there's nothing in your code
                          that performs the conversion of the possible operands, nor the addition of them.
                          Things don't just happen automagically you know.
                          I speak from a Visual Basic perspective, so this may be no use to you. But I gather the Microsoft Script Control can evaluate an expression like this at runtime. Here's the description of its Eval method....
                          ------------------------------------------------------------
                          Evaluates an expression and returns the result.

                          Syntax

                          object.Eval(expression)

                          The Eval method has these parts:

                          Part / Description
                          object
                          Required. The object in which the expression is being evaluated or a reference to it. Can be any object in the "Applies To" list.

                          expression
                          Required. String containing the expression being evaluated.

                          Remarks

                          The context of the Eval method is determined by the object argument. If object is a module, the context is restricted to the named module. If object is the ScriptControl, the context is global.

                          Comment

                          • Wizard1981
                            New Member
                            • Aug 2007
                            • 11

                            #14
                            Another option:
                            Get yourself a string, read it in and parse it: You have to do some string operations to extract the '+' charaters and convert the rest to int (see atoi). Doing so, you have full control about what to accept as sound input.

                            Comment

                            • Hunderpanzer
                              New Member
                              • Jul 2007
                              • 60

                              #15
                              Originally posted by CyBerFirEZ
                              so how to I fix it, how do I modify my code dude?

                              Well Cyberfirez(stic kcaps) there's a huge function involved with trying to seperate strings and substrings with commas (or anything really) .

                              This would be MUCH easier, though I'm not sure if it's acceptable to you.


                              Code:
                              Enter the numbers you want to add ( 0 to find the total)
                              1 
                              Enter the numbers you want to add ( 0 to find the total)
                              2
                              Enter the numbers you want to add ( 0 to find the total)
                              3
                              Enter the numbers you want to add (0 to find the total)
                              0
                              The total is 6
                              This isn't exactly pseudo code but it will help you get the idea straight.
                              and may seem tedious, but it's easier to program.


                              All you need is a counter loop, and an empty variable to add the numbers to.

                              Maybe you can even find a way to only say the prompt only once ?

                              I have faith in you!
                              Last edited by Hunderpanzer; Aug 15 '07, 08:31 AM. Reason: I would like to put the psuedocode into some kind of quotation or code format for easier viewing

                              Comment

                              Working...