Print Array: strings!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Print Array: strings!

 
0
  #1
Mar 24th, 2009
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.

  1. template < typename T >
  2. void printArray(T * const array, int size)
  3. {
  4. for (int i=0; i < size; ++i)
  5. cout << array[i] << ' ';
  6. cout << endl;
  7.  
  8. }//printarray

any help would be great thanks.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Re: Print Array: strings!

 
0
  #2
Mar 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: Print Array: strings!

 
0
  #3
Mar 24th, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Re: Print Array: strings!

 
0
  #4
Mar 24th, 2009
hi
Thanks for the reply.
I've already tried flush. (cout << array[i] << 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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Re: Print Array: strings!

 
0
  #5
Mar 24th, 2009
sorry i ment:
cout << array[i] << ' ' << flush;

i have also tried:
cout << array[i] << ' ';
cout.flush();
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: Print Array: strings!

 
0
  #6
Mar 24th, 2009
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[i] << ' ';
cout << endl;

}//printarray
int main(void)
{
string array[] = {"abc", "pqr", "xyz"};
printArray<string>(array, 3);
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Re: Print Array: strings!

 
0
  #7
Mar 24th, 2009
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:
  1. void processFile_strings(const char * fileName, int &linecount, string *strptr)
  2. {
  3. string str;//for reading each line
  4. linecount = 0;
  5. ifstream inFile(fileName);//open file
  6.  
  7. if (inFile.fail()){
  8. cout << fileName << " " << strerror(errno) << endl;
  9. exit (1);
  10. }
  11. else
  12. {
  13.  
  14. while(true)
  15. {
  16. getline(inFile, str);
  17. if (!commentLine((char*)str.c_str()))
  18. {
  19. *strptr = str;
  20. linecount++;
  21. *strptr++;
  22. }
  23. if(inFile.eof())break;
  24. }
  25. }
  26. inFile.close();//close file
  27. }//processFile_strings
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Re: Print Array: strings!

 
0
  #8
Mar 24th, 2009
sorry about the indentation
  1. void processFile_strings(const char * fileName, int maxData, int &linecount, string *strptr)
  2. {
  3. string str;//for reading each line
  4. linecount = 0;
  5. ifstream inFile(fileName);//open file
  6.  
  7. if (inFile.fail()){
  8. cout << fileName << " " << strerror(errno) << endl;
  9. exit (1);
  10. }
  11. else
  12. {
  13.  
  14. while(true)
  15. {
  16. getline(inFile, str);
  17. if (!commentLine((char*)str.c_str()))
  18. {
  19. *strptr = str;
  20. linecount++;
  21. *strptr++;
  22. }
  23. if(inFile.eof())break;
  24. }
  25. }
  26. inFile.close();//close file
  27. }//processFile_strings[/B]
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Re: Print Array: strings!

 
0
  #9
Mar 25th, 2009
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC