Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,228 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,294 people online right now. Registration is fast and FREE... Join Now!




Digit Counting Program Help

 
Reply to this topicStart new topic

Digit Counting Program Help

oblate
18 Nov, 2007 - 09:36 PM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 13


My Contributions
I need to write a program that ask for a number to be input in and then I output the amount of digits in the number by finding out the amount of times the number is divided by 10 until the quotient is 0. So I have to make sure the inputted number is positive using a do..while statement. I made the program like this but its not working it takes in the value and does nothing.

CODE


void main()
{  int num, count, quotient;

    
        cout<< "Please enter a positive integer";
cin>> num;

do
{

    cout=0;

quotient=num/10;
    if(quotient>0) count=count +1;
}

while(quotient>=0);




cout<<count;





}
Any help will be appreciated.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Digit Counting Program Help
18 Nov, 2007 - 09:56 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Well, first off, this code could not have compiled. You can't assign a value to the cout object, as you do in the first line of the do-while loop. If you're going to post code, you're probably best off copying and pasting the actual source code you're working from, as typos are one of the biggest error sources, and typos just get magnified when you manually transcribe the code into your post.

With the value assigned to count instead of cout, the program compiles, and it actually does quite a bit - it enters into an infinite loop. The main reason for this is that the value of your variable num never changes - it's always the value that the user entered. So when you assign quotient=num/10, it's always got the same value, and the termination condition for the loop is never reached. To get around this, you could assign quotient the value of num prior to entering the loop, and assign it the value of itself divided by 10 within the loop:
CODE

quotient=num;
do {
    quotient=quotient/10;
    //....etc
}


However, although this will work to get the loop to execute properly, you won't get the correct number of digits in the number. THis is because you reassign the variable count a value of 0 every time through the loop. Move that assignment to a point before the loop, and you'll be on the right track. You should probably also move the quotient=quotient/10 statement that I suggested to a point after you conditionally increment the count variable. If you keep the current order of the assignment and increment steps, you'll always wind up with one fewer digits than the number actually has (can you see why?).

Hope that helps smile.gif

-jjh

User is offlineProfile CardPM
+Quote Post

oblate
RE: Digit Counting Program Help
20 Nov, 2007 - 10:42 PM
Post #3

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 13


My Contributions
nice got it to work. Thanks a lot man.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Digit Counting Program Help
20 Nov, 2007 - 10:50 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Good to hear icon_up.gif Glad it's working for you now.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Digit Counting Program Help
21 Nov, 2007 - 04:48 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Although it looks like your assignment probably asked you to use division, there is another way to calculate the number of digits in an integer:

digits = (int)(log10(number))+1


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 06:04AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month