Hey everyone, I'm new to C++ programming and have just started the semester and having a bit of trouble with my second homework assignment. Here's the assignment: Write a program that accepts a 7-9 digit integer andechoes the number with commas between every three
digits from the right. Here's what I have so far:

#include <iostream>

using namespace std;

int main() //Needed in every program, where program execution begins
{
      int number;
      
      cout << "Please enter a 7 to 9 digit number" << endl;
      cin >> number; 
      cout << "The number you entered is: " << number << endl;

      return 0; 
}

As you can see from my code above that my issue comes in when it comes to placing the commas. Would I use an if statement and mod division? I don't want you to do my homework, I want you to point me in the right direction. Thanks in advance for the help.

Recommended Answers

All 5 Replies

you could divide you 7-9 digit number into 3 numbers and place commas between them...

I don't want you to do my homework, I want you to point me in the right direction.

Ok, here are a few hints that you can bring together to do the program.

1: You can find the right side (least significant) digit for a number in any base by saying number % base. If the number is 123 and the base is 10, 123 % 10 gives you 3.

2: You can trim the least significant digit for a number in any base by saying number / base. I the number is 123 and the base is 10, 123 / 10 gives you 12.

3: You can find the number of digits in an integer by adding 1 to the floor of the base 10 logarithm of the absolute value of the integer.

int digits = 1;

if ( number != 0 ) {
  digits = 1 + (int)floor( log10( abs( number ) ) );
}

4: You can find the left side (most significant) digit for a number of any base by saying number / (int)pow( (double)base, digits ) .

5: You can trim the most significant digit for a number of any base by saying number % (int)pow( (double)base, digits ) .

6: A rotating counter is easy to write. Start at 0 and when it gets to the limit, set it back to 0.

int counter = 0;

cout<<"Starting count\n";
for ( int i = 0; i < 20; ++i ) {
  if ( ++counter == 3 ) {
    counter = 0;
    cout<<"\tLimit reached\n";
  }
}
cout<<"Stopping count\n";

7: The remainder of division is always in the range of the divisor. x / 10 will have a remainder of [0,9) regardless of x.

8: If the remainder is 0 then that means the dividend is perfectly divisible by the divisor and it's the same as if you had a counter that hit the limit of the divisor. In C++ese that means you can do this.

cout<<"Starting count\n";
for ( int i = 0; i < 20; ++i ) {
  if ( ( i + 1 ) % 3 == 0 ) {
    cout<<"\tLimit reached\n";
  }
}
cout<<"Stopping count\n";

i think it would be easier and the same results will be reached if hi truncates the number by dividing it:

#include <iostream>

using namespace std;

int main(){
   int n1,n2,n3;
   cout<<"Input a 7-9 digit number";
   cin>>n3;

1.- the leftmost number (the millions) can be obtained by dividing the whole number by 1,000,000... since the three variables are int, the decimals will be truncated and will be lost... then, multiply this number by 1,000,000 and substract it from the original one... that will get rid of the millions...

n1=n3/1000000;
   n3-=n1*1000000;

2.- for the middle number (the thousands), do the same: divide your resulting number from the last operation by 1,000, then multiply it by 1000 and substract it from the original one...

n2=n3/1000;
   n3-=n2*1000;

3.- the resulting number is your rightmost number(the hundreds).

next: output the number inserting commasbetween them...

cout<<"The number is: "<<n1<<","<<n2<<","<<n3<<endl;
   system("pause");
   return 0;
}

and it gives the same results as the one Hamrick suggested...

i think it would be easier and the same results will be reached if hi truncates the number by dividing it

That's the same thing I suggested except it's too specific to the problem. If this is homework then a good next project is to generalize the problem into a number of any length or to change the intervals that commas are added. If you use magic numbers, those next projects are even harder, but if you do it one of the ways I mentioned, the next projects just fall right out of the current solution. :)

Easier now usually means harder later. ;) If you do it right the first time, you don't have to undo it and then do it right the second time.

Thanks for all the replies. I got it and it works perfectly. Thanks for all your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.