Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Lab 05 - Structs and enums

Reading strings

Help function for reading longer strings with spaces into a char*

char* getInput()
{
	char* string = NULL;
	int strlen = 0;
	while(1)
	{
		char character;
		scanf("%c", &character);
		if(character == 10)
		{
			return string;
		}
		strlen++;
		string = realloc(string, strlen);
		string[strlen-1] = character;
	}
}

Strings revision

  • Write a program which will ask the user for their name, and set the first letter of each word to uppercase. Remember to use functions!
  • Let’s build a program that can take an input string from the user and remove any whitespace (spaces) at the start and end of the string.
    • We will be re-using this code later, so remember to make use of functions
  • Let’s build a program that will take the user’s name “vaclav havel”, and will output:
    • Name 1: Vaclav
    • Name 2: Havel
  • You can reuse your functions from before for capitalization and white-space removal.

Enums and structs

  • Let’s build on our previous example and ask for the user's detail, including date of birth, but implement this as an enum.
  • Let’s write a program that will ask for people's names and store them in a struct.
    • The structs will be dynamically allocated every time a person enters a new user’s information

Doxygen

Examples

  • local or global variables

/// number of color channels
static const int numChannels = 3;

  • macro

/// A macro that returns the maximum of \a a and \a b.
#define MAX(a,b) (((a)>(b))?(a):(b))

  • enum or struct

/// Enum for area fill modes. Passed to sglAreaMode() function.
enum sglEAreaMode {
  SGL_POINT = 0,	///< Draw only vertices
  SGL_LINE  = 1,	///< Draw only borders of graphics elements (lines)
  SGL_FILL  = 2 	///< Draw filled elements, default
};

  • function declaration

/// Create a new drawing context.
/**
  Create a new drawing context for the drawing window with dimensions width x height of
  RGB pixels. Allocates additional internal structures for the context (including the
  depth buffer).

  \param[in] width   Desired canvas width.
  \param[in] height  Desired canvas height.
  \return Unique identifier of the drawing context.

  ERRORS:
   - SGL_OUT_OF_MEMORY ... not enough memory
   - SGL_OUT_OF_RESOURCES ... too many contexts
     (Implementation dependent. At least 32 must be provided, though.)
*/
int sglCreateContext(int width, int height);

courses/be5b99cpl/labs/lab05.txt · Last modified: 2022/10/31 11:11 by brouggeo