Skip to main content »

Trinity College Dublin

Questions, comments and answers.


26 January 2009
c++/++c Just to clarify the difference between the two incrementors (c++ and ++c) mentioned in class today. I stumbled across a definition of both plus a sample programme that explains them. I thought it would be good to have these on the website or to use them in class.

++c preincrement operator. Increment c by 1 and then use the new value in the expression in which c resides.

c++ postincrement operator. Use the current value of c in the expression in which c resides, then increment c by 1.

Sample code is at Increment.cpp

Reply and the usual remark is that the language should be called ++c rather than c++

26 January 2009
Assigning to a char I am trying to use the char data type as you have done in your notes. However, when I try to compile (with g++) I get the following error:

"initialization to `char' from `const char *' lacks a cast"

I've tried replacing 'char' with 'const char *' and this works the way I would have hoped 'char' would. Do you know why this is happening? I have included both copies of the programme, firstly as it appears in your notes and then with the change to char.

      #include

      using namespace std;

      int main()
      {

          char x="e";
          cout<

and corrected



     #include

     using namespace std;

     int main()
     {

         const char *x="e";
         cout<

Reply Wrong sort of quotes! You need a single quote not a double one for chars. The explanation is somewhat technical, but basically double quotes like "some characters" are used for strings and single quotes, like the letter 'e' is used for a single characters. It might look like "e" contains a single character but it doesn't, it consists of the string e\0, the \0 is the null character, it does not print out, but it used to indicate the end of a string, hence when you try to assign "e" to a char, it tries to assign e\0 which is two char and isn't allow, it then assumes you wanted to declare an array of chars which is what you do with the * and so on in your corrected version of the programme.

Further comment Thanks it works fine now! Just so you know, within your notes online and in the handout the double quotes are used for the char declarations in the Cast.cpp programme. When you link into the programme it is with single quotes but in the actual text of the notes they are double.

Further reply Thanks, I have fixed that now. In a way it was good I caused you to make this mistake because it is a funny error and it is good to have it noted on the website; but I am sorry for the inconvenience it caused you!

9 March 2009
Website The link to the 'code' section of the webpage doesn't appear to be working, could you fix this?
Reply Thanks for letting me know, it should work now.

20 March 2009
Projects I was just looking over the list of project ideas for your 481++ course and had two questions. First, by what date must the projects be submitted? Also, for the tic-tac toe project, for it to count as two projects does the AI need to be perfectly optimal or would a decent attempt at optimal play (ie. not perfect but not easy to beat every time) be acceptable? Thanks.
Reply End of term and any programme that evaluated the game position and moved based on that evaluation would be fine: it is a computing rather than a tic-tac-toe theory project: a programme that tried its best to always loose would be just as good. You should note that this seems to me to be easily the hardest project.

12 April
Dev-C++ Hi, I recently installed Dev-C++ on my pc. Whenever I write and compile a program, it complies fine, but when I click 'run', a window with my desired output only flashes up for a split second, how do I stop it closing this window? I'd rather like to see the end result. Thanks,
Reply I don't have a windows computer so I can't promise my answer is correct, but I found a tutorial about Div-C++ which says that the window closes when the program is done, which is annoying, and suggests the low tech solution of adding an input before the end, a press return when done sort of thing.
http://www.uniqueness-template.com/devcpp/
Alternatively, you can run the compiled programme in a terminal window, again, this is described in the tutorial. Let me know if this works..
Further message I just found a solution like that online, cout << "Press enter to exit" <<... and so on, appears to work. Some suggested solutions on forums weren't working for me, but this one does the trick, thanks anyway!

12 April 2009
Loading in words and characters One problem I am having is to load in sentences. I can only load the first word and it (after the first space) just ignores the rest of the text? Is this something you can help me with? thanks.
Reply I would suggest reading the text in from a file, I have added code to do that to the project lists; one for characters and one for words. Basically it opens a file called "Example.txt" and the character one reads it in character by character using the get() method, this just grabs the next character. It stops when it detects the end of the file, the word one uses the stream operator >> to take words off the file one by one, the stream operator is clever about skipping spaces and just taking words. Let me know if these make sense.