943,810 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1941
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 28th, 2008
0

how to save big number into array

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harryoma is offline Offline
12 posts
since Dec 2008
Dec 28th, 2008
0

Re: how to save big number into array

Have you any code?
What are the problems you have with it?
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008
Dec 28th, 2008
0

Re: how to save big number into array

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;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harryoma is offline Offline
12 posts
since Dec 2008
Dec 28th, 2008
0

Re: how to save big number into array

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.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008
Dec 28th, 2008
0

Re: how to save big number into array

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harryoma is offline Offline
12 posts
since Dec 2008
Dec 28th, 2008
0

Re: how to save big number into array

You can read in a string like this :

string Mystr;
cout << "What's your name? ";
getline (cin, Mystr);
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008
Dec 28th, 2008
0

Re: how to save big number into array

but getline only works with letters not with numbers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harryoma is offline Offline
12 posts
since Dec 2008
Dec 28th, 2008
0

Re: how to save big number into array

You could use atoi or write your own char-to-digit converter, like this:
c++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 28th, 2008
0

Re: how to save big number into array

this looks complicated, i need simpler solution
please help


p.s. sorry, i will use code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harryoma is offline Offline
12 posts
since Dec 2008
Dec 28th, 2008
0

Re: how to save big number into array

I hope this is not homework, so I'll show you a little more (it's simpler than it seems!)
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Binary search
Next Thread in C++ Forum Timeline: quick pointer question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC