473,803 Members | 3,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The addition problem

48 New Member
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 2217
sicarie
4,677 Recognized Expert Moderator Specialist
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
CyBerFirEZ
48 New Member
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 Recognized Expert Moderator Specialist
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
CyBerFirEZ
48 New Member
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 Recognized Expert Contributor
I feel that your answer will lie in what the value of b is after the getline call.
Aug 14 '07 #6
CyBerFirEZ
48 New Member
and the answer is?
?????
Aug 14 '07 #7
CyBerFirEZ
48 New Member
now total is 1241320
Aug 14 '07 #8
TRScheel
638 Recognized Expert Contributor
now total is 1241320
My guess is that b is a memory address, not a value
Aug 14 '07 #9
CyBerFirEZ
48 New Member
so how to I fix it, how do I modify my code dude?
Aug 14 '07 #10

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

Similar topics

3
3438
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 Gaussian integer is a complex number x+yi for which both x and y are integers): class Gaussian(object): """class representing Gaussian integers"""
2
1232
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 issue but all my ctypes are set to decimal. I am resetting all my textfields to "" after adding the detail information. Any help would be appreciated. Here's the code:
24
2721
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 com http://mathforum.org/library/view/10978.html
34
16694
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 - 3;
7
4782
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 error occurs in both installations. When I do a compile/build of my app and go to the browser and hit reload there comes an error:
3
2191
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, dashes) into each variable. For just numbers, it's relatively easy: ifstream fout("s1.in"); for (a=0; a<17; a++) { fout >> data; cout << data << endl; }
2
1435
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. There are a few sort criteria made by checkboxes, and the projects shown update immediately to reflect that. There are a series of companies that work is being done for, and for
5
4598
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 Edition program to control a remote basketball scoreboard display unit. All I'm trying to do is add 5 byte variables and store the result in an integer variable. I added a Try/Catch block to take look at things. This exception occurs only when...
3
1612
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 to see the updated addition performed so far in the final textbox..
1
1333
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 cast((adprem)*12 as money) WHEN Original.CampCode like '%u%' AND(Original.CompletedCode <> 25 AND Original.CompletedCode <> 24) THEN 0
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.