im trying to make a counter to count the number of odd numbers in an integer entered by a user but somehow my loop doesnt seem to go on as i hoped it would...im trying practise using the while loop that some of u guys demonstrated on my other thread...

#include <iostream>
using namespace std;

int main()
{
    int oddNum, num, oddCount = 0;
    cout << "enter an integer: " << endl;
    cin >> num;
    
    
    while (oddNum != 0 && num > 10)
    {
          num = num % 10;
          oddNum = num % 2;
          oddCount++;
    }
    
    cout << "number of odds is " << oddCount << endl;
}

let me guess. your while does not complete even one loop right? that is because your logic for the loop is all wrong. in addition to that it is needlesly complex.

int counter = 0;

while ( counter <= numberuserentered )
{
    counter++;

that should get your loop going from zero until the number that the user entered. now all you need to do is check counter every time to see if it is an odd number. if it is then you add one to some accumulator.

then you close the loop, diplay and exit. check logic first. programming is all about logic.

The definition of the problem is not clear (at least to me).

Given the decimal integer, 523456

Is the answer
1) 3
Becuase there are 3 odd digits (5,3, & 5)

2) 2
Becuase there are 2 unique odd digits (5 & 3)

3) 7
Becuase there are 7 unique odd numbers (5, 3, 523, 52345, 23, 2345 & 345)

4) 261728
Becasue there are 261728 odd numbers before 523456. (1,3,5,7,9,11,13...523455)

Or something else?

Comments
Good point you are making

u can make it something like that
when the no is not divisible by 2 then make an increment in the counter..
n kept on checking eveyno..

im trying to find the number of odd digits in a number. eg if i enter 12345, the output should be something like 3 odd numbers. but somehow.....theres something not right about my loop i cant figure out...

and i dont understand the loop from Ravenous Wolf....coz if lets say i enter the number 893747457......then the loop would have to count all the way to 893747457??

and sweety0 im attempting to follow that method in my code.....as in when integer % 2 != 0

Well if you want to know about digits I usually convert the int into a string. Then you can access each letter in the array. You can use a foreach() loop.

int counter = 0;
string tempstring = ""+ numberentered;
foreach(char x in tempstring){
if((x=='1')||(x=='3')||(x=='5')||(x=='7')||(x=='9'))){
counter++;
}
}

or in a while loop

int counter = 0;
string tempstring = ""+ numberentered;
int i =0;
char x = 'a';
while(i < tempstring.Length){
x = tempstring[i];
if((x=='1')||(x=='3')||(x=='5')||(x=='7')||(x=='9'))){
counter++;
}
}

There is probably a faster way to get rid of the multiple or's in the if statement. I would go with the foreach loop. Which just does the while stuff for you really.

:)

Comments
There is no such thing as for each in standard c++

I think you are close to your goal:

#include <iostream>
using namespace std;

int main()
{
    int oddNum, num, oddCount = 0;
    cout << "enter an integer: " << endl;
    cin >> num;
    
    
    while (oddNum != 0 && num > 10)
    {
          num = num % 10;
          oddNum = num % 2;
          oddCount++;
    }
    
    cout << "number of odds is " << oddCount << endl;
}

You don't want this as your control loop though:

while (oddNum != 0 && num > 10)

This bails you out of the loop too soon (whenever you hit your first even number). Instead use this:

while (num > 0)   // what about one digit numbers?

Also you want to change to divide num by 10 each time, not take a remainder. You'll need an "if" statement in the loop to check oddNum.

C++ does not have foreach. It has for_each in one of its libraries, but I don't think you want to use it here.

#include <iostream>
using namespace std;

int main()
{
    int [B]oddNum[/B], num, oddCount = 0;
    cout << "enter an integer: " << endl;
    cin >> num;
    
    
    while ([B]oddNum[/B] != 0 && num > 10)
    {
          [B]num = num % 10;[/B]
          oddNum = num % 2;
          [B]oddCount++;[/B]
    }
    
    cout << "number of odds is " << oddCount << endl;
}

1) You are using oddNum variable without first initialize it.
2) Normally, c = a % b , c must smaller than b . So, num = num % 10 , num will be alway smaller than 10, which make your loop condition false. I think you should change it to num = num / 10 3) oddNum = num % 2; , whenever num is an even number, the loop condition will be become false. Your goal is to run through every digit of the number and count the odd number. However, whenever you encounter the even number, your loop will stop.
4) oddCount++;[ , Without checking whether that digit is an odd number or even number, you keep counting every digit.

My solution would be:

while(num>0) {
  // Whenever the numb is an odd number, it will return 1.
  // In C++, all the non-zero numbers mean true and
  // zero means false. I put this condition to check the
  // current digit whether it is an odd number or even number
  if (num%2) oddCount++;

  // Move to the next digit.
  num = num / 10;
}

You also able to do it without using condition inside the loop too:

while(num>0) {
   oddCount += num % 2;
   num = num / 10;
}
This article has been dead for over six months. Start a new discussion instead.