Hey everyone,
I have a char array of supposing 20.
like char array [20];
When I enter "Animation is good", it has 17 letters of course. Extra 3 spaces are neglected by C++. I wonder if somehow these last 3 null arrays filled with spaces.
When I enter "Animation is good", it has 17 letters of course. Extra 3 spaces are neglected by C++. I wonder if somehow these last 3 null arrays filled with spaces.
You using scanf or cin to read input? Answer depends on which one.
@elohssa I'm using gets() for inputting an char array..
@Duthomhas You answer as far as I understand is add spaces to your char array by yourself.
But I want that last 3 character is somehow
like for Animation is good
char array[20] = { 'A','n','i','m','a','t','i','o','n',' ','i','s',' ','g','o','o','d', 0, ?, ? };
last 3 should be filled with 3 spaces..
And if user enters any string like Animation
char array should be char array[20] = { 'A','n','i','m','a','t','i','o','n',' ',' ',' ',' ',' ',' ',' ',' ',' ' ,' ' , ' ' };
last null indexes should filled spaces automatically...
#include <iostream>
int main()
{
constint size = 20;
char array[size] = {}; // Initialize all characters to null
// get input (gets is not safe, so I don't suggest using it)
std::cin.getline(array, size);
// Changes all remaining null characters (except for the last one) into spaces
for (int i = 0; i < size-1; i++)
{
if (array[i] == '\0')
array[i] = ' ';
}
std::cout << "-->" << array << "<--" << std::endl;
}
An array of characters is not necessarily a C-string. It can also be a fixed-size buffer, containing data that is not null-terminated.
1 2 3 4 5 6 7
char array[20];
// entirely fill array with spaces
std::fill(std::begin(array), std::end(array), ' ');
// partially fill array with some data, overwriting the first size spaces
std::ifstream str{"/dev/urandom", std::ios::bin};
str.read(array, size);
After the snippet, assuming success, the contents of array is the following:
This could work. There is probably a way to condense it more, but I wasn't sure.
I took the liberty of assuming the IDs and dates are ints internally.
If they are simply strings, ex "003", "09|08|2018", then it's even easier, just follow the pattern I did for "ID Name Department issue date"