Cpp Learning Discussion

The game so far.. (moved from CeeProgramsForBeginnersDiscussion)

 #include <iostream>

struct RoomDefinition { char *description; RoomDefinition *nextRoom[2]; };

// Declare that these rooms exist, prior to defining what they are: extern RoomDefinition footpath, forest, stream1, clearing, fairycircle; extern RoomDefinition finishgame, bridge, fort, bed;

RoomDefinition footpath = { "You are on a footpath... To go to the forest enter 1, to go to the stream enter 2\n", { &forest, &stream1 } };

RoomDefinition forest = { "You are in the forest. Enter 1 to go to the clearing, Enter 2 to return to the footpath\n", { &clearing, &footpath } };

RoomDefinition clearing = { "You are in the clearing. Enter 1 to go to the fairy circle or 2 to return to the forest.\n", { &fairycircle, &forest } };

RoomDefinition fairycircle = { "You are in the fairy circle. Enter 1 to leave the world or 2 to return to the clearing.\n", { &finishgame, &clearing } }; RoomDefinition finishgame = { "Thanks for playing. I hope you enjoyed exploring, see you soon.\n", { 0, 0 } // 0 means exit };

RoomDefinition stream1 = { "You are standing next to a stream. Enter 2 to find a bridge or 1 to go back to the footpath.\n", { &footpath, &bridge } };

RoomDefinition bridge = { "You have found a bridge. To cross the bridge to the fort enter 2 or 1 to go back to the stream.\n", { &stream1, &fort } };

RoomDefinition fort = { "You have reached the fort. Enter 2 to go to bed or 1 to go back to the bridge.\n", { &bridge, &bed } };

RoomDefinition bed = { "Night Night sleepy-head. Thank-you for playing.\n", { 0, &bridge } // 0 means exit };

RoomDefinition * move_somewhere(RoomDefinition *currentRoom) { RoomDefinition *nextRoom; int userChoice;

std::cin >> userChoice; if (userChoice == 1) nextRoom = currentRoom->nextRoom[0]; else nextRoom = currentRoom->nextRoom[1]; if (!nextRoom) { // exit if room pointer is 0 system("PAUSE"); exit(0); } return nextRoom; }

main() { RoomDefinition *room = &footpath; while (1) { std::cout << room->description; room = move_somewhere(room); } } // End of Susannah's code recast into the data-driven // form that Jonathan recommended.

// Note that forest and clearing have paths leading to each other, which // was what you wanted. But this is quite different from having functions // forest() and clearing() call each other, which is what someone was talking // about above. // This is a natural confusion; the reason it doesn't work as well for them // to call each other is that a function call must return to where // it started. But that's not true of moving around in a forest. So the // problem is correctly modeled as a change to data, and less correctly // as a function call. -- dm

Here are a few obvious questions which beginners might ask -
  1. The struct differs from the one previously discussed - how exactly does it work in the above?

  2. If "char *" is replaced by "string " in the definitions, what exactly is the difference in the structure?
    • That raises the question of whether to introduce "char *" at all, or just go with "string". It's not clear to me that it's good to pretend that "char *" doesn't exist anymore, though.
      • So prior to answering it would be useful to some to explain the "*" syntax explicitly, though that tends to lead into areas that needn't be explored yet. The char vs string issue isn't specific to this coding example.
  3. Are there alternatives to struct in C++ which achieve a similar overall structure?
    • No, because a class is a struct in C++. Or vice versa, if you prefer.
      • That may be true, but needs to be put in layman's terms, given that the term "class" hasn't been introduced yet.
  4. What kinds of structure, if any, are NOT readily accomplishable in C++, even though they are in other languages?
    • What are you driving at here?
      • Beginners in C++ may have previous experience of other languages, and need to know if what they imagine ought to be straightforward is not in fact doable. Example question: can characters held in memory be given alternative structures (analogous to different record types)?
  5. What is the point of putting a space after an opening brace and before a closing brace?
  6. The "->" syntax looks clumsy (two characters for one symbol). Any comments on it?
    • Sure: no, it's not clumsy. I mean, ok, sure, you clearly think so, but that doesn't make it an absolute truth. This is such a small issue compared with the rest of C++ that I wouldn't worry about it at all. We could switch to (*somestruct).member if that serves a pedagogical purpose - but not for aesthetics.
      • I grant it's easy enough to get used to, but bear in mind that it was presented without any previous mention of its meaning. Bear in mind that "switch to" is ambiguous - does it mean that what you switch to is equivalent to the original or an alternative to (but slightly different from) the original?
      • The construct (*somestruct).member is precisely equivalent to somestruct->member, in the absence of operator overloading. (Beware of classes that do operator overloading -- redefine the meaning of -> or * or other operators; resulting code can be misleading.) Since it's syntactic sugar, it should be mentioned only in passing after the former construct has been explained. It's not necessary to use it in writing code, but does need to be understood for reading code. Perhaps I should change the code away from using "->".
  7. Certain minor aspects of coding style are visible in the above. Can someone with the SAMS book comment on whether it uses the same conventions?
    • I've borrowed the SAMS book (4th edition, 2001) from the library :-). I'll return with news about its style soon.
  8. The statements are given in a sensible order, but are there any arcane rules about order or positioning of statements, especially definitions?
    • Good question, but doesn't the subject need to be discussed before a beginner can comment on it?
      • These are not questions I am putting to a beginner, but questions that a beginner might raise. Obviously, I am not suggesting digging something really obscure out of the rules, but remember that the beginner needn't have previous experience of the relevance of statement order.

Thanks. Hmm...as to rephrasing...hmm....
CeePlusPlusRoadMap CategoryCpp

EditText of this page (last edited February 24, 2012) or FindPage with title or text search