I am programming student and my teacher has told to me to store some names from file into 2D char array. Now I know it is easy by string but my teacher has limited me to use cstring only.
This is my code:
#include<iostream>
#include<fstream>
#include<cstring>
int main()
{
char names[5][50];
int row=0,col=0,x=0;
ifstream input("file.txt");
while(input.good())
{
input.getline(names[row],50);
row++;
cout<<names;
}
input.close();
return 0;
}
it is giving garbage value. This is my input file:
cout<<names;
appears to be trying to print the whole 2D array, but cout
doesn't know how to, so treats it as an address and prints just that. If instead you try and print individual names (ie out<<names[row-1];
), these are character pointers, so it does know how to print them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.