Skip to main content
added 6 characters in body
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188

I agree with your teacher that breaking the code into functions withwill help the readability of your code. 

For example, haveput the code, for the result of each decision, in its own function.

cout << "1. Go West" << endl; ... if (choice2 == 1) { go_west(); } else if (choice2 == 2) { go_east(); } else if (choice2 == 3) { wait(); } 

If I am reading the code this makes it much easier to figure out what is happening. What happens if I input 2? I do not need to skip over all the go west or wait for something to happen code because they are in functions somewhere else. If I am interested in that code then I look at the function.

This also helps if two routes lead to the same result, you do not need to copy and paste the code you simply call the same function.

If you have learned about switch statements then it might get you some more points to use a switch instead of if/else.

switch (choice2) { case 1: go_west(); break; case 2: go_east(); break; case 3: wait(); break; } 

This is not very important in the real world though. Use whichever you think looks better.

It would also be an idea to use enumsenums or constant values when processing the decision points. If iI look at the if/else statments Istatements I need to rememeberremember: does 1 mean east or west?

#define WEST 1 #define EAST 2 #define WAIT 3 if (choice2 == WEST) { go_west(); } else if (choice2 == EAST) { go_east(); } else if (choice2 == WAIT) { wait(); } 

A more advanced strategy would be to use a jump table.

void(*tbl[4])(void) = { &invalid_input, &go_west, &go_east, &wait }; cin >> choice2; if (choice2 > 3) choice2 = 0; tbl[choice2](); 

This may not be a viable option for you at your current stage but it might help other people or yourself later on in your education.

I agree with your teacher that breaking the code into functions with help the readability of your code. For example, have the code for the result of each decision in its own function.

cout << "1. Go West" << endl; ... if (choice2 == 1) { go_west(); } else if (choice2 == 2) { go_east(); } else if (choice2 == 3) { wait(); } 

If I am reading the code this makes it much easier to figure out what is happening. What happens if I input 2? I do not need to skip over all the go west or wait for something to happen code because they are in functions somewhere else. If I am interested in that code then I look at the function.

This also helps if two routes lead to the same result, you do not need to copy and paste the code you simply call the same function.

If you have learned about switch statements then it might get you some more points to use a switch instead of if/else.

switch (choice2) { case 1: go_west(); break; case 2: go_east(); break; case 3: wait(); break; } 

This is not very important in the real world though. Use whichever you think looks better.

It would also be an idea to use enums or constant values when processing the decision points. If i look at the if/else statments I need to rememeber: does 1 mean east or west?

#define WEST 1 #define EAST 2 #define WAIT 3 if (choice2 == WEST) { go_west(); } else if (choice2 == EAST) { go_east(); } else if (choice2 == WAIT) { wait(); } 

A more advanced strategy would be to use a jump table.

void(*tbl[4])(void) = { &invalid_input, &go_west, &go_east, &wait }; cin >> choice2; if (choice2 > 3) choice2 = 0; tbl[choice2](); 

This may not be a viable option for you at your current stage but it might help other people or yourself later on in your education.

I agree with your teacher that breaking the code into functions will help the readability of your code. 

For example, put the code, for the result of each decision, in its own function.

cout << "1. Go West" << endl; ... if (choice2 == 1) { go_west(); } else if (choice2 == 2) { go_east(); } else if (choice2 == 3) { wait(); } 

If I am reading the code this makes it much easier to figure out what is happening. What happens if I input 2? I do not need to skip over all the go west or wait for something to happen code because they are in functions somewhere else. If I am interested in that code then I look at the function.

This also helps if two routes lead to the same result, you do not need to copy and paste the code you simply call the same function.

If you have learned about switch statements then it might get you some more points to use a switch instead of if/else.

switch (choice2) { case 1: go_west(); break; case 2: go_east(); break; case 3: wait(); break; } 

This is not very important in the real world though. Use whichever you think looks better.

It would also be an idea to use enums or constant values when processing the decision points. If I look at the if/else statements I need to remember: does 1 mean east or west?

#define WEST 1 #define EAST 2 #define WAIT 3 if (choice2 == WEST) { go_west(); } else if (choice2 == EAST) { go_east(); } else if (choice2 == WAIT) { wait(); } 

A more advanced strategy would be to use a jump table.

void(*tbl[4])(void) = { &invalid_input, &go_west, &go_east, &wait }; cin >> choice2; if (choice2 > 3) choice2 = 0; tbl[choice2](); 

This may not be a viable option for you at your current stage but it might help other people or yourself later on in your education.

added 404 characters in body
Source Link

I agree with your teacher that breaking the code into functions with help the readability of your code. For example, have the code for the result of each decision in its own function.

cout << "1. Go West" << endl; ... if (choice2 == 1) { go_west(); } else if (choice2 == 2) { go_east(); } else if (choice2 == 3) { wait(); } 

