SE1SA5 – Programming Project Review
As part of my degree studies at the University of Reading I was required to design and implement a either a Chatbot, or code analysis program in C. I opted for the former project as I thought it would be a more interesting challenge.
The first considerations were: how do Chatbots work; how are they implemented; are there existing examples of source to reference? Unfortunately it would seem as though C is not the best programming language to choose to develop a Chatbot; a point noted in a Tweet from Shirley Williams.
I looked around for quite a while at existing implementations and kept coming across A.L.I.C.E and ELIZA, two famous Chatbots that have been influential in the development of games and other software since their creation. Needless to say it was unrealistic to expect a similarly functional program to be designed, developed and tested in the time allowed with the skills acquired over the course so far; so I set about desiging a very simple Chatbot to ask a few questions, remember information and perform some basic deduction.
The errors I continually came across were generally my fault; simple problems such as using an ‘=’ sign when not appropriate, or missing off a semicolon from the end of a statement, although there were one or two interesting issues that kept me thinking for hours at a time, most notably: getchar().
56 57 58 | printf("Ready? (y or n)\n"); userAnswer = getchar(); |
This code should ask the simple question requiring a ‘y’ or ‘n’ answer, it should then read in the answer to the ‘userAnswer’ variable using getchar(); however, every time I ran the program it would seem to skip over line #58 and end the program. I spent a long time searching for the answer, and eventually discovered that getchar() was registering a previous carriage return as an input, and was in fact working. The simplest, albeit least elegant solution was to run the line twice, thereby avoiding the problem:
56 57 58 59 | printf("Ready? (y or n)\n"); userAnswer = getchar(); userAnswer = getchar(); |
I also struggled getting my head round nested IF statements; not particularly hard in principle, but difficult to keep track of in practise.
I am not a particularly confident programmer, most especially in C, so I deliberately set out to keep the program small, simple and most of all – working! I know that the program will not work as intended on other operating systems, this is due wholly to the fact that I have used code to clear the output screen, and to pause the program that is compatible with Windows only; there are similar functions that will also work in Linux, but for the moment the program will only work in Windows:
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | int clearScreen() { /* Function clears the standard output, only works in Windows. */ system("cls"); return 0; } int pauseProgram() { /* Function pauses program, waits for user to press any key, only works in Windows. */ system("PAUSE"); return 0; } |
In conclusion I would say that the assignment was challenging and enjoyable, but not without its difficulties. I would have preferred to develop the program in C++, most especially as our lectures had moved onto C++ already by the time the assignment started; remembering to switch back to C commands was an extra consideration which I think was a disadvantage.
I have certainly learned to structure my code and comment sensibly, as well as learning the need for a solid set of specifications rather than programming on the fly. Even if the specifications are small in number, it does not mean they are easy to implement.
James














i had the same problem with scanf as you did with getchar, it turned out, like you said the buffer wasnt beeing flushed properly. I found using getchar twice would only work if there was 1 character then the return, and therefore if in my case someone something totally random in it would loop the lenght of that random string. To solve it i manually flushed the buffer, with fflush(stdin).
[Reply]
Strictly, you shouldn’t use fflush() for stdin.
http://www.linuxforums.org/forum/linux-programming-scripting/41287-problem-fflush-stdin-function.html
Probably best to read everything in as a string from the user, do some checks and convert it as appropriate.
I like the review though. Do you think the freedom to do this in C++ would have helped or hindered? (i.e. uncertainties over string handling, etc.)
Andrew
[Reply]
I think the freedom to choose between the two languages would have been a more appropriate move, as it gives more confident / proficient coder-writers a chance to show off their skills.
I learnt a little Java when I did my OU course last year so my mind is already familiar with classes – having to learn C feels like a step backwards; albeit a necessary one.
I don’t know if doing the whole thing again in C++ would be of help or hinderence – I don’t think developing a chatbot in either language is easy.
[Reply]