Hello Everyone,
I am supposed to create a program that will store the first and last names of 10 people. I have got to the point of storing the strings into an array. I would like to know how can I add a NULL terminator directly after the name. The code will print the names out but give me "garbage" in the unused spaces of the array. How can I eliminate this or is there a better way to write this program? Thanks for the help in advance!
[code]
#include #include void main()
{
char Name[9][20];
int i=0;
int j=0;
for (i=0; i<10; i++){
cout <<"Please enter name number "<< i+1 <<":"<<endl;
cin.getline(Name[i],20, '
');
cout <<endl;
}
for(i=0; i < 10; i++){
for(j=0; j < 20; j++){
cout << Name[i][j];
}
cout << endl;
}
}// end main
[/code]
Comments
[code]
for(i=0; i < 10; i++)
cout << Name[i] << endl;
[/code]
should work now.
: Hello Everyone,
:
: I am supposed to create a program that will store the first and last names of 10 people. I have got to the point of storing the strings into an array. I would like to know how can I add a NULL terminator directly after the name. The code will print the names out but give me "garbage" in the unused spaces of the array. How can I eliminate this or is there a better way to write this program? Thanks for the help in advance!
:
: [code]
: #include <iostream.h>
: #include
:
: void main()
: {
:
: char Name[9][20];
: int i=0;
: int j=0;
:
: for (i=0; i<10; i++){
: cout <<"Please enter name number "<< i+1 <<":"<<endl;
: cin.getline(Name[i],20, '
');
: cout <<endl;
: }
: for(i=0; i < 10; i++){
: for(j=0; j < 20; j++){
: cout << Name[i][j];
: }
: cout << endl;
: }
:
: }// end main
:
:
: [/code]
:
you you can either use ragged array like *name[10] or use
header file and use string array like string name[10]
rather than char [9][20] its beter to use string Name [10] as it will handle the character as string it should work.
: if you just do this:
:
: [code]
: for(i=0; i < 10; i++)
: cout << Name[i] << endl;
: [/code]
:
: should work now.
:
:
:
: : Hello Everyone,
: :
: : I am supposed to create a program that will store the first and last names of 10 people. I have got to the point of storing the strings into an array. I would like to know how can I add a NULL terminator directly after the name. The code will print the names out but give me "garbage" in the unused spaces of the array. How can I eliminate this or is there a better way to write this program? Thanks for the help in advance!
: :
: : [code]
: : #include <iostream.h>
: : #include
: :
: : void main()
: : {
: :
: : char Name[9][20];
: : int i=0;
: : int j=0;
: :
: : for (i=0; i<10; i++){
: : cout <<"Please enter name number "<< i+1 <<":"<<endl;
: : cin.getline(Name[i],20, '
');
: : cout <<endl;
: : }
: : for(i=0; i < 10; i++){
: : for(j=0; j < 20; j++){
: : cout << Name[i][j];
: : }
: : cout << endl;
: : }
: :
: : }// end main
: :
: :
: : [/code]
: :
:
:
does not exist. I tried using the other sugestion but when I ran the program it repeated the value entered on the line until it filled 20 spaces.
The program after that crashed. Any other ideas of what I can do to correct this. I am still learning C++ and I know that I can do this some
way with classes and objects however my teacher said there is an easier way of doing it. I do not know if I am on the right track or if I am not
can someone please direct me in the right way. Thanks for the help everyone!!!
Joe
: you are using true 2D array
:
: you you can either use ragged array like *name[10] or use
:
: header file and use string array like string name[10]
:
: rather than char [9][20] its beter to use string Name [10] as it will handle the character as string it should work.
:
:
:
:
:
:
: : if you just do this:
: :
: : [code]
: : for(i=0; i < 10; i++)
: : cout << Name[i] << endl;
: : [/code]
: :
: : should work now.
: :
: :
: :
: : : Hello Everyone,
: : :
: : : I am supposed to create a program that will store the first and last names of 10 people. I have got to the point of storing the strings into an array. I would like to know how can I add a NULL terminator directly after the name. The code will print the names out but give me "garbage" in the unused spaces of the array. How can I eliminate this or is there a better way to write this program? Thanks for the help in advance!
: : :
: : : [code]
: : : #include <iostream.h>
: : : #include
: : :
: : : void main()
: : : {
: : :
: : : char Name[9][20];
: : : int i=0;
: : : int j=0;
: : :
: : : for (i=0; i<10; i++){
: : : cout <<"Please enter name number "<< i+1 <<":"<<endl;
: : : cin.getline(Name[i],20, '
');
: : : cout <<endl;
: : : }
: : : for(i=0; i < 10; i++){
: : : for(j=0; j < 20; j++){
: : : cout << Name[i][j];
: : : }
: : : cout << endl;
: : : }
: : :
: : : }// end main
: : :
: : :
: : : [/code]
: : :
: :
: :
:
:
Good luck,
Bass
: I tried using both suggestions but still it does not work. When I included Visual C++ gave me an error saying that the library
: does not exist. I tried using the other sugestion but when I ran the program it repeated the value entered on the line until it filled 20 spaces.
: The program after that crashed. Any other ideas of what I can do to correct this. I am still learning C++ and I know that I can do this some
: way with classes and objects however my teacher said there is an easier way of doing it. I do not know if I am on the right track or if I am not
: can someone please direct me in the right way. Thanks for the help everyone!!!
:
: Joe
:
:
:
: : you are using true 2D array
: :
: : you you can either use ragged array like *name[10] or use
: :
: : header file and use string array like string name[10]
: :
: : rather than char [9][20] its beter to use string Name [10] as it will handle the character as string it should work.
: :
: :
: :
: :
: :
: :
: : : if you just do this:
: : :
: : : [code]
: : : for(i=0; i < 10; i++)
: : : cout << Name[i] << endl;
: : : [/code]
: : :
: : : should work now.
: : :
: : :
: : :
: : : : Hello Everyone,
: : : :
: : : : I am supposed to create a program that will store the first and last names of 10 people. I have got to the point of storing the strings into an array. I would like to know how can I add a NULL terminator directly after the name. The code will print the names out but give me "garbage" in the unused spaces of the array. How can I eliminate this or is there a better way to write this program? Thanks for the help in advance!
: : : :
: : : : [code]
: : : : #include <iostream.h>
: : : : #include
: : : :
: : : : void main()
: : : : {
: : : :
: : : : char Name[9][20];
: : : : int i=0;
: : : : int j=0;
: : : :
: : : : for (i=0; i<10; i++){
: : : : cout <<"Please enter name number "<< i+1 <<":"<<endl;
: : : : cin.getline(Name[i],20, '
');
: : : : cout <<endl;
: : : : }
: : : : for(i=0; i < 10; i++){
: : : : for(j=0; j < 20; j++){
: : : : cout << Name[i][j];
: : : : }
: : : : cout << endl;
: : : : }
: : : :
: : : : }// end main
: : : :
: : : :
: : : : [/code]
: : : :
: : :
: : :
: :
: :
:
:
[code]
#include
#include
#include
void main()
{
char FNames[10][128];
char LNames[10][128];
int i=0, j=0;
for (i=0; i <= 9; i++){
cout <<"Enter Name number "<<i+1<<":";
cin >> FNames[i] >> LNames[i];
}
cout <<endl<<endl;
for (i=9; i >= 0; i--){
cout << FNames[i] <<" "<< LNames[i];
cout <<setw(20);
cout << LNames[i] <<", "<< FNames[i];
cout << endl;
}
}//end main
[/code]
: You should try to make your array static, that will make all fields blank, also you could use puts() function from conio.h to output name. Give it a try!
: Good luck,
: Bass
:
: : I tried using both suggestions but still it does not work. When I included <cstring.h> Visual C++ gave me an error saying that the library
: : does not exist. I tried using the other sugestion but when I ran the program it repeated the value entered on the line until it filled 20 spaces.
: : The program after that crashed. Any other ideas of what I can do to correct this. I am still learning C++ and I know that I can do this some
: : way with classes and objects however my teacher said there is an easier way of doing it. I do not know if I am on the right track or if I am not
: : can someone please direct me in the right way. Thanks for the help everyone!!!
: :
: : Joe
: :
: :
: :
: : : you are using true 2D array
: : :
: : : you you can either use ragged array like *name[10] or use
: : :
: : : header file and use string array like string name[10]
: : :
: : : rather than char [9][20] its beter to use string Name [10] as it will handle the character as string it should work.
: : :
: : :
: : :
: : :
: : :
: : :
: : : : if you just do this:
: : : :
: : : : [code]
: : : : for(i=0; i < 10; i++)
: : : : cout << Name[i] << endl;
: : : : [/code]
: : : :
: : : : should work now.
: : : :
: : : :
: : : :
: : : : : Hello Everyone,
: : : : :
: : : : : I am supposed to create a program that will store the first and last names of 10 people. I have got to the point of storing the strings into an array. I would like to know how can I add a NULL terminator directly after the name. The code will print the names out but give me "garbage" in the unused spaces of the array. How can I eliminate this or is there a better way to write this program? Thanks for the help in advance!
: : : : :
: : : : : [code]
: : : : : #include <iostream.h>
: : : : : #include
: : : : :
: : : : : void main()
: : : : : {
: : : : :
: : : : : char Name[9][20];
: : : : : int i=0;
: : : : : int j=0;
: : : : :
: : : : : for (i=0; i<10; i++){
: : : : : cout <<"Please enter name number "<< i+1 <<":"<<endl;
: : : : : cin.getline(Name[i],20, '
');
: : : : : cout <<endl;
: : : : : }
: : : : : for(i=0; i < 10; i++){
: : : : : for(j=0; j < 20; j++){
: : : : : cout << Name[i][j];
: : : : : }
: : : : : cout << endl;
: : : : : }
: : : : :
: : : : : }// end main
: : : : :
: : : : :
: : : : : [/code]
: : : : :
: : : :
: : : :
: : :
: : :
: :
: :
:
: