string array

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

Join Date: Mar 2007
Posts: 154
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

string array

 
0
  #1
Mar 5th, 2008
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

  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <string>
  4.  
  5. #define row 15
  6. #define col 2
  7.  
  8. using namespace std;
  9.  
  10. void PrintArray(char *Title, string array[row][col]);
  11. void Shuffle(string m[row][col]);
  12.  
  13. int main(void)
  14. {
  15. string array[row][col] = {"the farmer drove to the city",
  16. "the brakes were slipping",
  17. "the road got very wet",
  18. "water ran down the hill",
  19. "potatoes spilled everywhere",
  20. "she laid an egg",
  21. "the feathers went flying",
  22. "the duck went straight into the air",
  23. "the dog ran quickly after it",
  24. "everything was planted last spring",
  25. "the farmers jumped in",
  26. "april is a good time",
  27. "rocks rolled everywhere",
  28. "big bumps shook the cargo",
  29. "the clock seems to tick faster"};;
  30.  
  31.  
  32.  
  33. /* initialize random generator */
  34. srand( time(NULL));
  35.  
  36. /* fill the array with element # and random number */
  37.  
  38. for (int i=0; i<row; i++)
  39. {
  40. array[i][1] = rand();
  41. }
  42.  
  43. PrintArray("Original Data",array);
  44. Shuffle(array);
  45.  
  46. PrintArray("Shuffled Data",array);
  47.  
  48.  
  49. return 0;
  50. }
  51.  
  52.  
  53. void PrintArray(char *Title, string array[row][col])
  54. {
  55. /* print the contents */
  56. printf("\t%s\n",Title);
  57.  
  58. //printf("\tRandom\tElement\n");
  59. printf("%14s%10s\n","Random","Element");
  60.  
  61. for (int i=0; i<row; i++)
  62. {
  63. printf("%14d%10d\n",array[i][1], array[i][0]);
  64. }
  65.  
  66. /*add some white space*/
  67. puts("\n\n");
  68. }
  69.  
  70. void Shuffle(int a[row][col])
  71. {
  72.  
  73. /* column 1 is random number, column 0 is element*/
  74. int i,j,t;
  75. /* the row for loop */
  76. for (i=0; i < row-1; i++)
  77. /* the column for loop */
  78. for (j=0; j < row-i-1; j++)
  79.  
  80. if (a[j][1] > a[j+1][1])
  81. {
  82. t=a[j][1];
  83. a[j][1]=a[j+1][1];
  84. a[j+1][1]=t;
  85.  
  86. t=a[j][0];
  87. a[j][0]=a[j+1][0];
  88. a[j+1][0]=t;
  89. }
  90. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,350
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: string array

 
0
  #2
Mar 5th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 154
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: string array

 
0
  #3
Mar 5th, 2008
now i get "undefined reference to `Shuffle(std::string (*) [2])'
collect2: ld returned 1 exit status
"
but it seems to compile. I am using code blocks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,350
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: string array

 
0
  #4
Mar 5th, 2008
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.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC