Read numbers until 0 is reached

Hi guys! I need help with a problem. It says that the program should read numbers until the digit 0 is typed.

How do i do that in C++?

I tried to use an array but it didn't work at all.

Hi there,

Without knowing more specifics about your program it's kind of hard, but a common way of doing this is the following:

1
2
3
4
5
6
7
int input = 1;

while (input != 0)
{
    std::cout << "Please enter a number: ";
    std::cin >> input;
}


Of course, if you need to store every number, you will have to store them in a container, like std::vector.

All the best,
NwN
Thanks! You saved me!

One more thing! This problem asks to read n numbers an then output the sum of numbers that have only even digits:

I have done this so far but the output is still not good. What should i do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int a[100], n=0, i, sum=0;
int main()
{
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>a[i];
    for(i=1;i<=n;i++)
    {
        while(a[i]){
            if((a[i]%10)%2==0)
                sum+=a[i];
            a[i]=a[i]/10;
        }
    }
    cout<<sum;
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int a[100], n=0, i, sum=0;
int main()
{
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
        for(i=0;i<n;i++)
        {
            if(a[i]%2==0)
                sum+=a[i];
        }
    cout<<sum;
    return 0;
}
You don't need an array. That array takes up more space than you need. It's really unnecessary. Just the array of 100 elements takes up 95 * 4 more bytes than my 5 variables.

On top of all of that, your ways of getting the digits won't ever work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

int main()
{
    int n;
    int sum = 0;
    int num;

    cin >> n;

    for(int x = 0; x < n; x++)
    {
        int digits = 0;

        cin >> num;
        int numT = num;

        while(numT > 0)
        {
            numT /= 10;
            digits++;
        }

        if(digits % 2 == 0)
            sum += num;
    }

    cout << sum;

    return 0;
}
Last edited on
@GRex2595 where did you store the numbers? Your code always outputs 0.
Storing the numbers is unnecessary because you are only summing those numbers, not using them again.

Because I didn't test my code first, I went back to test it. It worked exactly as I expected it too (only adding numbers that have an even number of digits).

Here is my previous code updated for user readability.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

int main()
{
    int n;
    int sum = 0;
    int num;

    cout << "Enter number of numbers to input:  ";
    cin >> n;

    for(int x = 0; x < n; x++)
    {
        int digits = 0;

	cout << "Enter number:  ";
        cin >> num;
        int numT = num;

        while(numT > 0)
        {
            numT /= 10;
            digits++;
        }

        if(digits % 2 == 0)
            sum += num;
    }

    cout << "Sum of all even-digit numbers:  " << sum << endl;

    return 0;
}
Last edited on
the size of the array is always 4Bytes even a[1000]
The array reserves space for all its elements. If you have two integers, they take up 8 bytes. If you have an array reserving space for two integers, that array is saving up 8 bytes of data for future use.
still output is 0 ;)
Does the problem state that only numbers with an even count of digits are totaled, or only numbers of which each digit is an even number?
@Chriscpp
I have no idea what you think an even-digit number is, or what you are inputting, or what compiler you are using. You seem to be upset that I mentioned that the array is inefficient, for that I'm sorry. I'm just trying to help the OP with their issue.

This is what I get as output for my code. This comes from my compiler (Visual Studio 2008) and it comes straight from the console.


Enter number of numbers to input:  5
Enter number:  10
Enter number:  13
Enter number:  5
Enter number:  7
Enter number:  255
Sum of all even-digit numbers:  23
Press any key to continue . . .


10 and 13 are even-digit numbers and none of the rest have even digits. The sum of 10 and 13 is 23.
Last edited on
13 is odd not even
even numbers are 2 4 6 8 10 12 14 ...
odd numbers are 1 3 5 7 9...
See last line of my last post.
Guys, I want to propose the following conjecture:

If a thread in cplusplus.com has more than five replies, then an argument is going on in it.

Does it sound plausible?
@Josue Molina is 13 an even number?
@Chriscpp

Read GRex2595's reply.

He is meaning place digit even.

If it has two numbers E.G 11 = 2 number 1 and 1 thus even
If it has one number E.G 9 = 1 number 9 thus odd
If it has five number E.G 54365 = 5 number 5, 4, 3, 6 and 5 thus odd.
@Josue Molina
I agree with you. I don't know if this is what the OP wants or not, but I'm trying to help and explain my solution.

@Chriscpp
13 is an odd number. 27 is an odd number 2363 is an odd number. Each of these numbers have 2, 2, and 4 digits respectively. 2, 2, and 4 are all even numbers. I know what you are saying, and if my program operated the way you think it is supposed to, then yes you would be right. My program does not sum the number based on whether or not that number is odd or even. My program sums the number based on whether or not the number of digits that number has is odd or even.

@antirsi96
I'm sorry this erupted on your thread. You are not getting the answer you are looking for because of this, and for that I offer my sincerest apologies.
It's ok. I think i figured a way of solving this. I'll try it when i get home.

@booradley60

The problem states that only numbers that have even digits must be sumed up. Like 224 or 68
Topic archived. No new replies allowed.