help with homework assignment

I need to write a program to read an integer number and find its number of digits. Example 3425 has 4 digits and 485567 has 6 digits.

The program should be able to test as many numbers without leaving the program and I can only use int or long int data type.

I need to test

Value ----- digits
1241 ----- 4
19 ----- 2
5684900 ----- 7
0 ----- 1
1267678901 ----- 10
-153 ----- 3
62000 ----- 5
4000000000 ----- invalid


I did a do, while loop. I had it working at one point, but changed it and cant remember how I got it working.

I believe I need to use a log10 which is x = x /10

I can’t remember what I did to make it work before, but this is what I have now and it doesn’t work properly.

int x;
int count = 0;

do
{
cout << “Enter the value for x : “ <<endl;
cin>> x;

x = x / 10;
count ++;

cout << x << count << endl;

}

while (x > 0) ;


}
Last edited on
Try taking the part that ask for the value of x out of the loop
doesn't help. This is what I get when I take the value of x out of the loop
Enter the value for x :
12345
12341
1232
123
14
05
Press any key to continue . . .
Oh my bad you have to take the cout function out also and in it insert a space between x and count because if you look closely and guessing your input was 12345 it happens that your are printing the loop so 1234 is x and 1 count... until you reach x = 0 and count = 5
I took cout out of loop and it was able to calculate the count correctly, but it doesn't display the correct x value.

Also, I think there is something wrog with my while part of the code becase it can't do negative numbers.

Enter the value for x :
384567
Number entered: 0
digits 6
Press any key to continue . . .
the x value isn't correct because as the loop goes it goes down until it reaches 0 to fix that create another variable and assign x value to it and call it at the end

since negative numbers are less than zero you will need to make a function that checks if the number is negative and multiply it by -1 before sending it to the loop
I think I understand what your saying. I tried to create another variable to assign to x value but I don't get any reading in the ouput.

y = x / 10;

cout << x <<endl;

all I get is

Enter the value for x :
12345

am I understanding you correctly when you said to create another variable?
just code a program if x less than or equal to 9 cout 1. and so on
I actually meant that when you get the value of x immediately create the other variable something like this
1
2
3
cout << “Enter the value for x : “ <<endl;
cin>> x;
int y = x;


then in the loop you use either one and for the cout the other
Thank you so much for all your help!!!! I really appreciate it! It's working now and I understand how we got there. Thanks again.
Last edited on
Why not convert that number to string and get the length of that ?
Just 2 lines of code instead of that loop.
we didn’t learned about stringing in class yet. My teacher is very particular about how we need to write our code. We just learned about looping and this is our 1st assignment on looping.
Last edited on
I have another problem with this homework assignment ---

I can't figure out how to loop it so that the user can test as many numbers as desired without leaving the program.

Also, I can't for the life of me figure out how make the program reject any input outside the long int range. My teacher didn't really explain any of this real well. Please can someone show me how to write this?

Everything I've tried doesn't come out right.

#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()

{

int x,y,s,n;
int count = 0;


//ofstream
cout<< "Enter the value for x : " <<endl;
cin>> x;

s = x;

do
{
if (x < 0)
{
x = x * -1;
}

x = x / 10;
count ++;

}

while (x > 0) ;
{}

cout << setw(20) << "Number entered" << setw (20) << "Digits" <<endl;
cout << setw(15) << s << setw (20) << count <<endl;

return (0);
}
Well, let's address validating the input first. The way formatted extraction works (cin >> x) there is always going to be something in the input stream after it exits. Normally, this will be a '\n', but if a user entered a number that was too large to fit into the variable you're reading into, there will still be digits in the input stream.

You can check to see what the next character in the input stream is without removing it by invoking the peek method on cin. The other thing that's going in in the code below is that we're checking to see if the input operation succeeded (ie. nobody borked up our input with letters.)

1
2
3
4
5
6
7
8
9
10
       bool input_is_valid = false ;
       while ( !input_is_valid )
       {
              cout << "Enter the value for x: \n" ;
              if ( cin >> x  &&  cin.peek() == '\n' )
                     input_is_valid = true ;
              else
                     while ( cin.get() != '\n' ) // remove unwanted stuff from the input stream.
                             ;  
       }


As for the other thing, you just need to wrap everything in another loop and ask the user if he wants to keep entering numbers to evaluate.
I see. Thanks for the explanation . I didn’t know that there was always something in the input stream after it exits. My teacher didn't go through any of that. Thanks for the help.
Topic archived. No new replies allowed.