943,522 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 577
  • C++ RSS
Oct 23rd, 2008
0

I need help quick!!

Expand Post »
Ok, I'm writing a simple for loop with an accumulator for my class tonight and can't figure out what I'm doing wrong. I want the program to accept the first 10 even numbers from the keyboard and add them up and display an error message if someone enters an odd number, heres what I've got so far and the only real problem I'm having is getting the numbers to add up correctly. If some one could give me a quick response it would be great... Thanks so much.

// This program will accept and sum the first 10 even numbers as read from the keyboard.
#include <iostream>
using namespace std;
//constants
const int terminatingvalue= 10;const int two=2;const int zero=0;

int main ()
{
//Variables
int ctr=1;int num;int sum=0;

// procces
for (int ctr=1; ctr<=10; ctr++)
{
cout<<"Enter a number"<<endl;
cin>>num;
if (num%two==zero)
{
sum=sum+num;
ctr++;
}
else
cout<<"Please enter an even number"<<endl;

cout<<"Enter a number"<<endl;
cin>>num;

}
cout<<"Total is:"<<sum<<endl;
//output

return 0;
}
Last edited by thinfineline; Oct 23rd, 2008 at 5:55 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thinfineline is offline Offline
4 posts
since Oct 2008
Oct 23rd, 2008
0

Re: I need help quick!!

You're doing input of num twice in the loop, but only adding it once.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 23rd, 2008
0

Re: I need help quick!!

You should use while loop, not for.

And you haven't declared 'two' and 'zero'.
Why don't you simply put 2 and 0?
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 23rd, 2008
0

Re: I need help quick!!

My teacher said it had to be a for loop and had to declare 2 and 0 and type them out (yeah she's a little nuts). Take the second num input out worked for getting it to add correctly but now my counters off it's only accepting 6 numbers not 10.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thinfineline is offline Offline
4 posts
since Oct 2008
Oct 23rd, 2008
0

Re: I need help quick!!

because your for loop is counting for you, and still, you are counting for yourself in
if (num%two==zero)
{
sum=sum+num;
ctr++; }
Last edited by Sci@phy; Oct 23rd, 2008 at 6:18 pm.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 23rd, 2008
0

Re: I need help quick!!

That fixed it. I'm really sorry about the stupid questions, this is my first programming class and I'm having a really hard time with it. Thanks a bunch.

Wes
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thinfineline is offline Offline
4 posts
since Oct 2008
Oct 23rd, 2008
0

Re: I need help quick!!

If you leave it as is, it will only perform 10 repetitions of the loop, including invalid inputs. Then you'd only get the first 10 - (number of errors) even integers in your summation. My suggestion is this:

C++ Syntax (Toggle Plain Text)
  1. for (int ctr=1; ctr<=10; )
  2. {
  3.  
  4. cout<<"Enter a number"<<endl;
  5. cin>>num;
  6. if (num%two==zero)
  7. {
  8. sum=sum+num;
  9. ctr++; // only increment when a valid summation occurs
  10. }
  11. else
  12. cout<<"Please enter an even number"<<endl;
  13.  
  14. cout<<"Enter a number"<<endl;
  15. cin>>num;
  16.  
  17. }

Let me explain the for structure I used here.
for (int ctr=1; ctr<=10; )

- your original for loop incremented ctr on each run (int ctr=1; ctr<=10; ctr++)
- what I'm doing different is removing the incrementing of ctr from every run, so that
invalid numbers that are input (odds) do not count toward the number of repetitions
- this way you get 10 total numbers and not a lower number
Last edited by chococrack; Oct 23rd, 2008 at 7:36 pm.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ calender
Next Thread in C++ Forum Timeline: Reading Specific Ints and Chars from a String





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC