Computer Club Meeting 10-24-2017

In our meeting today we wrote a program to get user input and then display that input on the console.  The following listing is an example of the program:

/*we include the stdio.h header file which contains the prototypes for
* the printf and getc functions
*/

#include <stdio.h>

/*the main function is the entry point of our program
*when we execute our program this is where it starts from
*/
int main(int argc, char** argv) {

    /*We create a variable type char to store our user input.
    *Our variable which we call  UserInput and we allocate 255 bytes of storage.
    */
    char UserInput[255];

    /*Another variable called PlaceHolder.  This is an integer and we initialize it to 0*/
    int PlaceHolder=0;

    /*We prompt the user to enter his or her name*/
    printf ("What is your name? ");

    /*Now we enter the do loop.  We will get one character at a time until the user presses return*/
    do{
        /*the getc  function reads one character at a time
         * We store that charcter in UserInput at the position indicated by
         * Placeholder
         */
         UserInput[PlaceHolder]=getc(stdin);


         /*We increment placeholder by 1 so that the next call
         * to getc stores the result in the next position
         */
         PlaceHolder++;

    }while(UserInput[(PlaceHolder-1)]!='\n');
    /*We have to use PlaceHolder-1 in the line about to check for return
    *This is because we already increased PlaceHolder for the next position which we have
    *not read in yet.
    */

    /*Now we display Hello and what the user entered at our prompt
    */
    printf("Hello %s",UserInput);

    /*The program is complete*/
    return 0;
}

mrcampbell / October 24, 2017 / Computer Club