I use DevC++ and I have an assignment to do. I am trying to get the loops understood but can be head racking when actually putting it into code. I have to ask for 20 numbers either positive or negative. Then I must find the sum while ignoring the negative number and only produce the sum of the positive numbers. I know yall say easy but for me a beginner its not so easy. I would post what I have but yeah I have really nothing as far as it ignoring the negative numbers. any help and I am very thankful.

-- Sun Nov 11, 2012 8:03 pm --

to better clear this up it has to do with loops, I am not sure what loop to use, The program must prompt the user for 20 numbers, it have to sum up only the positive numbers while ignoring the negative numbers.example Enter a list of 20 numbers:2 3 1 -6 10 2 6 5 9 -10 9 2 -4 3 1 2 -6 7 3 2The sum of the positive numbers is: 67

I'm pretty sure this goes beyond the simple concept of the loops like "while-do" and into distinguishing positive & negative...I'm sorry if this has a lot of one-character errors, I haven't revisited my tutorials in a while...

Oh, so it is how you end a line. I spent five minutes searching and coming up with nothing, when previously there were billions of places where you could find the ending syntax. Google gives you the wrong answers at the worst of moments...But glad I could help.

not_essence2 wrote:I'm pretty sure this goes beyond the simple concept of the loops like "while-do" and into distinguishing positive & negative...I'm sorry if this has a lot of one-character errors, I haven't revisited my tutorials in a while...

int result; //I'm just declaring a global variablewhile (a>0);{result+=a;} //Sorry, I forgot how to end it. Was it something like "endl}"?while (b>0);{result+=b;} //And end the loop again...

Does that help? Sorry, my syntax is probably way off, but you get the basic idea.

That code is broken. Let's say we run it, and 'a' happens to equal 50, well, then we go to the while code, and since 'a' is greater than 0, we step into it. Then, we add 'a' to results, but, because 'a' still equals 50 (which is greater than 0), we're going to keep repeating the same line of code, over, and over, and over again. (I think the statement you were looking for is 'if')Here's my take at it (Note: I haven't written C++ in a long time, so it's probably not all that efficient.):

I didn't actually test that code I just wrote, I was just going off the top of my head, but it should still work. Tell me if you have any troubles.

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook

That was just a fix for my own code. C3ntipede's is fine as well.Actually, the "if" loop would probably be better...--EDIT--Just putting semicolons where I forgot to. *facepalm* And removing other stuff. It'll still be riddled with holes, I promise you that.

Last edited by not_essence2 on Mon Nov 12, 2012 10:30 pm, edited 1 time in total.

That was just a fix for my own code. C3ntipede's is fine as well.Actually, the "if" loop would probably be better...

first off, you lack semi-colons. second, if is not a loop, its a conditional statement. third, why in the hell would you do a -= a? this isn't assembly. you can just do a = 0;. and endl is a standard library macro for the end of line character(s). you don't use that to end any statements. fourth, you don't need 2 nested brackets, you only need one. fifth, PLEASE, PLEASE, test your code before posting it if you aren't sure. I almost tore my hair out when I first saw it, but I thought you could correct it properly. Some poor soul is going to look at this and think it's good code, and then be absolutely confused when nothing even compiles. it's not that hard:

That was just a fix for my own code. C3ntipede's is fine as well.Actually, the "if" loop would probably be better...

To comment on your code: 1. You have two brackets after a while statement... What? 2. A semi-colon goes after EVERY LINE of code that isn't a conditional statement (i.e. if statements, else statements, etc.), or a loop (i.e. for, while, goto (don't use goto))3. If you subtract 'a' from 'a', you'll get the same thing as setting 'a' to 0, the only difference is that you're requiring the computer to make much more of an effort than just setting it to 0. 4. Why are you using an 'endl;'? 'endl;' stands for 'end line', or, the end of a line, or, the thing you send at the end of a STRING to make it go down a line (you could also send '\n' (for new-line))5. The while loop was not the only issue with your previous code -- You also instantiated (made) 19 variables that you didn't need, making it run much, much slower. As I always say: "First make it work; then, make it work better". While you did make it work(ish), you didn't make it work better... At all.

Suggestion: I'd suggest you start learning C++ a bit more (if you want to continue in the language (if not, don't give advise on it!)). Even though I hate the guy, thenewboston has lots of C++ tutorials to get you on your feet.

Side note: People here are really critical of code that doesn't work, when the person made it seem like they know what they're doing. We aren't being assholes (-ahem- WallShadow), we just don't want someone who doesn't know what they're doing, to think that what you did was right. So don't stop programming, you're doing just fine for a beginner, and will only get better.

WallShadow wrote:@cent I see what you did there with the c

What'd I do? It was supposed to be shorthand for "count"...

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook

Sorry, I was in a rush. Sloppy code and all that. I should probably learn more. The syntax in my head got really messed up, and of course, I forgot to check for efficiency.You guys are right, I gave stuff out before learning it myself. I'm sorry.WallShadow- the OP was asking for a code with loops, and I haven't read on C++ in a while, so I assumed centip3de was talking about loops. Stupid me.And yes, +1 to cent.