tags:

views:

117

answers:

2

Hi i have a problem with this program. Its not too complex, but frustrating. Here is the question......Write a complete c++ program to find the sum of first 5 odd numbers using while loop. Make sure to write an output statement to show the sum. Hint: Variable sum needs to be initialized to 0. Here is what I have so far

#include <iostream>
using namespace std; 
int main () 

{ 
    int x, b,c;
     x= 1;
     b=1;
     c=0;

    do 

    {

    x+=2;

    cout<<x<<endl;
}

    while (x<=5);

   {
          c= c +x;

          x++;
          cout<<x<<endl;




    system ("pause"); 

    return 0; 
+6  A: 
In silico
+1 for an educational answer
schnaader
The original code he posted uses a do-while loop, so that `{` seems to just be there in the abyss. The original code should not compile since it has a dangling open brace. Regardless, good answer, +1.
birryree
@kuropenguin: Ah, I didn't see that. The non-matching braces and the `x++` threw me off. Thanks for pointing that out.
In silico
I disagree with "you should initialize all your variables as soon as they spring into existence". I think his style is neater.
Alexander Rafferty
@Alexander Rafferty: It's not a style issue. The OP's code and my code are in fact not equivalent. The OP's code performs initialization, then assignment. My code performs initialization only. In general, you should declare variable right before they're used, then initialize them to some value. Also, I've fixed many, many bugs caused by variables not being initialized before they're used.
In silico
I see what you mean. It just looks neater having them declared on one line: int x=1, b=0, c=0;
Alexander Rafferty
@Alexander Rafferty: Ah, okay. You can do it like that too - initialization was the only thing I was worried about.
In silico
A: 

Here's one way:

#include <iostream>
using namespace std; 
int main () 

{ 
    int x,b,c;
     x = 1;
     b = 1;
     c = 0;

    while (b <= 5) { 

       c = c + x;

       x+=2;
       b++;
    }
    cout<<x<<endl;

    system ("pause"); 

    return 0; 
}
Lance Roberts
-1 for doing his homework for him
meagar
If people need help, I'll help them, and he definitely needed help.
Lance Roberts
@Lance You haven't helped him; you've only harmed him by doing the work for him.
meagar
-1 for doing his homework for him, and doing it in poor style: the `system` call is unnecessary and unportable, that should be a `for` loop (!), there are too many variables, the formatting is pretty funky, …
Potatoswatter
@Potatoswatter, the OP specifically asked for a `while` loop, I would always use a for loop in my code. I'm amazed how many people are __cold-hearted__ towards those who need homework help, I won't allow myself to be that way. I'm on my third degree now and I always appreciated help.
Lance Roberts
Except in this case, In silico posted his answer a minute before you, and he's not coldhearted by any stretch. Nor does he simply provide an answer with no *real* help.
Potatoswatter
There's dozens of meta posts about this, including an [faq entry](http://meta.stackoverflow.com/questions/10811/how-to-ask-and-answer-homework-questions) that specifically says "Don't downvote others who answer homework questions in good faith, even if they break these guidelines". If you think the code itself is bad like Potatoswatter then go for it, but downvoting *because* it includes code is fairly ridiculous
Michael Mrozek