| School of Physics |
|
| Physics Home | Study here | Our Teaching | Our Research | Our Centres | News | Work here | EMPS |
Back to top
PHY3134 Exercise 2: notesThese are a few quick notes to help you with the exercise. Strings don't need the "&" with scanf()For example:
int i;
char name[MAXLEN];
...
scanf("%i", &i);
scanf("%s", name);
Similarly if foobar is an array of structures which contain an integer i and a character array called name we write:
scanf("%i", &foobar[j].i);
scanf("%s", foobar[j].name);
Remember, that if foobar is an array of structures then foobar[j] is just a single structure. Print it out!Since it's so easy to go wrong I would suggest printing out the player's name the moment we read it in. This is handily combined with the "token" question:
printf("%s, please enter your game token\n", player[i].name);
Don't be surprised if it doesn't print out correctly the first time. Consider leaving the token checks until lastThese are not really central to the program and if you don't have time to finish you can leave them out. The "play a game" functionWe ask you to move all the code for actually playing a game into a separate function. This is something you do a lot as a programmer (although it will often be a function from the very beginning). There are a couple of useful things to think about: If we had to play two games, what would need to happen twice?Basically, everything that happens twice goes inside the "play a game" function, everything that only happens once goes outside.Our functions should just do one thingIf our function just plays the game, it's easy to have a loop inside of main() that repeatedly calls the function, prints out the scores so far, and asks if they want to play another game. This is probably easier than putting the question side the "play" function, and is certainly cleaner. |