C++

C++

Post by shdwcat on Sun Nov 11, 2012 6:55 pm
([msg=70742]see C++[/msg])

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 2
The sum of the positive numbers is: 67

sorry if there was confusion.
shdwcat
New User
New User
 
Posts: 27
Joined: Sun Nov 11, 2012 6:48 pm
Location: FL
Blog: View Blog (0)


Re: C++

Post by not_essence2 on Sun Nov 11, 2012 9:55 pm
([msg=70751]see Re: C++[/msg])

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...
Code: Select all
int main();
{
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t
cout <<"Please enter your first number /n";
cin >> a;
cin.ignore;
cout <<"Please enter your second number /n";
cin >> a;
cin.ignore;

... And so on until you get all of the numbers:
Code: Select all
int result;      //I'm just declaring a global variable
while (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.
not_essence2
Poster
Poster
 
Posts: 189
Joined: Fri Sep 14, 2012 6:09 pm
Blog: View Blog (0)


Re: C++

Post by shdwcat on Sun Nov 11, 2012 9:58 pm
([msg=70752]see Re: C++[/msg])

Yes thanks I do get the point thanks so much, and yes endl; is to end the line :-)
shdwcat
New User
New User
 
Posts: 27
Joined: Sun Nov 11, 2012 6:48 pm
Location: FL
Blog: View Blog (0)


Re: C++

Post by not_essence2 on Sun Nov 11, 2012 10:03 pm
([msg=70755]see Re: C++[/msg])

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
Poster
Poster
 
Posts: 189
Joined: Fri Sep 14, 2012 6:09 pm
Blog: View Blog (0)


Re: C++

Post by centip3de on Sun Nov 11, 2012 11:14 pm
([msg=70762]see Re: C++[/msg])

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...
Code: Select all
int main();
{
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t
cout <<"Please enter your first number /n";
cin >> a;
cin.ignore;
cout <<"Please enter your second number /n";
cin >> a;
cin.ignore;

... And so on until you get all of the numbers:
Code: Select all
int result;      //I'm just declaring a global variable
while (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.):

Code: Select all
/* Includes and such go here */
int main()
{
int c;
int result;
while(c <= 20)
{
int num;
cout << "Enter a number: " << endl;
cin >> num;
if(num > 0)
{
result += num;
}
c++;
}
cout << "And the final calculation is... " << result << "!" << endl;
return 0;
}


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
User avatar
centip3de
Moderator
Moderator
 
Posts: 1412
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: C++

Post by not_essence2 on Mon Nov 12, 2012 3:48 pm
([msg=70781]see Re: C++[/msg])

Dang, c3ntipede, got in right before I could fix my mistake.
Code: Select all
while(a>0);
{
result +=a;
a -=a;
}

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.
not_essence2
Poster
Poster
 
Posts: 189
Joined: Fri Sep 14, 2012 6:09 pm
Blog: View Blog (0)


Re: C++

Post by WallShadow on Mon Nov 12, 2012 6:16 pm
([msg=70792]see Re: C++[/msg])

not_essence2 wrote:Dang, c3ntipede, got in right before I could fix my mistake.
Code: Select all
while(a>0)
{
{
result +=a
a -=a
}
endl;
}

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:

Code: Select all
if (a > 0)
{
    result += a;
}


@cent I see what you did there with the c
User avatar
WallShadow
Contributor
Contributor
 
Posts: 594
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: C++

Post by centip3de on Mon Nov 12, 2012 9:20 pm
([msg=70796]see Re: C++[/msg])

not_essence2 wrote:Dang, c3ntipede, got in right before I could fix my mistake.
Code: Select all
while(a>0)
{
{
result +=a
a -=a
}
endl;
}

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
User avatar
centip3de
Moderator
Moderator
 
Posts: 1412
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: C++

Post by limdis on Mon Nov 12, 2012 9:25 pm
([msg=70799]see Re: C++[/msg])

Cent +1 to you too
"The quieter you become, the more you are able to hear..."
"Drink all the booze, hack all the things."
User avatar
limdis
Moderator
Moderator
 
Posts: 1310
Joined: Mon Jun 28, 2010 5:45 pm
Blog: View Blog (0)


Re: C++

Post by not_essence2 on Mon Nov 12, 2012 10:22 pm
([msg=70805]see Re: C++[/msg])

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.
not_essence2
Poster
Poster
 
Posts: 189
Joined: Fri Sep 14, 2012 6:09 pm
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests