What is data driven programming?

I have been set an assignment which suggest the use of data driven programming and I'm struggling to understand the difference between this and a standard coding style.

I have looked on various forums but I can't seem to find any c++ examples of data driven programs to see the difference.

Any help would be appreciated; sorry for the open ended question.
Data driven programming is where a program has a set of rules that operate on meta data that is loaded into the program. A different set of meta data will cause the program to operate differently using the same rules.

A recent example was posted where a typical dungeon crawler game was modified from the traditional hard coded if-else statements for every room to a table driven approach, where the information about each room (paths, objects, enemies, etc) was loaded at startup. By changing the data file loaded at startup, a completely different game could be played even though the rules for interpreting the tables did not change.
A simple example is to create a menu.

First the non-data driven way:
1
2
3
std::cout << "What do you want to do:\n"
          << "1) Eat\n"
          << "2) Sleep\n";


A data driven approach would be to create an array of strings as the options:
1
2
3
4
5
6
const char* options[] = { "Eat" , "Sleep" };
const unsigned int amt_options = sizeof(options) / sizeof(const char*);

std::cout << "What do you want to do:\n";
for (unsigned int i = 0; i < amt_options; i++)
  std::cout << i + 1 << ") " << options[i] << '\n';


The data-driven method is easier to change, because all that has to be done is change the strings that appear in the array.

After getting an option from the user, you may think to use a switch case with the results. The operations to perform based on the choice could be included in the data driven aspect. I made this little program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

double add(double x, double y)     { return x + y; }  
double subract(double x, double y) { return x - y; }
double multiply(double x, double y){ return x * y; }
double divide(double x, double y)  { return x / y; }  // No error checking

typedef double (*fn)( double, double );
fn operations[] = {add, subract, multiply, divide};

const char* choices[] = { "Add", "Subract", "Multiply", "Divide" };
const unsigned int amt_choices = sizeof(choices) / sizeof(const char*);

// User input methods
double getNum();
int getInt();
int getIntInRange(int low, int high);



int main(void)
{
  std::cout << "Welocome to the calculator, make a choice:\n";
  for (unsigned int i = 0; i < amt_choices; i++)
    std::cout << i + 1 << ") " << choices[i] << '\n';
  int choice = getIntInRange(1, amt_choices);
  
  std::cout << "Enter the first number: ";
  double first = getNum();
  std::cout << "Enter the second number: ";
  double second = getNum();
  
  std::cout << "The result is: " << operations[choice - 1](first, second) << '\n';
  
  return 0;
}
double getNum(void)
{
  double num;
  while (!(std::cin >> num))
  {
    std::cin.clear();
    std::cin.ignore(80, '\n');
    std::cout << "Numbers only please: ";
  }
  std::cin.ignore(80,'\n');
  return num;
}
int getInt(void)
{
  double num = getNum();
  while( num != static_cast<int>(num))
  {
    std::cout << "Integers only please: ";
    num = getNum();
  }
  return static_cast<int>(num);
}
int getIntInRange(int low, int high)
{
  int num = getInt();
  while (num < low || num > high)
  {
    std::cout << "Must be within " << low << " and " << high << ". (Inlclusive): ";
    num = getInt();
  }
  return num;
}


I also tried to help someone with a "choose your own adventure program", here is what I did there. (I think it still works...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* choosepath.cpp
 * 
 * Jan 25, 2013
*/

/* ChoosePath is a Choose Your Own Adventure program
 * http://en.wikipedia.org/wiki/Choose_Your_Own_Adventure
*/

/* A properly formated choosepath.txt is required for this to work
 * Format:
 *   unsigned int exitVal = the value at which the game exits
 *   REPEAT:
 *     unsigned int PageID = the page number
 *	   string Page Title = the title of the page
 *	   string Page Text = the text that appears on the page
 *						  can include line breaks, must end with @
 *	   REPEAT:  unsigned ints = Pages that this page links to
 *
 *  EXAMPLE FORMAT
 *
 * 10
 * 0
 * Hi
 * This is the first page
 * I hope you like it@
 * 1 2
 * 1
 * Exit
 * This page will exit@
 * 10
 * 2
 * Not Exit
 * This page goes to the welcome@
 * 0
 *
 *  In this example, the program will open to page 0, displaying "Hi"
 *  The user has two options, 1 and 2
 *  Selecting 2, the program will go to page 2, the "Not Exit"
 *  Page two can only go back to the welcome page
 *  Selection 1, the program will end because the "Exit" Page
 *    Has the exit value as it's option
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

class Page{
private:
	unsigned int pageId;
	std::string title;
	std::string text;
	std::vector<unsigned int> options;
public:
	Page(std::ifstream& input)	{
		input >> pageId;
		input.ignore();
		getline(input, title);
		getline(input, text, '@');
		input.ignore();
		unsigned int temp;
		int ch;
		while ((ch = input.peek()) != -1 && ch  != '\n')
		{
			input >> temp;
			options.push_back(temp);
		}
	}
	Page(const Page& other) :pageId(other.pageId), title(other.title), text(other.text)
	{
		options.reserve(other.options.size());
		for (unsigned int i = 0; i < other.options.size(); i++)
			options.push_back(other.options[i]);
	}
	unsigned int getPageId(void) const { return pageId; }
	std::string getTitle(void) const { return title; };
	std::string getText(void)  const { return text;  };
	const std::vector<unsigned int>& getOptions(void) const{ return options; }
};

unsigned int getValidInt(const std::vector<unsigned int>& v){
  unsigned int ret;
  while (true)  // The only exit is a valid choice. (ten lines from now)
  {
    std::cout << "Choose your option: ";
    if (!(std::cin >> ret))
    {
      std::cin.clear();
      std::cin.ignore(80, '\n');
      continue;
    }
    for (unsigned int i = 0; i < v.size(); i++)
      if (v[i] == ret) return ret;      
  }
}

int main(int argc, char*argv[]){
	const char *defaultFile = "choosepath.txt";
	const char *actualFile = defaultFile;
	
	if (argc == 2) actualFile = argv[1];

	std::ifstream in(actualFile);
	if (!in.good())
	{
		std::cout << "Could not open file: " << actualFile << std::endl;
		return 0;
	}
	
	// Read the exit value
	unsigned int exitVal;
	in >> exitVal;
	
	// Create the pages
	std::vector<Page> pages;
	while (in.good())
	{
		Page temp(in);
		pages.push_back(temp);		
	}
	// Done with the file
	in.close();
	
	
	Page *current = &pages.front();
	unsigned int choice;	
	while (current->getOptions().front() != exitVal)
	{
		std::cout << current->getTitle() << std::endl;
		std::cout << current->getText() << "\n\n";
		for (unsigned int i = 0; i < current->getOptions().size(); i++)
		{
			std::cout << pages[current->getOptions()[i]].getPageId() << ": "
					  << pages[current->getOptions()[i]].getTitle() << std::endl;
		}
		choice = getValidInt(current->getOptions());
		current = &pages[choice];		
		std::cout << "\n\n";
	}
	std::cout << current->getTitle() << std::endl;
	std::cout << current->getText() << std::endl;
	std::cout << "Thanks for playing\n";
	
	return 0;
}


Here is the file for input: (the @ symbols are so the text can have new lines)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1000
0
Welcome page
Welcome to the book, if this is your first time reading, press 1 for help@
1 2
1
Help page
Read the text on the screen.
At the end of the text you can make a choice as to where to go.
Input the number next to the choice you want@
0
2
The Beginning
You wake up in your house, what do you want to do?@
3 4
3
Make coffee
Now you're ready to face the day@
1000
4
Go back to sleep
Oh well@
1000 
Last edited on
Thanks fr the help guys- and both replies from Abstraction and Lowest were actually extremely relevant to the program I have been assigned.

Thanks for the help and the quick replies.
Topic archived. No new replies allowed.