| | |
Print Array: strings!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
any help would be great thanks.
It works with an int array, but only prints a few strings.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
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)
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
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;
}
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:
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)
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
C++ Syntax (Toggle Plain Text)
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]
![]() |
Similar Threads
- Need to list an array of strings in reverse order. (C++)
- strings, arrays, length program (C++)
- Need help with Array sorting using Java Servlet (Java)
- A program to print out the names, remarks and average of five students in descending (C)
- Array problem in Pascal (Pascal and Delphi)
- Sorting a 2D Array of Strings (C++)
- print words in reverse in a given sentence (C)
- 2d array of strings (C++)
- Initializing an array of strings and printing it. (C++)
Other Threads in the C++ Forum
- Previous Thread: help!need making an random animation inside a function for allegro
- Next Thread: Question about array of objects....
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





