Answered # how to save big number into array

Discussion Starter harryoma ddanbe 2,467 Discussion Starter harryoma ddanbe 2,467 mrboolf 122 Discussion Starter harryoma mrboolf 122 VernonDozier 2,218 mrboolf 122 Featured Reply ddanbe 2,467 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

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;

0

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.

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

*Edited 3 Years Ago by mike_2000_17*: Fixed formatting

0

You can read in a string like this :

string Mystr;

cout << "What's your name? ";

getline (cin, Mystr);

0

You could use atoi or write your own char-to-digit converter, like this:

```
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!

0

this looks complicated, i need simpler solution:)

please help

p.s. sorry, i will use code tags

0

I hope this is not homework, so I'll show you a little more (it's simpler than it seems!)

```
#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;
}
```

0

```
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;
}
}
}
```

Take advantage of the fact that '0' through '9' are contiguous on the ASCII chart and that you can subtract characters.

```
int char2int(char c)
{
if (c < 48 || c > 57)
return -1; // signifies non-digit/invalid
return c - 48; // '0' is 48 in ASCII
}
```

0

@VernonDozier: Yes, you're obviously right, I just wanted to make it as plain as possible for the OP :)

1

but getline only works with letters not with numbers

strange... what if my name is U2 ?

This question has already been answered. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...