473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 :-)
Aug 14 '07 #1
41 2141
sicarie
4,677 Expert Mod 4TB
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.
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...
Aug 14 '07 #2
This is my code for my math console application so far:

Expand|Select|Wrap|Line Numbers
  1. // math.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4.  
  5. #include <iostream>
  6.  
  7. #include <string>
  8.  
  9. #include <sstream>
  10.  
  11. using namespace std;
  12.  
  13.  
  14. int main()
  15. {
  16.  
  17.    /* ... snipped a==1 and a==2 ... */
  18.  
  19.  if ( a == 3)
  20.     {
  21.      string b;
  22.      int c;
  23.      cout << "Parameter 3 has been called. This is the addition function. \n";
  24.      cout << "USAGE: no1 + no2 + no3 + no4+...... | A default error will result in TOTAL to equal 0. Leave spaces between addition symbol and integers. \n";
  25.      getline (cin, b);
  26.      stringstream(b) >> c;
  27.      cout << "TOTAL:" << c << endl;
  28.     }
  29.  
  30.     } while (a != 1);
  31.     return 0;
  32. }
  33.  
Can anyone see any problems with it?
Aug 14 '07 #3
sicarie
4,677 Expert Mod 4TB
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.
Aug 14 '07 #4
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?
Aug 14 '07 #5
TRScheel
638 Expert 512MB
I feel that your answer will lie in what the value of b is after the getline call.
Aug 14 '07 #6
and the answer is?
?????
Aug 14 '07 #7
now total is 1241320
Aug 14 '07 #8
TRScheel
638 Expert 512MB
now total is 1241320
My guess is that b is a memory address, not a value
Aug 14 '07 #9
so how to I fix it, how do I modify my code dude?
Aug 14 '07 #10
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?!!
Aug 14 '07 #11
JosAH
11,448 Expert 8TB
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
Aug 14 '07 #12
Killer42
8,435 Expert 8TB
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.
Aug 15 '07 #13
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.
Aug 15 '07 #14
so how to I fix it, how do I modify my code dude?

Well Cyberfirez(stickcaps) 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.


Expand|Select|Wrap|Line Numbers
  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. 2
  4. Enter the numbers you want to add ( 0 to find the total)
  5. 3
  6. Enter the numbers you want to add (0 to find the total)
  7.  
  8. 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!
Aug 15 '07 #15
thanks a lot hunderpanzer, you've given me a good idea of what i have to do!
Thanks...
Aug 15 '07 #16
Killer42
8,435 Expert 8TB
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.
I was thinking about that, too. But order of precedence would complicate things, and it gets even worse if you allow parentheses.
Aug 15 '07 #17
I can substitute hyphens
Aug 15 '07 #18
But how would I know how many variables I need, ie.

Expand|Select|Wrap|Line Numbers
  1.  int a;
  2.           int b; 
Aug 15 '07 #19
sicarie
4,677 Expert Mod 4TB
But how would I know how many variables I need, ie.

Expand|Select|Wrap|Line Numbers
  1.  int a;
  2.           int b; 
You don't need to - you just need to keep a running total. Takes two variables tops, if you want to read it in to a var and then add it to your total.
Aug 15 '07 #20
OK, I just want to know what is wrong with my code.

It keeps showing-

TOTAL: 1306856

I'm using stringstream to covert the user input such as 1 + 2 to an integer, but as soon as a == 3, it doesnt wait for user input and just shows this total!

What is wrong here, and how do I fix it? Here is the code if it helps:

Expand|Select|Wrap|Line Numbers
  1. // math.cpp : main project file.
  2.  
  3. #include <iostream>
  4.  
  5. #include <string>
  6.  
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.  
  14.    /* ... snipped unrelated code ... */
  15.  
  16.     if (a == 3) 
  17.     {
  18.     string add;
  19.     int b;
  20.     cout << "Functioning arg 3, (literally arg 4). Addition Operator: \n \n";
  21.     cout << "Enter the sum you are performing separating symbols and numbers by a space. ie. 1 + 2 + 4. Hit RETURN after doing so. \n \n";
  22.     getline (cin,add);
  23.     stringstream(add) >> b;
  24.     cout << "TOTAL:" << b << endl;
  25.     cout << endl;
  26.     }
  27.     } while (a != 1);
  28.     return 0;
  29. }
  30.  
  31.  
Aug 15 '07 #21
sicarie
4,677 Expert Mod 4TB
Did you try doing what TRScheel suggested and looking at the getline() function? Did you try putting some print statements in before and after to see what value is in there (as you don't instantiate the variable), and what value it's spitting back after the reading?
Expand|Select|Wrap|Line Numbers
  1.     cout << "Enter the sum you are performing separating symbols and numbers by a space. ie. 1 + 2 + 4. Hit RETURN after doing so. \n \n";
  2.     getline (cin,add);
  3.     stringstream(add) >> b;
  4.  
Aug 15 '07 #22
Yeah I have. And I dont know whats wrong or what to do!
Aug 15 '07 #23
sicarie
4,677 Expert Mod 4TB
Yeah I have. And I dont know whats wrong or what to do!
Well, what are the outputs that you are getting when you do that?
Aug 15 '07 #24
it does exactly the same thing, skipping all the user input and showing some random number at TOTAL.
Aug 15 '07 #25
sicarie
4,677 Expert Mod 4TB
it does exactly the same thing, skipping all the user input and showing some random number at TOTAL.
But we're not looking at user input, we're trying to figure out what it is doing with the 'add' variable (in version 2, I think it was different in the first..., can't remember right now and can't scroll up!). We want to see what variables are in there, and if that's what is being printed out.

And just out of curiosity, does this mean you can't use the method Hunderpanzer suggested to you?
Aug 15 '07 #26
ilikepython
844 Expert 512MB
it does exactly the same thing, skipping all the user input and showing some random number at TOTAL.
I think what you need is this:
Expand|Select|Wrap|Line Numbers
  1. string line;
  2. getline(cin, line);   // "1 + 6 + 9 + 2 etc... "
  3.  
  4. for (int x = 0; x < line.size(); x++)  // iterate over string
  5. {
  6.     if (line[x] == "+" etc.... )
  7.     {
  8.         skip
  9.     }
  10.     keep going, looking for integers and add to total sum
  11. }
  12.  
Aug 15 '07 #27
no luck so far!
thanks for trying anyway.
Please, does anybody have a solution
P.S
Sicarie, I really dont have an idea why it keeps showing that output, thats why I came here for help, so if anyone has any ideas or suggestions, please give them.
Aug 15 '07 #28
ilikepython
844 Expert 512MB
no luck so far!
thanks for trying anyway.
Please, does anybody have a solution
P.S
Sicarie, I really dont have an idea why it keeps showing that output, thats why I came here for help, so if anyone has any ideas or suggestions, please give them.
What do you mean? That was pseudocode. Will Hunderpanzer's method work for you? (askinf for each number individually). That would be much easier.
Aug 15 '07 #29
I dont understand your code and how to make it interact with mine. I need precise help.
Could you explain to me how I'm to make that interact with my code, cos I havent seen half the stuff in that pseudocode before!
:P
Aug 15 '07 #30
actually, if you could help me with hunderpanzer's method, that would be good!
Aug 15 '07 #31
sicarie
4,677 Expert Mod 4TB
Well, as you said you did this (and it seems now you just said you did), i was talking about doing this:
Expand|Select|Wrap|Line Numbers
  1.     cout << "Enter the sum you are performing separating symbols and numbers by a space. ie. 1 + 2 + 4. Hit RETURN after doing so. \n \n";
  2.     printf("%s",add);
  3.     getline (cin,add);
  4.     printf("%s",add);
  5.     stringstream(add) >> b;
  6.  
That would show you what add is going into the getline function (as you don't initialize it when you create it), and then again when you go out and send it to b.

Hunderpanzer's method would be much easier, and you should be able to figure that one out on your own. (I mean, look at it. It's a loop that executes until someone puts in a 0, and when it executes it prints that line and asks for an in to be cin'd.)
Aug 15 '07 #32
it shows (NULL)(NULL) before the random number at total
Aug 15 '07 #33
sicarie
4,677 Expert Mod 4TB
it shows (NULL)(NULL) before the random number at total
Yeah, you're probably pulling in the memory location then. If you can do Hunderpanzer's method, I would recommend it.
Aug 15 '07 #34
actually, if you could help me with hunderpanzer's method, that would be good!

Alright here's how to do it. You actually don't even need a counter loop for this.

Expand|Select|Wrap|Line Numbers
  1. /* full code snipped  - we don't spoonfeed (though an algorithm might help him...)*/
  2.  

The while(1) is a loop that keeps ...looping until it is broken off with break;


the sum = sum + n just takes the number the user just input, and adds it to sum. That way you get the previous numbers and the new number the user input.


if (n == 0){
cout << sum ;
break;
}


If the user inputs 0, print the sum, and stop looping. That's it!
Aug 15 '07 #35
Posting Complete Solutions

When a person asks for help, they want help in understanding the problem. Posting the completed program code without explaining concepts or useful tools does not help that person learn and, thus, violates the first and second guideline above.

I'm pretty sure I explained the new ideas to him clearly
Aug 15 '07 #36
sicarie
4,677 Expert Mod 4TB
Yes, but having interacted with the OP I'm in disagreement that after it is read it will be internalized and/or learned, which defeats the purpose.

I would argue that the quoted material is merely one explanation of why the above rules were created, but I don't really care, it seems to me that the OP consistently tries as hard as they can to avoid work anyway, if you post again I won't take it down.
Aug 15 '07 #37
I apologize for the trouble, sicarie.



Crazyfire it's actually pretty simple:

Expand|Select|Wrap|Line Numbers
  1.  
  2. while (1){
  3.           cout << "Enter numbers to add ( 0 to total )";
  4.           cin >> n;
  5.           sum = sum + n;
  6.           if (n == 0){
  7.              cout << sum ;
  8.            break;
  9.              }
  10.  
The while(1) is a loop that keeps ...looping until it is broken off with break;


the sum = sum + n just takes the number the user just input, and adds it to sum. That way you get the previous numbers and the new number the user input.


if (n == 0){
cout << sum ;
break;
}

This means that If the user inputs 0, print the sum, and stop looping. That's it!
Aug 15 '07 #38
sicarie
4,677 Expert Mod 4TB
I apologize for the trouble, sicarie.
It's no trouble, just me being jaded by the past few days. It's definitely possible that I was overreacting, which is why it's probably good you are re-posting.

Thanks!
Aug 15 '07 #39
JosAH
11,448 Expert 8TB
I need precise help.
You should supply precise information on what exactly is wrong with your code
then. I've been into scientific programming before you were born and I know
what I'm talking about. Sorrry for putting it that harsh; you should at least put
effort in helping people to allow them to help you. Programming a computer, no
matter the language, no matter all the IDEs, no matter the operating system, is
a very precise job. Please be precise yourself if you ever want to succeed in this field.

kind regards,

Jos
Aug 15 '07 #40
Killer42
8,435 Expert 8TB
...
Expand|Select|Wrap|Line Numbers
  1.  
  2. while (1){
  3.           cout << "Enter numbers to add ( 0 to total )";
  4.           cin >> n;
  5.           sum = sum + n;
  6.           if (n == 0){
  7.              cout << sum ;
  8.              break;
  9.              }
Just as a general point, CyBerFirEZ. In this sort of situation it will usually make more sense to do your "if blah then break" processing before you add to your total. I'm aware that it doesn't make any practical difference in this case, since you're adding zero. It's just something I would recommend keep in mind for future loops. It's generally preferable to avoid any unnecessary processing, for reasons of efficiency among others.
Aug 16 '07 #41
Thanks Hunderpanzer, that's what I needed, now I can make progress with my code. Thanks a lot.1. You have taught me a new way of getting information and showing it.
2. You have given me precise help ( a code snippet) with an explanation.


Thats all I wanted.
:-P
Aug 16 '07 #42

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mark Dickinson | last post by:
Can anyone either reproduce or explain the following apparently inconsistent behaviours of __add__ and __mul__? The class Gaussian provides a sub-bare-bones implementation of Gaussian integers (a...
2
by: Derek | last post by:
I am having a problem updating the main table (2nd set of code) based on the total of all details entered (1st set of code). It seems to be off by less than a dollar. I thought it was a decimal...
24
by: Alex Vinokur | last post by:
Consider the following statement: n+i, where i = 1 or 0. Is there more fast method for computing n+i than direct computing that sum? -- Alex Vinokur email: alex DOT vinokur AT gmail DOT...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
7
by: Martin Strojek | last post by:
Hi, I have the following problem with developing some web site. I use Visual Studio 2003 to build a website. I tried Windows 2003 Server and switched now back to Windows XP with PWS but the...
3
by: snow.carriers | last post by:
Let me first state that I'm using Borland Turbo C++, it's relatively old so the new string methods won't work. Anyways, first I'm trying to collect a line of a string (with numbers, letters,...
2
by: S. Kitty | last post by:
Hi everyone! I have a bit of a weird problem right now with the addition of a record in a subform. The database is supposed to keep track of a list of projects for a consulting company. ...
5
by: Mike | last post by:
Hello All, Please, if anyone can point me to the problem, I'd sure appreciate it! I am very new to VB programming and not a programmer to begin with. This is part of a Visual Basic 2005 Express...
3
by: srinivas33034 | last post by:
Hi there, my problem is i have to perform addition dyamically My req is i have 3 txt boxes.. and another text box to display total additon.. as i am entering the values in TextBox1 i want...
1
by: sjohnson1984 | last post by:
I have the following within a SELECT statement: : AnnualRP = sum(CASE WHEN Original.CampCode like '%u%' AND(Original.CompletedCode = 24 OR Original.CompletedCode = 25) THEN...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.