-1

So I've to write a simple program(that loops) where you can enter an int and it spews out the number count and the sum of the numbers. Since I am such a tard when it comes to programming, I just scavenged the code online and tried to piece it together. I guess the sum block screws with n, but I am not really sure. Anyway, I would really appreciate it if somebody could point out mistakes and show me how can I make it work.

#include <iostream>
using namespace std;

int main()
{
    while(1)
    {
        int i,p,n,sum=0;  //sum block
        cout<<"enter an int: ";
        cin>>n;

        {
            while(n!=0)
            {
                p=n % 10;
                sum+=p;
                n=n/10;
            }
            cout<<"int digit sum: "<<sum <<endl;
        }
        {
            int count = 0;
            while(n)
            {
                n /= 10;
                ++count;
            }
            cout <<"number of digits: " << count << '\n';
        }
    }
}
Useless
  • 64,155
  • 6
  • 88
  • 132
user1710386
  • 23
  • 1
  • 1
  • 2
  • 3
    You should start with learning how to at least format your code to make it readable for you and others... –  Sep 30 '12 at 21:33
  • Your outer while will never terminate. `while(1)` doesn't stop unless there is a break. – David G Sep 30 '12 at 21:34
  • "Your outer while will never terminate. while(1) doesn't stop unless there is a break." yeah, I intended that. I need the program too loop back at the beginning when all values have been returned – user1710386 Sep 30 '12 at 21:38

4 Answers4

3

Since the loops that you are using are destructive (i.e. they make n go to zero by the end of the loop) you need to combine the two loops into one:

int sum=0, count=0;
while(n!=0)
{
    count++;
    sum += n%10;
    n /= 10;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You need to save a copy of n before the first loop to use it in the second loop.

stark
  • 12,615
  • 3
  • 33
  • 50
0

Use this code, if you are trying to get the sum of digits and no. of digits in the number.

#include <iostream>
using namespace std;

int main()
{

    int i, p, n, sum=0, count = 0;  //sum block
    cout<<"enter an int: ";
    cin>>n;

    while(n!=0)
    {
        p=n % 10;
        sum+=p;
        count++;
        n=n/10;
    }

    cout<<"int digit sum: "<<sum<<endl;
    cout<<"count of digits: "<<count<<endl;
 }

Your second while loop while(n) will never execute as the value of n till then would have become 0.

akaHuman
  • 1,332
  • 1
  • 14
  • 33
0

I guess the sum block screws with n, but I am not really sure

you guess right: you change n inside that first nested loop. You don't exit that loop until n is zero ... so now it's zero!

For reference, I'd probably structure it more like this, to avoid having to keep an explicit copy around ...

#include <iostream>
using namespace std;

int sum_of_digits(int n);
int num_of_digits(int n);

int main()
{
    while(1)
    {
        cout << "enter an int: ";
        int n;
        cin >> n;

        cout << "sum of digits: " << sum_of_digits(n) << endl;
        cout << "num of digits: " << num_of_digits(n) << endl;
    }
}

int sum_of_digits(int n)
{
    int sum = 0;
    while(n)
    {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}

int num_of_digits(int n)
{
    int count = 0;
    while(n)
    {
        ++count;
        n /= 10;
    }
}
Useless
  • 64,155
  • 6
  • 88
  • 132