Hi whassup i need help with this i need to get the sum of the integers entered and output the individual digits of the number i have the sum got ok but i cant get the individual digits of the number with spaces between them like 34567 has to be 3 4 5 6 7 this is what i have i keep getting 0 at the last statement

#include <cstdlib>

#include <iostream>

#include <math.h>

using namespace std;

int main(int argc, char *argv[])

{

int numdigits;

int lastdigit;

int number;

int sum;

int count;

int firstdigit;

int temp;

int digit;

cout<<"The number of digits to be entered is "<<endl;

cin>>numdigits;

cout<<"The integer entered is "<<endl;

cin>>number;

sum =0;

count =0;

while (number != 0) {

lastdigit = number % 10;

number = number / 10;

sum = sum + lastdigit;

count ++;

}

temp = number;

while(numdigits != 0){

digit = (temp / ((int)pow((double)10, (double)(numdigits - 1)))) % 10;

numdigits = numdigits - 1;

}

cout<<"The sum of the number of digits entered is " <<sum<<endl;

cout<<"The digits entered with spaces is " <<numdigits<<endl;

system("PAUSE");

return EXIT_SUCCESS;

}

## 5 Replies - 3339 Views - Last Post: 03 September 2009 - 04:06 PM

##
**Replies To:** numbers and digits

### #2

## Re: numbers and digits

Posted 14 November 2008 - 07:09 PM

I see some problems with this code. Rather, bad design choices that over complicate the assignment. Create an array with the length of the # of digits of the number. After determining each digit place it in the corresponding array index. Then iterate through the array with a space between each number.

### #3

## Re: numbers and digits

Posted 14 November 2008 - 07:14 PM

**Please post your code examples using**

philip soraghan, on 14 Nov, 2008 - 06:00 PM, said:

Hi whassup i need help with this i need to get the sum of the integers entered and output the individual digits of the number i have the sum got ok but i cant get the individual digits of the number with spaces between them like 34567 has to be 3 4 5 6 7 this is what i have i keep getting 0 at the last statement

#include <cstdlib> #include <iostream> #include <math.h> using namespace std; int main(int argc, char *argv[]) { int numdigits; int lastdigit; int number; int sum; int count; int firstdigit; int temp; int digit; cout<<"The number of digits to be entered is "<<endl; cin>>numdigits; cout<<"The integer entered is "<<endl; cin>>number; sum =0; count =0; while (number != 0) { lastdigit = number % 10; number = number / 10; sum = sum + lastdigit; count ++; } temp = number; while(numdigits != 0){ digit = (temp / ((int)pow((double)10, (double)(numdigits - 1)))) % 10; numdigits = numdigits - 1; } cout<<"The sum of the number of digits entered is " <<sum<<endl; cout<<"The digits entered with spaces is " <<numdigits<<endl; system("PAUSE"); return EXIT_SUCCESS; }

So you want the input entered in response to the first prompt to be "1 2 3 4 5" instead of "12345"?

Or do you want the output to be "1 2 3 4 5"?

Or Both?

You keep getting zero for the spaces output because you are decrementing numdigits to zero.

This post has been edited by **n8wxs**: 14 November 2008 - 07:17 PM

### #4

## Re: numbers and digits

Posted 14 November 2008 - 07:16 PM

error: ambiguous call to overloaded question

### #5

## Re: numbers and digits

Posted 15 November 2008 - 02:17 PM

n8wxs, on 14 Nov, 2008 - 06:14 PM, said:

**Please post your code examples using**

philip soraghan, on 14 Nov, 2008 - 06:00 PM, said:

Hi whassup i need help with this i need to get the sum of the integers entered and output the individual digits of the number i have the sum got ok but i cant get the individual digits of the number with spaces between them like 34567 has to be 3 4 5 6 7 this is what i have i keep getting 0 at the last statement

