Array input stopping

Hello, I was solving this problem:

Write a program that accepts characters from the user and inputs it in an array. It terminates when the user enter “0” and prints out the input of these characters next to each other.


I managed to get through it using this code:

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
36
37
38
39
40
#include <iostream>


#define SIZE 256


using namespace std;

int main()
{
    char a[SIZE];

    cout << "                                         "
         << "Adding characters together"
         << endl;

    cout << "Enter the characters you'd like to put together:" << endl;

    for(int i = 0; i < SIZE ; i++)
    {
        cin >> a[i];
        if(a[i] == '0')
        {
            a[i] = ' ';
            break;
        }
    }

    cout << endl;

   //cout << a[0] << " " << a[1]; //debug

    for(int i = 0; a[i] != ' '; i++)
    {
            cout << a[i];
    }
    cout << endl;

    return 0;
}



I assume its correct through my trials using it but, I don't want this weird way I used to check on the user's input of "0" to stop the loop and the weird way I used to print out the array cause otherwise it would print all the 256-x ascii chars and x being the number of inputs the user has input, if someone has a better way of checking if user inputs 0 and stop the input to the array and also print it out in a more elegant way i'll be so thankful.
Last edited on
Instead of ' ' you may add '\0' (the end of string character). Then you can use it like so: cout << a;
Huh, you probably didn't get what I mean. I want to know when the user inputs 0 in another way than the one I used inorder to stop the loop which inputs to the array, and aswell printing the array out without using the condition I used "a[i] != ' ';" that's what I want. Already mentioned that in the last few lines :c
Like coder777 suggested, instead of setting the char to ' ', you can set it to (char)0, which should not be confused with '0'. The former will set all the bits in memory to 0, and the latter is a character code for symbol '0', which in memory will represent 32. See this link for char codes: http://www.asciitable.com/

If you set one of the character's bits to 0, the array will become a null-terminated string. What that means is its a C string (not C++ std::string), which starts at the memory address pointed by your array, and ends at the first character whose bits are 0.

If you set bits to 0 when user inputs '0', all of these should print out the same result.
1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < SIZE; i++) {
    if (a[i] == 0) { // Notice we aren't comparing to '0', but 0 instead. '0' translates to 32
        break;
    }
    cout << a[i];
}

for (char* iter = a; iter != 0; ++iter) {
    cout << *iter;
}

cout << a;


Passing char array to cout will be interpreted as a C string - a string of characters which terminates at the first 0bits char.

There is another case which might be of interest to you. In this example, you are dealing with text data, which means that 0bit character terminates the string.

If you were working with binary data, a 0bit character is perfectly legal.
The way to handle it would be to track the length of your binary data:
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
36
37
38
39
#include <iostream>

#define SIZE 256

int main()
{
    char a[SIZE];

    std::cout << "                                         "
         << "Adding characters together"
         << endl;

    std::cout << "Enter the characters you'd like to put together:" << std::endl;

    int dataSize = 0;

    while (dataSize < SIZE)
    {
        std::cin >> a[dataSize];
        if(a[dataSize] == '0')
        {
            break;
        }
        else
        {
            dataSize++;
        }
    }

    std::cout << endl;

    for (int i = 0; i < dataSize; i++)
    {
        std::cout << a[i];
    }
    std::cout << endl;

    return 0;
}

Topic archived. No new replies allowed.