readHelpers


Why do I care?

readHelpers are a group of functions that I have written that will make it easier to read input from the user.  When you use scanf() and gets(), sometimes you run into problems where a scanf will not stop and wait for the user to type any input -- your program will ask the user for the information, but then go straight on without giving the user a chance to type anything.  The methods needed to fix this are painful to use, so I wrote the readHelpers to avoid the problem all together.

As we worked in lab on Friday, it became apparent that lots of you were going to run into this type of problem with homework 3, hence these functions.

What are they really?

There are three readHelper functions.  Here are their prototypes:

int readInt(void);      // reads in a single integer from the user, and then returns the number.

char readChar(void);   // reads in a single character from the user, and returns that character.

void readLine(char stringVar[]);    // reads in an entire line from the user, and fills in the passed in stringVar as a string (with a null terminator)

Here are some examples of how you might use these functions.  Be sure to read these carefully.  Remember you don't have to use the same variable names -- these are examples only!

Here is how you might use readInt():

int numVar;
printf("Type a number: ");
numVar = readInt();


And here is how you can use readChar():

char myChar;
printf("Type a character: ");
myChar = readChar();

Lastly, readLine():

char myStringArray[45];
printf("Type a string: ");
readLine(myStringArray);

When do I use these?


Use these functions for homework 3 instead of scanf or gets.  You should not have any calls to scanf or gets in your code for homework 3.    For homework 3, these calls will be in your main() -- since main() will ask the user for the string to search and what characters to look for in that string.

How do I use the files you gave us?

I've given you two files:   readHelpers.h and readHelpers.c.   To use them, do the following:
  1. download both files into the same directory as your .c file for this assignment
  2. add the following to your source file, with your other includes:
#include "readHelpers.h"       // note the double quotes instead of the <>

  1. When compiling your code with gcc, you need to add readHelpers.c to the end of your gcc line.   If your program is called homework3.c, your gcc would look like this:
# gcc -o homework3.exe homework3.c readHelpers.c
# ./homework3.exe

That's all folks.....