431,919 Members | 1,587 Online
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

 P: 48 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 Replies

 Expert Mod 2.5K+ P: 4,677 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

 P: 48 This is my code for my math console application so far: Expand|Select|Wrap|Line Numbers // math.cpp : main project file.   #include "stdafx.h"   #include    #include    #include    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? Aug 14 '07 #3

 Expert Mod 2.5K+ P: 4,677 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

 P: 48 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

 Expert 100+ P: 638 I feel that your answer will lie in what the value of b is after the getline call. Aug 14 '07 #6

 P: 48 and the answer is? ????? Aug 14 '07 #7

 P: 48 now total is 1241320 Aug 14 '07 #8

 Expert 100+ P: 638 now total is 1241320 My guess is that b is a memory address, not a value Aug 14 '07 #9

 P: 48 so how to I fix it, how do I modify my code dude? Aug 14 '07 #10

 P: 48 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

 Expert 10K+ P: 11,448 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

 Expert 5K+ P: 8,434 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

 P: 11 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

 P: 60 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 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! Aug 15 '07 #15

 P: 48 thanks a lot hunderpanzer, you've given me a good idea of what i have to do! Thanks... Aug 15 '07 #16

 Expert 5K+ P: 8,434 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

 P: 48 I can substitute hyphens Aug 15 '07 #18

 P: 48 But how would I know how many variables I need, ie. Expand|Select|Wrap|Line Numbers  int a;           int b;  Aug 15 '07 #19

 Expert Mod 2.5K+ P: 4,677 But how would I know how many variables I need, ie. Expand|Select|Wrap|Line Numbers  int a;           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

 P: 48 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 // math.cpp : main project file.   #include    #include    #include    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; }     Aug 15 '07 #21

 Expert Mod 2.5K+ P: 4,677 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     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;   Aug 15 '07 #22

 P: 48 Yeah I have. And I dont know whats wrong or what to do! Aug 15 '07 #23

 Expert Mod 2.5K+ P: 4,677 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

 P: 48 it does exactly the same thing, skipping all the user input and showing some random number at TOTAL. Aug 15 '07 #25

 Expert Mod 2.5K+ P: 4,677 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

 Expert 100+ P: 844 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 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 }   Aug 15 '07 #27

 P: 48 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

 Expert 100+ P: 844 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

 P: 48 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

 P: 48 actually, if you could help me with hunderpanzer's method, that would be good! Aug 15 '07 #31

 Expert Mod 2.5K+ P: 4,677 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     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.) Aug 15 '07 #32

 P: 48 it shows (NULL)(NULL) before the random number at total Aug 15 '07 #33

 Expert Mod 2.5K+ P: 4,677 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

 P: 60 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 /* 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! Aug 15 '07 #35

 P: 60 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

 Expert Mod 2.5K+ P: 4,677 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

 P: 60 I apologize for the trouble, sicarie. Crazyfire it's actually pretty simple: Expand|Select|Wrap|Line Numbers   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! Aug 15 '07 #38

 Expert Mod 2.5K+ P: 4,677 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

 Expert 10K+ P: 11,448 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

 Expert 5K+ P: 8,434 ... Expand|Select|Wrap|Line Numbers   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. Aug 16 '07 #41

 P: 48 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