I'm new to arrays with dynamic sizes. That being said, I'm working with a simple code which seems to work just fine, my only concern is that once I display the 'char array', not only displays the user's inputs but some extra data, symbols and what not.
Can someone explain why, if to my understanding the first user's input already sets the size of the array, and how can this be fixed?
'cout' has no way to know how big the array is. You're just giving it a chunk of data and saying "print this".
It determines where to stop printing by looking for a special "null terminator". Which basically is the literal value of zero. This marks the end of a C-style string (a char array).
This also means your array has to be 1 larger than the desired size, because you need to have that extra 1 char to hold the null terminator.
1 2 3 4 5 6 7 8 9 10 11 12 13
char foo[5];
foo[0] = 'a';
foo[1] = 'b';
foo[2] = 'c';
foo[3] = 'd';
// no null terminator
cout << foo; // prints "abcd" possibly followed by a bunch of other garbage
foo[4] = 0; // the null terminator
cout << foo; // prints "abcd" cleanly. End of string is now marked
Of course this would be much easier if you use actual strings instead of char arrays. But I'm going to assume you're doing it the hard way because this is a homework assignment or something.
So in arrays there is no way to input a "null terminator"?
I want to use a dynamic size array because of the fact that its size may very whenever I call this function (not a function now but planning on making it one)
There is a way to put the null terminator in the array. It is '\0', a character with the numeric value of 0, as Disch showed on line 11 of his example.
#include <iostream>
#include <iomanip>
#include <new>
usingnamespace std;
int main()
{
int size,x;
char a;
cout << "Please enter the number of pin numbers: "<<endl;
cin >> size;
char *array=new (nothrow) char[size];
if(array)//<---If you are gonna use this, then use the nothrow method when creating the array
{
array[0]='\n';
for(int i=1;i<size;i++)
{
cout<<"Enter\n";
cin>>a;
array[i]=a;
cout<<array<<endl;
}
delete [] array;
}
}
#include <iostream>
#include <iomanip>
#include <new>
using std::cin;
using std::cout;
using std::endl;
using std::nothrow;
int main()
{
int size;
char a;
cout << "Please enter the number of pin numbers: "<<endl;
cin >> size;
char *array=new (nothrow) char[size+1];
if(array)//<---If you are gonna use this, then use the nothrow method when creating the array
{
array[0]='\n';
array[size+1] = 0;
for(int i=1;i<size + 1;i++)
{
cout<<"\nEnter\n";
cin>>a;
array[i]=a;
cout<<array<<endl;
}
delete [] array;
}
}
$ ./temp
Please enter the number of pin numbers:
6
Enter
r
r
Enter
u
ru
Enter
s
rus
Enter
s
russ
Enter
i
russi
Enter
a
russia
I'm a little confused , I thought every time a string is declared it has to be followed by its size in square brackets, like so string a[50] variable 'a' type string with 50 parameters.
An individual string can hold any number of characters.
1 2 3 4 5 6 7 8 9
string a = "This is a string.";
a += " I'm adding to the end of the string.";
a += " More data at the end of the string.";
cout << a; // prints:
// This is a string. I'm adding to the end of the string. More data at the end of the string."
// you can also easily determine the length of the string:
cout << a.length(); // prints the length of the string.
Plus you don't have to worry about deleting / cleaning up memory... or null terminators... or exceeding your array size... etc.
string a[50]; This gives you an array of 50 strings. This is perfectly legal to do, but if all you need is one string, it's entirely unnecessary.