# Thread: Can anyone help me?

1. ## Can anyone help me?

The code below takes 5 seperate numbers user entered numbers and makes them into one number, 2 seperate times. It then takes the values and adds them together for the sum. My probelm is I don't want to have to specify how many numbers (like only 5). I want them to be able to enter numbers and when they enter a -1 the number ends. can anyone help?

Code:
``` #include<iostream.h>

int convertNumber(int [], int);
void showvalues(int[]);

void main ()
{

int num1[5], num2[5];
int val1, val2;

cout<< "enter number" <<endl;
for (int count = 0; count < 5; count++)
cin >> num1[count];
val1 = convertNumber(num1, 5);
cout << val1 << endl;
cout << endl;

cout<< "enter number" <<endl;
for (int count1 = 0; count1 < 5; count1++)
cin >> num2[count1];
val2 = convertNumber(num2, 5);
cout << val2 << endl;
cout << endl;

cout << "the sum of the numbers is: " <<  val1 + val2 << endl;

}

int convertNumber(int nums[], int arySize)
{
int multiplier = 1;
int val = 0;
for (int x = arySize - 1; x >= 0; x--)
{
val += nums[x] * multiplier;
multiplier *= 10;
}
return val;
}```

2. Well, you can either read up on dynamic memory so that you know how to allocate memory for your array, or you can just use the vector class.

Code:
```while(TRUE)
{
cout<<"Enter a number: ";
cin >> num;
if(num == SENTINEL) break;
myVector.pushback(num);
}```
I think that'll do what you want.

3. Or if you don't use vectors do this:

[code]
int input=0;
int sum=0;

while (input!=-1) {
cout<<"enter your number sil vous plait"<<endl;
cin>> input;
sum+=input
}

4. > #include <iostream.h>

Code:
`#include <iostream>`
>

Code:
`using namespace std;`
or

Code:
```using std :: cout;
using std :: cin; // etc.```
or

Code:
`std :: cout << "..." << std :: endl;`
>void main()

Code:
`int main()`
- SirCrono6

P.S. I'm a standard boy =/

5. Standards aside, I think that the question needs more explanation, because "5 seperate numbers user entered numbers and makes them into one number, 2 seperate times" sounds like it was written by someone who was a little drunk, or was trying for a tongue twister

Could you provide an example of input and output?

6. example output;

enter number (one at a time)
5
6
7
-1

enter second number (one at time)

6
4
3
2
-1

number enter where 567 and 6432
the sum is 6999

What I have below is what I have now but the if statement doesn't really do what I want it to. It end the number but then it messes up the sum part. Any ideas???

Code:
```

#include<iostream.h>

int convertNumber(int [], int);
void showvalues(int[]);

void main ()
{
int num1[5], num2[5];
int val1, val2;

cout<< "enter number" <<endl;
for (int count = 0; count < 5; count++)
{
cin >> num1[count];
if (num1[count] == -1)
break;
}

val1 = convertNumber(num1, 5);
cout << endl;

cout<< "enter number" <<endl;
for (int count1 = 0; count1 < 5; count1++)
{
cin >> num2[count1];
if (num2[count1] == -1)
break;
}

val2 = convertNumber(num2, 5);
cout << endl;

cout << "You entered the numbers:" << val1 << " and " << val2 << endl;
cout << "the sum of the numbers is: " <<  val1 + val2 << endl;

}

int convertNumber(int nums[], int arySize)
{
int multiplier = 1;
int val = 0;
for (int x = arySize-1 ; x >= 0; x--)
{
val += nums[x] * multiplier;
multiplier *= 10;
}
return val;
}```

7. Originally posted by sflyers
Code:
```int convertNumber(int nums[], int arySize)
{
int multiplier = 1;
int val = 0;
for (int x = arySize-1 ; x >= 0; x--)
{
val += nums[x] * multiplier;
multiplier *= 10;
}
return val;
}```
Since the last value entered is -1, how about
Code:
```int convertNumber(int nums[])
{
int val = 0;
int j = 0;
while (nums[j] > -1)
{
val = (val * 10) + nums[j];
j++;
}
return val;
}```
This way you can have a larger number if you need it (and you use long)