I need help quick!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 4
Reputation: thinfineline is an unknown quantity at this point 
Solved Threads: 0
thinfineline thinfineline is offline Offline
Newbie Poster

I need help quick!!

 
0
  #1
Oct 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: I need help quick!!

 
0
  #2
Oct 23rd, 2008
You're doing input of num twice in the loop, but only adding it once.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: I need help quick!!

 
0
  #3
Oct 23rd, 2008
You should use while loop, not for.

And you haven't declared 'two' and 'zero'.
Why don't you simply put 2 and 0?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: thinfineline is an unknown quantity at this point 
Solved Threads: 0
thinfineline thinfineline is offline Offline
Newbie Poster

Re: I need help quick!!

 
0
  #4
Oct 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: I need help quick!!

 
0
  #5
Oct 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: thinfineline is an unknown quantity at this point 
Solved Threads: 0
thinfineline thinfineline is offline Offline
Newbie Poster

Re: I need help quick!!

 
0
  #6
Oct 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: I need help quick!!

 
0
  #7
Oct 23rd, 2008
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:

  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.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC