getchar

The Following code is to go along with our getchar() Lesson:


#include <cstdlib>
#include <stdio.h>
using namespace std;


int ReadLine(char* Buffer,int bufsize){
    char ch;
    int read=0;
    do{
        ch=getchar();
        Buffer[read]=ch;
        read++;
    }while (ch!='\n'&&read<bufsize);
    /* Subtract 1 for the newline*/
    read--;
    /*Convert newline to null*/
    Buffer[read]='\0';
    
    return(read);
}


int main(int argc, char** argv) {

    char buffer[255];
    int rc;
    printf("Enter Some Data:\n");
    rc=ReadLine(buffer,255);
    printf("You Typed: %s\n\nTotal Characters: %d\n",buffer,rc);
    return 0;
}