# numbers and digits

Page 1 of 1

## 5 Replies - 3023 Views - Last Post: 03 September 2009 - 04:06 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=71962&amp;s=0df6f1a461218f9b93c14b09a47bb504&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 philip soraghan

Reputation: 0
• Posts: 9
• Joined: 03-April 08

# numbers and digits

Posted 14 November 2008 - 07: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;
}

Is This A Good Question/Topic? 0

## Replies To: numbers and digits

### #2 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## 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 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: numbers and digits

Posted 14 November 2008 - 07:14 PM

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 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: numbers and digits

Posted 14 November 2008 - 07:16 PM

```error: ambiguous call to overloaded question
```

### #5 philip soraghan

Reputation: 0
• Posts: 9
• Joined: 03-April 08

## Re: numbers and digits

Posted 15 November 2008 - 02:17 PM

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

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.

Reputation: 0
• Posts: 1
• Joined: 03-September 09

## 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
```#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.