ending null character

L

las

code :
char *programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
programs[index]=new char[21];

file.getline(programs[index], 21, ' ');

cout << *program[index] << endl;
}

This runs fine but it only prints the first char. I thought getline was
suppse to automatically add a null terminating char, hence the whole word
should be printed.

also tryed using get

for(int index=0; index<5; index++)
{
programs[index]=new char[21];
data.get(programs[lp], 21 ,' ');
data.ignore(1,' '); //remove space
// how do I add null terminating char to programs[lp] ?

}

Thanks for any help.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

las escribió:
code :
char *programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
programs[index]=new char[21];

file.getline(programs[index], 21, ' ');

cout << *program[index] << endl;
}

This runs fine but it only prints the first char.

program [index] --> a char pointer
* program [index] --> a char

cout << program [index] << endl;

Is what you need.

Regards.
 
P

Peter van Merkerk

code :
char *programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
programs[index]=new char[21];

file.getline(programs[index], 21, ' ');

cout << *program[index] << endl;
}

This runs fine but it only prints the first char.

program [index] --> a char pointer
* program [index] --> a char

cout << program [index] << endl;

Is what you need.

Or even better, get rid of the char pointer misery altogether:

string programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
getline(file, programs[index], ' ');
cout << programs[index] << endl;
}

The advantages: less code, more robust, no arbitrary string length
limitations and no worries about freeing the memory.
 
M

Mike Wahler

Peter van Merkerk said:
code :
char *programs[5];
ifstream file("...");

for(int index=0;index<5;index++)
{
programs[index]=new char[21];

file.getline(programs[index], 21, ' ');

cout << *program[index] << endl;
}

This runs fine but it only prints the first char.

program [index] --> a char pointer
* program [index] --> a char

cout << program [index] << endl;

Is what you need.

Or even better, get rid of the char pointer misery altogether:

string programs[5];

And better yet, also remove the array misery...

vector<string> programs(5);

-Mike
ifstream file("...");

for(int index=0;index<5;index++)

for(vector::size_type index = 0; index < programs.size(); ++i)
{
getline(file, programs[index], ' ');
cout << programs[index] << endl;
}

The advantages: less code, more robust, no arbitrary string length
limitations and no worries about freeing the memory.

And now no arbitrary array limitations. :)

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,138
Messages
2,570,804
Members
47,349
Latest member
jojonoy597

Latest Threads

Top