This code worked exactly as I designed it to work until I added lines 36-39. Upon adding those lines to the code the name outputted from the function is only 4 characters in length when it should be 6. Take those lines away, however, and the program runs correctly. I cannot figure out why this is though as lines 36-39 shouldn't alter the program except for storing and outputting a name entered by the user.
//this program allows the user to enter a single name
//and for the entered name to be outputted back to the user
#include <iostream>
#include <cstdio>
#include <cstdlib>
usingnamespace std;
//this function named 'name_input_output' allows the user to input a
//name into a null terminated character array named 'name_array1'
//and then have it outputted on the screen
void name_input_output (char name_array1 [])
{
//the user inputs a name into a null terminated char array, 'name_array1'.
//'index1' is the variable which counts the number of characters inputted
//by the user, and allows them to be stored in 'name_array1' until
//'name_array1' is null
for (int index1 = 0; name_array1 [index1] != '\0'; index1++)
{
cin >>name_array1 [index1];
}
//prints "You entered:" on the screen
cout <<"You entered: ";
//this for statement outputs the name entered by the user. 'index2'
//is a counting variable which tells the computer which element to be
//outputted, until 'name_array2' is null
for (int index2 = 0; name_array1 [index2] != '\0'; index2++)
{
cout <<name_array1 [index2];
}
}
int main ()
{
cout <<"Enter a person's name:"<<endl;
string name1;
cin >>name1;
cout <<"You entered: "<<name1<<endl;
cout <<"Now enter a person's name six characters in length."<<endl;
//this is the array passed to the function name_input_output
char name_entry [100];
name_entry [6] = '\0';
//this is the call for name_input_function with the argument name_entry
name_input_output (name_entry);
cout <<'\n';
system ("PAUSE");
return 0;
}
Since line 36 is int main () I presume that the line numbers do not correspond to the source you've posted.
On line 46, you declare an array. On line 47 you set one element of the array to '\0', but you have no idea what value any other element of the array holds. Since name_input_output depends on the value of the elements in the array, and you've made no effort to ensure those values are what you expect (save one), it shouldn't be surprising the elements of the array are not what you expect.
Sorry the preview question option before I posted numbered the lines differently so I actually meant lines 38-41. I understand what you're saying but the reason only one element of the array is null is so that a user may enter a six character long name. The null breaks the for loop after the sixth character. The program works correctly without lines 38-41 in the code, but those lines seem to be causing a bug for some reason I cannot figure out.
Ahh thank you. Went back and initialized the array and it worked perfectly. Didn't think about the fact that the random data could have a '\0' in it. One question also about character arrays in general and their use. Is it possible for character arrays to be used in such a way that a user could initialize them? For example, say I wanted a user to be able to enter a character string to be stored in a character array without knowing beforehand the number of characters to be entered (assuming of course the array had enough memory to store the characters entered). Would that be possible, or is that something character arrays just aren't used for?
Ok thanks that helps a lot. I'm trying to teach myself C++ and have been trying to make sure I fully understand character arrays. Didn't know if that was possible but now I see I just was going about it the wrong way (and I probably need to learn pointers first). Thanks!