| | |
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:
Solved Threads: 0
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
#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
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
You could use atoi or write your own char-to-digit converter, like this:
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!
c++ Syntax (Toggle Plain Text)
int char2int(char c) { switch(c) { case '0': { return 0; } case '1': { return 1; } // ... and so on } }
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!
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
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)
#include <iostream> #include <string> using namespace std; int char2int(char c) { switch(c) { case '0': { return 0; } case '1': { return 1; } case '2': { return 2; } case '3': { return 3; } case '4': { return 4; } case '5': { return 5; } case '6': { return 6; } case '7': { return 7; } case '8': { return 8; } case '9': { return 9; } } } int main() { string myBigNum; int myBigNumArray[20]; cout << endl << "Enter the number" << endl; getline(cin, myBigNum); if(myBigNum.size()>20) { cout << endl << "You entered a number too big!" << endl; return EXIT_FAILURE; } for(unsigned int i = 0; i < myBigNum.size(); ++i) { myBigNumArray[i] = char2int(myBigNum[i]); } // now you have your number saved in the array // of course you'll need sign checking and a better input validation // but this is a start and it's simple return EXIT_SUCCESS; }
Last edited by mrboolf; Dec 28th, 2008 at 12:04 pm.
![]() |
Similar Threads
- StreamReader and Position (In VB) (VB.NET)
- Shopping Cart Not Working (ASP.NET)
- Working with doubles, having some troubles (C++)
- File Reading (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Is a variant return type possible in C++? (C++)
- fibonacci (C++)
- Pascal assistance (Pascal and Delphi)
- Source Code that don't work? (Java)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Binary search
- Next Thread: quick pointer question
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets








