944,126 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 416
  • C++ RSS
Nov 5th, 2009
1

Removing stl features from a program

Expand Post »
Hi guys, anyone familiar with the Markov Chain Algorithm? I've got an assignment to take the code from a C++ implementation and remove any stl features like deques and maps, but continue to use strings as my main object type. I've based my changes around a C version of the code, which really just needs to be adapted to use C++ strings instead of C char arrays.

The place I'm running into trouble is trying to initialize all my strings with the "non-word" value
C++ Syntax (Toggle Plain Text)
  1. "\n"
. Here's the relevant code:

C++ Syntax (Toggle Plain Text)
  1. ...
  2. const char NONWORD[] = "\n";
  3. ...
  4. int main(void)
  5. {
  6. int i, nwords = MAXGEN;
  7. string *prefix[NPREF]; // current input prefix
  8.  
  9. srand(time(NULL));
  10. for (int i = 0; i < NPREF; i++)
  11. hashadd(prefix, NONWORD);
  12. hashbuild(prefix, cin);
  13. hashadd(prefix, NONWORD);
  14. hashgenerate(nwords);
  15. return 0;
  16. }
  17. ...
  18. void hashadd(string *prefix[NPREF], string *suffix)
  19. {
  20. State *sp;
  21. sp = lookup(prefix, 1);
  22. addsuffix(sp, suffix);
  23. memmove(prefix, prefix+1, (NPREF-1)*sizeof(prefix[0]));
  24. prefix[NPREF - 1] = suffix;
  25. }
The error I get when I go to compile the code is
"prob3.cpp:104: error: invalid conversion from `const char' to `std::string*'"
pointing to the body of the for loop in main intended to put "\n" in each field of prefix. I've tried replacing all references to NONWORD with
C++ Syntax (Toggle Plain Text)
  1. "\n"
but I get the same error. Does anyone have a tip to caste or convert newline as a string?

I have a feeling this is just one of several little hurdles, so if anyone wants to see more of my code to try and find a way around this problem, let me know.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jmoran19 is offline Offline
12 posts
since Oct 2009
Nov 5th, 2009
0
Re: Removing stl features from a program
I don't understand why you would want to remove stl features? They make things MUCH easier...
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 5th, 2009
0
Re: Removing stl features from a program
Click to Expand / Collapse  Quote originally posted by daviddoria ...
I don't understand why you would want to remove stl features? They make things MUCH easier...
It's a homework assignment to prove why it's so useful to have the stl features, while at the same time testing your abilities. Unfortunately, being a transfer student at my school puts you at a disadvantage during some of these more arcane assignments.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jmoran19 is offline Offline
12 posts
since Oct 2009
Nov 5th, 2009
0
Re: Removing stl features from a program
Is this what you are looking for?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6. const char NONWORD[] = "\n";
  7.  
  8. std::string test = NONWORD;
  9. return 0;
  10. }
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008

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: program writen to transfer galons into liters per mile
Next Thread in C++ Forum Timeline: how to display this using for loop??





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


Follow us on Twitter


© 2011 DaniWeb® LLC