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 :-)
41 2141
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...
This is my code for my math console application so far: - // 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;
-
}
-
Can anyone see any problems with it?
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.
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?
I feel that your answer will lie in what the value of b is after the getline call.
now total is 1241320
My guess is that b is a memory address, not a value
so how to I fix it, how do I modify my code dude?
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?!!
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
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.
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.
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. - 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)
-
-
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!
thanks a lot hunderpanzer, you've given me a good idea of what i have to do!
Thanks...
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.
But how would I know how many variables I need, ie.
But how would I know how many variables I need, ie.
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.
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: -
// math.cpp : main project file.
-
-
#include <iostream>
-
-
#include <string>
-
-
#include <sstream>
-
-
using namespace std;
-
-
int main()
-
{
-
-
/* ... snipped unrelated code ... */
-
-
if (a == 3)
-
{
-
string add;
-
int b;
-
cout << "Functioning arg 3, (literally arg 4). Addition Operator: \n \n";
-
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";
-
getline (cin,add);
-
stringstream(add) >> b;
-
cout << "TOTAL:" << b << endl;
-
cout << endl;
-
}
-
} while (a != 1);
-
return 0;
-
}
-
-
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? -
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";
-
getline (cin,add);
-
stringstream(add) >> b;
-
Yeah I have. And I dont know whats wrong or what to do!
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?
it does exactly the same thing, skipping all the user input and showing some random number at TOTAL.
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?
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: -
string line;
-
getline(cin, line); // "1 + 6 + 9 + 2 etc... "
-
-
for (int x = 0; x < line.size(); x++) // iterate over string
-
{
-
if (line[x] == "+" etc.... )
-
{
-
skip
-
}
-
keep going, looking for integers and add to total sum
-
}
-
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.
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.
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
actually, if you could help me with hunderpanzer's method, that would be good!
Well, as you said you did this (and it seems now you just said you did), i was talking about doing this: -
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";
-
printf("%s",add);
-
getline (cin,add);
-
printf("%s",add);
-
stringstream(add) >> b;
-
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.)
it shows (NULL)(NULL) before the random number at total
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.
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. -
/* full code snipped - we don't spoonfeed (though an algorithm might help him...)*/
-
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!
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
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.
I apologize for the trouble, sicarie.
Crazyfire it's actually pretty simple: -
-
while (1){
-
cout << "Enter numbers to add ( 0 to total )";
-
cin >> n;
-
sum = sum + n;
-
if (n == 0){
-
cout << sum ;
-
break;
-
}
-
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!
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!
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
... -
-
while (1){
-
cout << "Enter numbers to add ( 0 to total )";
-
cin >> n;
-
sum = sum + n;
-
if (n == 0){
-
cout << sum ;
-
break;
-
}
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.
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 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |