one or more multiplied symbols

hello everybody
I cant find where the error is in my simple code ....
#include<iostream>
#include<conio.h>
using namespace std ;



int Num(int z){
int i = 0 ;
while(z % 10){
i++ ;
}
return i ;
}
int main(){
cout<<Num(56)<<endl ;
getch();
return 0 ;
}
Hey wadee,

next time please use code tag. It will make code simpler to read.

Your problem is that Num function will have infinite while loop in it.

While loop looks like this:

1
2
3
4
while(condition)
{
code
}


and if integer inside condition of while is equal to 0 - while loop stops.
Otherwise, it will run.
56 % 10 = 6. You never increment or change 56, so it will always check for result of 56%10. You have to change this part of code.

PS. Maybe for loop would fit better?
Last edited on
Just from the looks of this code I would guess you want to output the remainder with a function?

If so you could do this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<conio.h>
using namespace std ;



int Num(int z){
int i =  z % 10; 

return i ; 
}
int main(){
cout<<Num(56)<<endl ;
getch();
return 0 ; 
} 
MatthewRock ...... thank you



Manga ..... i am actually trying to cout the digits in any number
#include<iostream>
#include<conio.h>
using namespace std ;



int Num(int z){
int i = 0 ;
while(z % 10){
z = z / 10 ;
i++ ;
}
return i ;
}
int main(){
cout<<Num(56)<<endl ;
getch();
return 0 ;
}
but it also gives the same error
on your line of the while loop...

you have
while (z % 10) {

this creates an infinate loop. It is the same as saying while(true){

you need a condition that will break the loop.

something like while(z>1){

As soon as z is no longer greater than 1 the loop ends.
While the loop condition is incorrect, it is certainly not infinite or the same as saying while (true).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

unsigned count_digits(unsigned value, unsigned base = 10)
{
    unsigned n_digits = 0;
    do
    {
        ++n_digits;
        value /= base;
    } while (value);

    return n_digits;
}

void print_digit_count(unsigned value)
{
    std::cout << "In base 10, " << value << " has " << count_digits(value) << " digits.\n";
    std::cout << "In base 2, " << value << " has " << count_digits(value, 2) << " digits.\n";
    std::cout << "In base 16, " << value << " has " << count_digits(value, 16) << " digits.\n\n";
}

int main()
{
    print_digit_count(56);
    print_digit_count(0);
    print_digit_count(1);
    print_digit_count(128);
    print_digit_count(0xFFFF);
}


http://ideone.com/Le5YZs
ok, I admit you lost me on this one cire.

what does while(value) mean then if value is a number like 56?
while(value) returns true when value != 0.
Last edited on
Wadee, to use a code tag, just do this-

(code)
write whatever here
(/code)

Except use [ ], not ( ).

I couldn't use [ ] here, though, or it would have turned my example itself into a code tag. Lol. Hope that helps.
I think that is like what I said, cire said it is not like while(true).
ok, I admit you lost me on this one cire.

what does while(value) mean then if value is a number like 56?

The point is that if z changes during the body of the loop, then when while (z % 10) is evaluated, it may now evaluate to false, depending on how z has changed. If z % 10 evaluates to 0 for the new value of z, then that's equivalent to it evaluating to false, and so the loop will end.

Hence, not necessarily infinite.
Last edited on
Topic archived. No new replies allowed.