If I am reading the code this makes it much easier to figure out what is happening. What happens if I input 2? I do not need to skip over all the go west or wait for something to happen code because they are in functions somewhere else. If I am interested in that code then I look at the function.

This also helps if two routes lead to the same result, you do not need to copy and paste the code you simply call the same function.

If you have learned about switch statements then it might get you some more points to use a switch instead of if/else.

switch (choice2) { case 1: go_west(); break; case 2: go_east(); break; case 3: wait(); break; } 

This is not very important in the real world though. Use whichever you think looks better.

It would also be an idea to use enums or constant values when processing the decision points. If i look at the if/else statments I need to rememeber: does 1 mean east or west?

#define WEST 1 #define EAST 2 #define WAIT 3 if (choice2 == WEST) { go_west(); } else if (choice2 == EAST) { go_east(); } else if (choice2 == WAIT) { wait(); } 

A more advanced strategy would be to use a jump table.

void(*tbl[4])(void) = { &invalid_input, &go_west, &go_east, &wait }; cin >> choice2; if (choice2 > 3) choice2 = 0; tbl[choice2](); 

This may not be a viable option for you at your current stage but it might help other people or yourself later on in your education.

I agree with your teacher that breaking the code into functions with help the readability of your code. For example, have the code for the result of each decision in its own function.

cout << "1. Go West" << endl; ... if (choice2 == 1) { go_west(); } else if (choice2 == 2) { go_east(); } else if (choice2 == 3) { wait(); } 

If I am reading the code this makes it much easier to figure out what is happening. What happens if I input 2? I do not need to skip over all the go west or wait for something to happen code because they are in functions somewhere else. If I am interested in that code then I look at the function.

This also helps if two routes lead to the same result, you do not need to copy and paste the code you simply call the same function.

If you have learned about switch statements then it might get you some more points to use a switch instead of if/else.

switch (choice2) { case 1: go_west(); break; case 2: go_east(); break; case 3: wait(); break; } 

This is not very important in the real world though. Use whichever you think looks better.

It would also be an idea to use enums or constant values when processing the decision points. If i look at the if/else statments I need to rememeber: does 1 mean east or west?

#define WEST 1 #define EAST 2 #define WAIT 3 if (choice2 == WEST) { go_west(); } else if (choice2 == EAST) { go_east(); } else if (choice2 == WAIT) { wait(); } 

I agree with your teacher that breaking the code into functions with help the readability of your code. For example, have the code for the result of each decision in its own function.

cout << "1. Go West" << endl; ... if (choice2 == 1) { go_west(); } else if (choice2 == 2) { go_east(); } else if (choice2 == 3) { wait(); } 

If I am reading the code this makes it much easier to figure out what is happening. What happens if I input 2? I do not need to skip over all the go west or wait for something to happen code because they are in functions somewhere else. If I am interested in that code then I look at the function.

This also helps if two routes lead to the same result, you do not need to copy and paste the code you simply call the same function.

If you have learned about switch statements then it might get you some more points to use a switch instead of if/else.

switch (choice2) { case 1: go_west(); break; case 2: go_east(); break; case 3: wait(); break; } 

This is not very important in the real world though. Use whichever you think looks better.

It would also be an idea to use enums or constant values when processing the decision points. If i look at the if/else statments I need to rememeber: does 1 mean east or west?

#define WEST 1 #define EAST 2 #define WAIT 3 if (choice2 == WEST) { go_west(); } else if (choice2 == EAST) { go_east(); } else if (choice2 == WAIT) { wait(); } 

A more advanced strategy would be to use a jump table.

void(*tbl[4])(void) = { &invalid_input, &go_west, &go_east, &wait }; cin >> choice2; if (choice2 > 3) choice2 = 0; tbl[choice2](); 

This may not be a viable option for you at your current stage but it might help other people or yourself later on in your education.

Source Link

I agree with your teacher that breaking the code into functions with help the readability of your code. For example, have the code for the result of each decision in its own function.

cout << "1. Go West" << endl; ... if (choice2 == 1) { go_west(); } else if (choice2 == 2) { go_east(); } else if (choice2 == 3) { wait(); } 

If I am reading the code this makes it much easier to figure out what is happening. What happens if I input 2? I do not need to skip over all the go west or wait for something to happen code because they are in functions somewhere else. If I am interested in that code then I look at the function.

This also helps if two routes lead to the same result, you do not need to copy and paste the code you simply call the same function.

If you have learned about switch statements then it might get you some more points to use a switch instead of if/else.

switch (choice2) { case 1: go_west(); break; case 2: go_east(); break; case 3: wait(); break; } 

This is not very important in the real world though. Use whichever you think looks better.

It would also be an idea to use enums or constant values when processing the decision points. If i look at the if/else statments I need to rememeber: does 1 mean east or west?

#define WEST 1 #define EAST 2 #define WAIT 3 if (choice2 == WEST) { go_west(); } else if (choice2 == EAST) { go_east(); } else if (choice2 == WAIT) { wait(); }