character arrays

I heard that character arrays can store up to their length ,for example the array " f[5] " can store shorter sequences of characters.
so why this program store just 5 characters?(it means it doesn't show the output until we enter the fifth number)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
int main ()
{
    char f[5];
    int j;
    for(int b=0;b<5;b++)
    {
            cin>>f[b];
    }
    for(int g=0;g<5;g++)
    {
            cout<<f[g];
    }
    cin>>j;
    return 0;
}
Last edited on
Any array including character arrays can contain no more elements than the array size. However it can contain less actual elements than its size.
Usually character arrays contain some string literals that appended by a zero element. However it is not necessary.

In your example character array has size equal to 5. So it is possible to enter 5 elements. And these 5 elements are outputed. So there is no any problem.
Last edited on
Ok I know them but I don't know how to make an array that store for example 5 or less?
in that program I showed you that if I make an array with the length of 5, it will store just 5.
now, what should I do ?????
The value '5' is used several times. It specifies the size of the array. It also specifies how many times the for-loop will repeat.

You could change the loops at line 7 and 11 to repeat a smaller number of times.
I want to write a program that the input is a sequence of characters that their length is 1 to 5 and the in the output it prints the characters.

So as you said if I change the loop to repeat a smaller number of times it wouldn't receive the length of 5 . (I want to write a program that receives any number from 1 to 5)
Last edited on
What you need is some way to signal the end of the sequence. At the moment you must supply exactly 5 characters from the keyboard, no more and no less. You need to choose some character that will indicate that the input is finished. When that value is detected by the program, break out of the first for loop.

The second for loop could use the same check to detect the end, or it could simply use the count of how many chars were previously input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    char f[5];
    const char end_marker = '#';
    int b;
    
    for (b=0; b<5; b++)
    {
        cin>>f[b];
        if (f[b] == end_marker)
            break;
    }

    cout << endl;

    for (int g=0; g<b; g++)
    {
        cout<<f[g];
    }


At line 2, the character '#' is chosen, when that is entered, the input will end.
Notice at line 14, g<b the second loop uses the count from the first.
Last edited on
Thank you so much ,but would you explain line 2 and 8 for me :)
Line 2 is pretty straightforward, it defines a character constant named end_marker. The value '#' is given, but you could use any character of your preference. The keyword const is optional, it reminds both yourself and the compiler that this value will not be changed during program execution.

Line 8
8
9
        if (f[b] == end_marker)
            break;

This compares the value which was just input from the user f[b] with our chosen character. If it is equal then the break is executed, which will end the first loop.

Note, it wasn't strictly necessary to use a separate variable for this purpose, but it's generally a good practice to do so. For one thing, you can keep all the variables of which you may wish to customise the value in one place at the top of the program where they are easy to find.
Ok thank you very much!!It was really useful :))))))))))))
Last edited on
Topic archived. No new replies allowed.