The purpose of this program as can be seen from the comments is to allow a user to input a name into a null terminated character array using a for loop where it is stored and then subsequently outputted by another for loop. I have tried comparing the code to other examples in books and online and have tried rewriting it multiple times but have been unable to discern what the problem is. When I compile the code and run the program it just allows me to repeatedly enter characters without breaking from the first for loop. Any help as to what this bug is would be appreciated as I am trying to teach myself C++ but have gotten stuck on null terminated character arrays.
//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;
//this is the array passed to the function name_input_output
char name_entry [100];
//this is the call for name_input_function with the argument name_entry
name_input_output (name_entry);
system ("PAUSE");
return 0;
}
I'm fairly certain that when you create the char array on line 41, it has completely random data in it. For your program to function as you intended, you'll have to fill it with non-null character data up until the final character, which you'll set to null.
I don't quite understand what you're saying. In the first for loop I try to have the user input data into the array so as to initialize it. Is it not possible for the user to input a string of characters into the array? I do understand that before initialized character arrays contain random data, but I'm trying to have a user initialize the array.
But your loop for input goes until the character in the array (before input) is equal to '\0', but since you didn't initialize it in main() you have no idea where that will be (or even if it will occur at all!).
Alright I went back and initialized the array before the function call and set an element equal to null. I tested the program and it worked out. Thanks a lot for your help.