#include <cstdlib> #include <iostream> #include <math.h> using namespace std; int main(int argc, char *argv[]) { int numdigits; int lastdigit; int number; int sum; int count; int firstdigit; int temp; int digit; cout<<"The number of digits to be entered is "<<endl; cin>>numdigits; cout<<"The integer entered is "<<endl; cin>>number; sum =0; count =0; while (number != 0) { lastdigit = number % 10; number = number / 10; sum = sum + lastdigit; count ++; } temp = number; while(numdigits != 0){ digit = (temp / ((int)pow((double)10, (double)(numdigits - 1)))) % 10; numdigits = numdigits - 1; } cout<<"The sum of the number of digits entered is " <<sum<<endl; cout<<"The digits entered with spaces is " <<numdigits<<endl; system("PAUSE"); return EXIT_SUCCESS; }

So you want the input entered in response to the first prompt to be "1 2 3 4 5" instead of "12345"?

Or do you want the output to be "1 2 3 4 5"?

Or Both?

You keep getting zero for the spaces output because you are decrementing numdigits to zero. THanks for your help i would like the ouput to be 1 2 3 4 5 what to you suggest about not decrementing the numdigits to zero.

### #6

## Re: numbers and digits

Posted 03 September 2009 - 04:06 PM

[quote name='philip soraghan' date='15 Nov, 2008 - 01:17 PM' post='465370']

[quote name='n8wxs' post='465006' date='14 Nov, 2008 - 06:14 PM']

[quote name='philip soraghan' post='464999' date='14 Nov, 2008 - 06:00 PM']

Hi whassup i need help with this i need to get the sum of the integers entered and output the individual digits of the number i have the sum got ok but i cant get the individual digits of the number with spaces between them like 34567 has to be 3 4 5 6 7 this is what i have i keep getting 0 at the last statement

This is an old topic but just in case someone else comes along looking for the same thing.

The two mistakes I found are the placing of

where he has it number is already 0 due to the first while loop. He should put it above the first while loop with the other variables he declared.

and the last cout statement should be within the second while loop and he should have but digit instead of numdigits as his output variable, like this:

And to answer a previous posters question, the reason we are not using arrays or anything like that, (I'm assuming the OP was doing this as a part of homework) at this point in the intro to programming class arrays have not been introduced so we have to make the program based on the material given up to this point.

[quote name='n8wxs' post='465006' date='14 Nov, 2008 - 06:14 PM']

**Please post your code examples using**[quote name='philip soraghan' post='464999' date='14 Nov, 2008 - 06:00 PM']

Hi whassup i need help with this i need to get the sum of the integers entered and output the individual digits of the number i have the sum got ok but i cant get the individual digits of the number with spaces between them like 34567 has to be 3 4 5 6 7 this is what i have i keep getting 0 at the last statement

#include <cstdlib> #include <iostream> #include <math.h> using namespace std; int main(int argc, char *argv[]) { int numdigits; int lastdigit; int number; int sum; int count; int firstdigit; int temp; int digit; cout<<"The number of digits to be entered is "<<endl; cin>>numdigits; cout<<"The integer entered is "<<endl; cin>>number; sum =0; count =0; while (number != 0) { lastdigit = number % 10; number = number / 10; sum = sum + lastdigit; count ++; } temp = number; while(numdigits != 0){ digit = (temp / ((int)pow((double)10, (double)(numdigits - 1)))) % 10; numdigits = numdigits - 1; } cout<<"The sum of the number of digits entered is " <<sum<<endl; cout<<"The digits entered with spaces is " <<numdigits<<endl; system("PAUSE"); return EXIT_SUCCESS; }

This is an old topic but just in case someone else comes along looking for the same thing.

The two mistakes I found are the placing of

temp=number;

where he has it number is already 0 due to the first while loop. He should put it above the first while loop with the other variables he declared.

and the last cout statement should be within the second while loop and he should have but digit instead of numdigits as his output variable, like this:

while(numdigits != 0){ digit = (temp / ((int)pow((double)10, (double)(numdigits - 1)))) % 10; cout<<digit<<" "; numdigits = numdigits - 1; }

And to answer a previous posters question, the reason we are not using arrays or anything like that, (I'm assuming the OP was doing this as a part of homework) at this point in the intro to programming class arrays have not been introduced so we have to make the program based on the material given up to this point.

Page 1 of 1