• 05-04-2004
CheyenneWay
Hello all,

Im new here and was hoping to get a bit of help with a program i am writing for class. The program has the user enter a start number then a ending number. Then displays only the Odd number. Ive been successful up to that point. The next part has been giving me a bit of a headache. Can you help?

After the odd numbers are displayed the program needs to Add those numbers together and display the sum. So if the user enters in 1 and 10 it will display 1,3,5,7,9 then 25 for the total sum. Any guidance would be much appreciated and I hope to hang around here and help anyone that might need it in the future.

Code:

```#include <iostream.h> #include <ctype.h> #include <math.h> void main() {         char doAgn = 'Y';         int startNum= 0;         int endNum = 0;         int oddCount = 0;         int sum = 0;         int sumNum = 0;                 while (toupper(doAgn) =='Y'){                 //get inputs                 cout<<"Please enter the beginning number:";                 cin>>startNum;                 cout<<"Please enter the ending number:";                 cin>>endNum;                 //display the odd numbers                                 for (int i = startNum; i < endNum; i++){                         oddCount++;                                                if(oddCount % 2 !=0)                         cout<<oddCount<<endl;                 }//end For                 //display the total sum for the odd numbers displayed                 ????????                 //display the average for all the odd numbers displayed                 //hopefully i will grasp this after understanding the above line of code.                                         //clear out odd count                 oddCount = 0;         }//end while }//end main```
• 05-05-2004
skorman00
keep track of the sum in a variable. Keep on adding to it whenever you find an odd number
• 05-05-2004
CheyenneWay
thats what i figured needs to happen. The problem for me is how do i code after the (oddCount % 2 !=0) finds the odd number once then again "then make those two numbers add" up that I can store in a variable. thanks for the point in the right direction skorman00.
• 05-05-2004
skorman00
if that test comes out to true, you know you have an odd number right? In addition to displaying it, also add it to the running sum.
• 05-05-2004
bigSteve
You wrote:

for (int i = startNum; i < endNum; i++){
oddCount++;
if(oddCount % 2 !=0)
cout<<oddCount<<endl;

Try this:-

for (int i = startNum; i < endNum; i++)
{

if(oddCount % 2 !=0)
{
oddCount++;
cout<<oddCount<<endl;
}
}

Hope this helps,

BigSteve
• 05-05-2004
CheyenneWay
thank you for your help. I'll keep thinking this one through :)
• 05-05-2004
CheyenneWay
oh i see bigSteve let me try that real quick
• 05-05-2004
CheyenneWay
well now that doesnt display my oddCount ... i guess this is one to sleep on or something
• 05-05-2004
skorman00
that threw you into an infinite loop. You only incrimented oddCount if it was odd, so it would just stop incrimenting if it became even. I'll now point out that you are using oddCount to keep track of the odd numbers, which starts at 0, not the number you got from the user. Keep track of that with i instead, which you have set up properly.
• 05-05-2004
biosx
What is the point of oddCount?

Here is some pseudo-code/logic to your program. I hope it helps.

Code:

```while still getting input {         declare total variable           get starting number         get ending number         for i = from starting-number to ending-number         {                 if i is an odd number                 {                         print i                         total += i                 }         }         print total }```
• 05-05-2004
CheyenneWay
Thanks peoples for the help. My c++ book doesnt have a thing about the += operator in it :( Thats what was causing me the problems. How would you achieve the same effect without it?
• 05-06-2004
laserlight
total = total + i
• 05-06-2004
CheyenneWay
thanks laserlight :)