943,650 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1728
  • C++ RSS
Mar 24th, 2009
0

Print Array: strings!

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 36
Solved Threads: 4
Junior Poster
Danny_501 is offline Offline
109 posts
since Mar 2009
Mar 24th, 2009
0

Re: Print Array: strings!

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.
Reputation Points: 36
Solved Threads: 4
Junior Poster
Danny_501 is offline Offline
109 posts
since Mar 2009
Mar 24th, 2009
0

Re: Print Array: strings!

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)
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 24th, 2009
0

Re: Print Array: strings!

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?
Reputation Points: 36
Solved Threads: 4
Junior Poster
Danny_501 is offline Offline
109 posts
since Mar 2009
Mar 24th, 2009
0

Re: Print Array: strings!

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

i have also tried:
cout << array[i] << ' ';
cout.flush();
Reputation Points: 36
Solved Threads: 4
Junior Poster
Danny_501 is offline Offline
109 posts
since Mar 2009
Mar 24th, 2009
0

Re: Print Array: strings!

I just tried it in VC++ 8 and it works fine for me. his is the program I tried.
Quote ...
#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;
}
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 24th, 2009
0

Re: Print Array: strings!

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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 36
Solved Threads: 4
Junior Poster
Danny_501 is offline Offline
109 posts
since Mar 2009
Mar 24th, 2009
0

Re: Print Array: strings!

sorry about the indentation
C++ Syntax (Toggle Plain Text)
  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]
Reputation Points: 36
Solved Threads: 4
Junior Poster
Danny_501 is offline Offline
109 posts
since Mar 2009
Mar 25th, 2009
0

Re: Print Array: strings!

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?
Reputation Points: 36
Solved Threads: 4
Junior Poster
Danny_501 is offline Offline
109 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help!need making an random animation inside a function for allegro
Next Thread in C++ Forum Timeline: Question about array of objects....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC