Jump to content
  • Advertisement

Stormtrooper

Member
  • Content Count

    320
  • Joined

  • Last visited

Community Reputation

100 Neutral

About Stormtrooper

  • Rank
    Member
  1. Stormtrooper

    Initialization list

    Ah! Okay, I get it. Thanks for your help, I should have know that :P
  2. Stormtrooper

    Initialization list

    // // task.h // rtodo2 // // This code has been released under the BSD license // See license.txt // Copyright 2008 Ryan Capote All rights reserved. // #include "task.h" #include <sstream> Task::Task(const std::string &task, const std::string &project = "none", const std::string &context = "none", Task::Priority priority = Task::NORMAL, bool done = false, bool nextAction = false) : _task(task), _project(project), _context(context), _priority(priority), _done(done), _nextAction(nextAction) { } Task::Task(const std::string &data) { } void Task::loadData(const std::string &data) { } std::string Task::serialize() { std::string result; std::stringstream stream; stream << _task << ',' << _project << ',' << _context << ',' << _priority << ',' << _done << ',' << _nextAction << '|'; stream.flush(); result = stream.str(); return result; } std::string Task::getTask() { return _task; } void Task::setTask(const std::string &task) { _task = task; } std::string Task::getProject() { return _project; } void Task::setProject(const std::string &project) { _project = project; } std::string Task::getContext() { return _context; } void Task::setContext(const std::string &context) { _context = context; } Task::Priority Task::getPriority() { return _priority; } void Task::setPriority(Task::Priority priority) { _priority = priority; } bool Task::isDone() { return _done; } void Task::isDone(bool done) { _done = done; } bool Task::isNextAction() { return _nextAction; } void Task::isNextAction(bool nextAction) { _nextAction = nextAction; }
  3. Stormtrooper

    Initialization list

    int main(int argc, char *argv[]) { Task *task = new Task("Hello world"); std::cout << task->getTask() << std::endl; return 0; } // // task.h // rtodo2 // // This code has been released under the BSD license // See license.txt // Copyright 2008 Ryan Capote All rights reserved. // #ifndef TASK_H #define TASK_H #include <string> class Task { public: typedef enum Priority {NORMAL, LOW, MEDIUM, HIGH}; Task(const std::string &task, const std::string &project, const std::string &context, Priority priority, bool done, bool nextAction); Task(const std::string &data); void loadData(const std::string &data); std::string serialize(); std::string getTask(); void setTask(const std::string &task); std::string getProject(); void setProject(const std::string &task); std::string getContext(); void setContext(const std::string &task); Priority getPriority(); void setPriority(Priority priority); bool isDone(); void isDone(bool done); bool isNextAction(); void isNextAction(bool nextAction); private: std::string _task; std::string _project; std::string _context; Priority _priority; bool _done; bool _nextAction; }; #endif
  4. Stormtrooper

    Initialization list

    My initialization list isn't setting my variables. No compiler errors, no run time errors. Task::Task(const std::string &task, const std::string &project = "none", const std::string &context = "none", Task::Priority priority = Task::NORMAL, bool done = false, bool nextAction = false) : _task(task), _project(project), _context(context), _priority(priority), _done(done), _nextAction(nextAction) { }
  5. Something that really helped me understand GUI systems is Guichan.
  6. Stormtrooper

    An OOP question

    A good thing to keep in mind while programming is that OOP is a tool, not a way of life. Use objects where objects will best help you solve a problem, and use functions where functions will solve the problem. You can do it either way. Putting everything in classes will help with organization, but might add more complexity than is needed.
  7. Stormtrooper

    Duplication?

    So I discovered strtok(), but every time I use it, it returns a stinking bus error. This is getting on my nerves. Maybe I'll switch back to my comfy cozy C++.
  8. Stormtrooper

    Duplication?

    I presume I will just have to coppy it to a new array, everytime I finish reading a part of the data string. Unless what I do is create a pointer to a character, then just keep free()ing the number of characters I don't need anymore.
  9. Stormtrooper

    what is a good "paint" program.

    I also have to recommend Paint.NET...excellent program!
  10. Stormtrooper

    Duplication?

    Hmm you're right. After I read the data upto the first comma, is there a way to cut that out? It didn't like me copyring the string from data back into data using strcpy. ripoff,load_task is ment to load a line from a txt file. So I think my problem is in the code parsing, as visitor said.
  11. Stormtrooper

    Duplication?

    char *task_serialize(Task *task) { // Add one for the camma int size = strlen(task->task); size += strlen(task->project); size += strlen(task->context); // two for priority size += 1; // two for done bool size += 1; // two for nextAction bool and cammas size += 8; char *result = malloc(size); //memcpy((void *)result, (void *)task, sizeof(task)); sprintf( result, "%s,%s,%s,%i,%i,%i|", task->task, task->project, task->context, task->priority, task->done, task->nextAction); return result; } Output: Check emai,Check email,none,comp,Check email,none,computer,0,1,1|,0,0,0| Should be: Check email,none,computer,0,1,1| Problem with my parsing code?
  12. Stormtrooper

    Duplication?

    Okay, I understand what you are saying. Shouldn't this work? char *task_serialize(Task *task) { int size = strlen(task->task); size += strlen(task->project); size += strlen(task->context); // one for priority size += 1; // one for done bool size += 1; // one for nextAction bool and null termination size += 2; char *result = malloc(size); //memcpy((void *)result, (void *)task, sizeof(task)); sprintf( result, "%s,%s,%s,%i,%i,%i|", task->task, task->project, task->context, task->priority, task->done, task->nextAction); return result; }
  13. Stormtrooper

    Duplication?

    Here (zip)
  14. Stormtrooper

    Duplication?

    So I tried this to get a better estimate for the size: int size = strlen(task); size += strlen(project); size += strlen(context); // one for priority size += 1; // one for done bool size += 1; // one for nextAction bool size += 1; Task *result = malloc(size); result->task = task; result->project = project; result->context = context; result->priority = atoi(priority); result->done = atoi(done); result->nextAction = atoi(nextaction); And got this output: Check emai,Check email,none,comp,Check email,none,computer,0,1,1|,0,0,0| should be Check email,none,computer,0,1,1|
  15. Stormtrooper

    Duplication?

    I have this(albeit messy) code that parses a string and fills a struct with the data. However, I get weird results. int main(int argc, char **argv) { Task *task = load_task("Check email,none,computer,0,1,1|"); printf("%s\n", task_serialize(task)); free(task); return 0; } char *task_serialize(Task *task) { char *result = malloc(sizeof(task)); sprintf( result, "%s,%s,%s,%i,%i,%i|", task->task, task->project, task->context, task->priority, task->done, task->nextAction); return result; } Task *load_task(char *data) { Task *result = malloc(sizeof(Task)); // First get the task description char *i = strchr(data, ','); int in = i - data; char *task = malloc(in); strncpy(task, data, in - 1); result->task = task; free(task); // Remove the task desc. //strncpy(data, data + in, strlen(data)); // Get the task project i = strchr(data, ','); in = in + i - data; char *project = malloc(in); strncpy(project, data, in - 1); result->project = project; free(project); // Remove the task desc. //strncpy(data, data + in, strlen(data)); // Get the context i = strchr(data, ','); in = in + i - data; char *context = malloc(in); strncpy(context, data, in - 1); result->context = context; free(context); // Remove the task desc. //strncpy(data, data + in, strlen(data)); // Get the priority i = strchr(data, ','); in = in + i - data; char *priority = malloc(in); strncpy(priority, data, in - 1); result->priority = atoi(priority); free(priority); // Remove the task desc. //strncpy(data, data + in, strlen(data)); // Get if the task is done i = strchr(data, ','); in = in + i - data; char *done = malloc(in); strncpy(done, data, in - 1); result->done = atoi(done); free(done); // Remove the task desc. //strncpy(data, data + in, strlen(data)); // Get if the task is next action i = strchr(data, ','); in = in + i - data; char *nextaction = malloc(in); strncpy(nextaction, data, in - 1); result->nextAction = atoi(nextaction); free(nextaction); return result; } I get this weird output: Check email,none,computer,0,1,1|,,computer,0,1,1|,,0,0,0| It should be what I inputted: Check email,none,computer,0,1,1|
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!