how to save big number into array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 12
Reputation: harryoma is an unknown quantity at this point 
Solved Threads: 0
harryoma harryoma is offline Offline
Newbie Poster

how to save big number into array

 
0
  #1
Dec 28th, 2008
hi everyone
i'm am having a problem, can you help me how to save big number into array?
i have to input two big numbers (20 digits or less) and sum them.
please help
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,925
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: how to save big number into array

 
0
  #2
Dec 28th, 2008
Have you any code?
What are the problems you have with it?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 12
Reputation: harryoma is an unknown quantity at this point 
Solved Threads: 0
harryoma harryoma is offline Offline
Newbie Poster

Re: how to save big number into array

 
0
  #3
Dec 28th, 2008
i have to input number, and then save that number so that evey digit is one element of array...

this is my code so far

cout<<"Input number:";
cin>>n;
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,925
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: how to save big number into array

 
0
  #4
Dec 28th, 2008
First declare an array and put your cin in a loop to read in every digit of your bignumber. Check if it's a digit and put it in the array.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 12
Reputation: harryoma is an unknown quantity at this point 
Solved Threads: 0
harryoma harryoma is offline Offline
Newbie Poster

Re: how to save big number into array

 
0
  #5
Dec 28th, 2008
ok, my code looks like this:


#include<iostream>
using namespace std;

int main(){
int number[20];
int n,j;

cout<<"How many digits have your number: ";
cin>>j;
cout<<"Input bigdigit: ";

for(int i=0;i<j;i++){
cin>>n;
number[i]=n;
}
for(int i=0;i<j;i++){
cout<<number[i];
}



return 0;

but how do i enter number without space;
i would like to enter 12345679, now i have to enter 1 2 3 4 5 6 7 9
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,925
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: how to save big number into array

 
0
  #6
Dec 28th, 2008
You can read in a string like this :

string Mystr;
cout << "What's your name? ";
getline (cin, Mystr);
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 12
Reputation: harryoma is an unknown quantity at this point 
Solved Threads: 0
harryoma harryoma is offline Offline
Newbie Poster

Re: how to save big number into array

 
0
  #7
Dec 28th, 2008
but getline only works with letters not with numbers
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: how to save big number into array

 
0
  #8
Dec 28th, 2008
You could use atoi or write your own char-to-digit converter, like this:
  1. int char2int(char c) {
  2. switch(c) {
  3. case '0': {
  4. return 0;
  5. }
  6. case '1': {
  7. return 1;
  8. }
  9. // ... and so on
  10. }
  11. }

You could also do it without the switch, but I'll let you figure out how.

P.s. Please use code tags when posting code!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 12
Reputation: harryoma is an unknown quantity at this point 
Solved Threads: 0
harryoma harryoma is offline Offline
Newbie Poster

Re: how to save big number into array

 
0
  #9
Dec 28th, 2008
this looks complicated, i need simpler solution
please help


p.s. sorry, i will use code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: how to save big number into array

 
0
  #10
Dec 28th, 2008
I hope this is not homework, so I'll show you a little more (it's simpler than it seems!)
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int char2int(char c) {
  6. switch(c) {
  7. case '0': {
  8. return 0;
  9. }
  10. case '1': {
  11. return 1;
  12. }
  13. case '2': {
  14. return 2;
  15. }
  16. case '3': {
  17. return 3;
  18. }
  19. case '4': {
  20. return 4;
  21. }
  22. case '5': {
  23. return 5;
  24. }
  25. case '6': {
  26. return 6;
  27. }
  28. case '7': {
  29. return 7;
  30. }
  31. case '8': {
  32. return 8;
  33. }
  34. case '9': {
  35. return 9;
  36. }
  37. }
  38. }
  39.  
  40. int main() {
  41. string myBigNum;
  42. int myBigNumArray[20];
  43. cout << endl << "Enter the number" << endl;
  44. getline(cin, myBigNum);
  45. if(myBigNum.size()>20) {
  46. cout << endl << "You entered a number too big!" << endl;
  47. return EXIT_FAILURE;
  48. }
  49. for(unsigned int i = 0; i < myBigNum.size(); ++i) {
  50. myBigNumArray[i] = char2int(myBigNum[i]);
  51. }
  52. // now you have your number saved in the array
  53. // of course you'll need sign checking and a better input validation
  54. // but this is a start and it's simple
  55. return EXIT_SUCCESS;
  56. }
Last edited by mrboolf; Dec 28th, 2008 at 12:04 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC