numbers and digits

help with digits

Page 1 of 1

5 Replies - 2687 Views - Last Post: 03 September 2009 - 04:06 PM Rate Topic: -----

#1 philip soraghan  Icon User is offline

  • New D.I.C Head

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

numbers and digits

Post icon  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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

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

Re: numbers and digits

Posted 14 November 2008 - 07:14 PM

Please post your code examples using :code:


View Postphilip 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

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: numbers and digits

Posted 14 November 2008 - 07:16 PM

error: ambiguous call to overloaded question

Was This Post Helpful? 0
  • +
  • -

#5 philip soraghan  Icon User is offline

  • New D.I.C Head

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

Re: numbers and digits

Posted 15 November 2008 - 02:17 PM

View Postn8wxs, on 14 Nov, 2008 - 06:14 PM, said:

Please post your code examples using :code:


View Postphilip 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.

Was This Post Helpful? 0
  • +
  • -

#6 muhammadnur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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']
Please post your code examples using :code:


[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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1