| | |
string array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 154
Reputation:
Solved Threads: 0
I am tring to compile this simple string array. I am getting some struct error. "Cannot pass object of non-POD type in line 63. which is "printf("%14d%10d\n",array[i][1], array[i][0]);"
please help
please help
C++ Syntax (Toggle Plain Text)
#include <stdlib.h> #include <time.h> #include <string> #define row 15 #define col 2 using namespace std; void PrintArray(char *Title, string array[row][col]); void Shuffle(string m[row][col]); int main(void) { string array[row][col] = {"the farmer drove to the city", "the brakes were slipping", "the road got very wet", "water ran down the hill", "potatoes spilled everywhere", "she laid an egg", "the feathers went flying", "the duck went straight into the air", "the dog ran quickly after it", "everything was planted last spring", "the farmers jumped in", "april is a good time", "rocks rolled everywhere", "big bumps shook the cargo", "the clock seems to tick faster"};; /* initialize random generator */ srand( time(NULL)); /* fill the array with element # and random number */ for (int i=0; i<row; i++) { array[i][1] = rand(); } PrintArray("Original Data",array); Shuffle(array); PrintArray("Shuffled Data",array); return 0; } void PrintArray(char *Title, string array[row][col]) { /* print the contents */ printf("\t%s\n",Title); //printf("\tRandom\tElement\n"); printf("%14s%10s\n","Random","Element"); for (int i=0; i<row; i++) { printf("%14d%10d\n",array[i][1], array[i][0]); } /*add some white space*/ puts("\n\n"); } void Shuffle(int a[row][col]) { /* column 1 is random number, column 0 is element*/ int i,j,t; /* the row for loop */ for (i=0; i < row-1; i++) /* the column for loop */ for (j=0; j < row-i-1; j++) if (a[j][1] > a[j+1][1]) { t=a[j][1]; a[j][1]=a[j+1][1]; a[j+1][1]=t; t=a[j][0]; a[j][0]=a[j+1][0]; a[j+1][0]=t; } }
that error message means that you are attempting to pass a std::string object to printf, but printf() required an integer -- "%d" is an integer, not a string. If all you want is the first character of a string then you need to access it like this:
array[i][1][0] , otherwise if you want to pass the entire string then use "%s" and array[i][1].c_str() Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
You didn't code any function with that parameter. The function you coded takes an array of ints, not an array of std::string. Consequently, in c++, they are two different functions.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- How do I convert a vector to a String array ? (Java)
- Declaring string array, initializing later... (C++)
- Copy string into an array (C#)
- ArrayList to multi-dimensional string array (C#)
- Geting elements from a String Array (C++)
Other Threads in the C++ Forum
- Previous Thread: random string array
- Next Thread: Anyone see where my garbage is?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






