Can someone tell me why my print array method won't print strings.
It works with an int array, but only prints a few strings.

template < typename T >
void printArray(T * const array, int size)
{
    for (int i=0; i < size; ++i)
        cout << array[i] << ' ';
    cout << endl;

}//printarray

any help would be great thanks.

OK i found out that when i use endl instead of ' ' its prints. But i want them all on one line.
Does anyone know what could be happening?
Any info would be great thanks.

I think it is because of buffering of cout. A call cout.flush() may help. (I didn't try it. So pls excuse if it is wrong)

hi
Thanks for the reply.
I've already tried flush. (cout << array << flush << ' ') but it didn't work.
I also found out that you only have to put a new line character ('\n') at the end to get it working. But as i said i need it on one line only. Any more ideas?

sorry i ment:
cout << array << ' ' << flush;

i have also tried:
cout << array << ' ';
cout.flush();

I just tried it in VC++ 8 and it works fine for me. his is the program I tried.

#include <iostream>
#include <string>

using namespace std;

template < typename T >
void printArray(T * const array, int size)
{
for (int i=0; i < size; ++i)
cout << array << ' ';
cout << endl;

}//printarray
int main(void)
{
string array[] = {"abc", "pqr", "xyz"};
printArray<string>(array, 3);
return 0;
}

OK, maybe I should give you a bit more info sorry.
Im using cygwin and the GCC compiler.
Also I'm getting the values for the array from a text file. Then sorting the array using a shell sort, then printing it.
here is the file input method:

void processFile_strings(const char * fileName, int &linecount, string *strptr)
{
	string str;//for reading each line
    linecount = 0;
	ifstream inFile(fileName);//open file

    if (inFile.fail()){
      cout << fileName << " " << strerror(errno) << endl;
      exit (1);
    }
	else
	{

		while(true)
		{
            getline(inFile, str);
			if (!commentLine((char*)str.c_str()))
			{
                *strptr = str;
                linecount++;
                *strptr++;
            }
            if(inFile.eof())break;
		}
	}
	inFile.close();//close file
}//processFile_strings

sorry about the indentation

void processFile_strings(const char * fileName, int maxData, int &linecount, string *strptr)
{
    string str;//for reading each line
    linecount = 0;
    ifstream inFile(fileName);//open file

    if (inFile.fail()){
      cout << fileName << " " << strerror(errno) << endl;
      exit (1);
    }
    else
    {

	while(true)
	{
             getline(inFile, str);
	      if (!commentLine((char*)str.c_str()))
	      {
                  *strptr = str;
                  linecount++;
                  *strptr++;
              }
              if(inFile.eof())break;
	  }
    }
     inFile.close();//close file
}//processFile_strings[/B]

Does anyone know what is happening? Could it be because theres unknown characters inputed from the file, that i should remove? If so what could they be?

This article has been dead for over six months. Start a new discussion instead.