Author Topic: C SOME UTILITY functions ...  (Read 395440 times)

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: C SOME UTILITY functions ...
« Reply #15 on: June 05, 2013, 07:24:35 PM »
This version of the program uses the file ...

"Student_List.h"

found above ...

It also needs file ...

"takeInLine.h" found at ...

http://developers-heaven.net/forum/index.php/topic,2580.msg3207.html#msg3207

and file "readLine.h" found at ...

http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

Note also, the file "StudentGradesManagementList2.c ", NOW ALL FITS into ONE PAGE here :)

Code: [Select]
/* StudentGradesManagementList2.c */  /* revised 2016-11-02 */

/*
    C/C++ students may like to see ...
    http://developers-heaven.net/forum/index.php/topic,127.0.html
    http://developers-heaven.net/forum/index.php/topic,134.0.html
    http://developers-heaven.net/forum/index.php/topic,106.0.html
    http://developers-heaven.net/forum/index.php/topic,46.0.html
*/

#include "Student_List.h" /* includes takeInLine.h that includes readLine.h etc... */


#define header1 "Student GRADES MANAGEMENT SYSTEM"
#define header2 \
    "1. (I)nitialize (i.e. CLEAR) Student record list in RAM\n" \
    "2. (A)dd i.e. INSERT into LIST inorder by name, a NEW Student record.\n" \
    "3. view/(D)elete a Student record\n" \
    "4. view/(E)dit a Student record\n" \
    "5. (S)how all Student records (as inserted BY NAME above)\n" \
    "6. (F)ile all Student records presently in RAM\n" \
    "7. (R)ead all Student records on file into RAM\n" \
    "8. set/change (P)assword\n" \
    "9. e(X)it"

#define FNAME "Studentinfo.txt"
#define FNAME_PW "StudentPW.txt"
#define MIN_PW_LEN 8
#define MIN_PW_LEN_STR "8"
#define MAX_STR_LEN 23
#define MAX_STR_LEN_STR "23"


int showMenuGetChoice( List* lst ) ;
void initList2ndCheck( List* lst ) ;
int fillFromFile( List* lst ) ;
void showAll( const List* lst ) ;
void add( List* lst ) ;

void viewDel( List* lst  );

void editStudent( List* lst, pStudent old_pS );
void viewEdit( List* lst );

int writeFile( List* lst );
void writeAllToFile( List* lst );
int exitNowYesNo( List* lst );

void scramble( char s[] );
void unscramble( char s[] );

int passWord( List* lst );
int newPW( List* lst );



int main() /* ************* BEGIN MAIN ****************** */
{
    int done = 0;
    List myLst;

    initList( &myLst );

    passWord( &myLst );

    if( fillFromFile( &myLst ) )
    {
        if( myLst.size != 1  )
            printf("There were %d records read from file ...\n", myLst.size );
        else
            printf("There was %d record read from file ...\n", myLst.size );
        showAll( &myLst );
    }
    else
    {
        printf( "There was some problem opening file %s ... \n"
                "perhaps it doesn't exist yet?\n", FNAME );
    }

    while( !done )
    {
        switch(  showMenuGetChoice( &myLst ) )
        {
            case '1' : case 'i' : case 'I' : initList2ndCheck( &myLst ); break;
            case '2' : case 'a' : case 'A' : add( &myLst ); break;
            case '3' : case 'd' : case 'D' : viewDel( &myLst ); break;
            case '4' : case 'e' : case 'E' : viewEdit( &myLst ); break;
            case '5' : case 's' : case 'S' : showAll( &myLst ); break;
            case '6' : case 'f' : case 'F' : writeAllToFile( &myLst ); break;

            case '7': case 'r': case 'R':
                if( myLst.size )
                {
                    if( tolower( takeInChar( "Do you really want "
                                             "to over-ride present memory? "
                                             "(y/n) ? " )) == 'y' )
                    {
                        clearList( &myLst );

                        if( fillFromFile( &myLst ) )
                            printf( "\n%d records were read in from file %s\n",
                                    myLst.size, FNAME );
                        else printf( "\nThere was a problem opening file %s\n",
                                         FNAME );
                    }
                    else printf( "\nOver-ride aborted ... \n" );
                }
                else
                {
                    if( fillFromFile( &myLst ) )
                        printf( "\n%d records were read in from file %s\n",
                                myLst.size, FNAME );
                    else printf( "\nThere was a problem opening file %s\n",
                                     FNAME );
                }
                break;
            case '9' : case 'X' : case 'x' : done = exitNowYesNo( &myLst ); break;
            case '8' : case 'p' : case 'P' : newPW( &myLst ); break;
            default : printf( "\nNOT implemented here yet ...\n" );
           
        } /* end switch */
       
    } /* end while ! done */

    clearList( &myLst );
    return 0;
   
} /* ******************* END MAIN *********************** */





int showMenuGetChoice( List* lst )
{
    puts( header1 );

    if( lst->size != 1 )
        printf( "Presently there are %d records.\n\n", lst->size );
    else
        puts( "Presently there is 1 record in RAM.\n" );

    puts( header2 );
    return takeInChar( "\nPlease enter your selection  : " );
}


void initList2ndCheck( List* lst )
{
    pStudent phead = lst->head;

    if( phead != NULL )
    {
        if( tolower( takeInChar( "\nDo you wish to overwrite the "
                                 "records in memory y/n ? " )) == 'y' )
            clearList( lst );
        else
        {
            if( lst->size == 1 )
                puts( "1 Student Record was left intact in memory." );
            else
                printf( "%d Student Records were left intact in memory.\n",
                        lst->size );
        }
    }
    else puts( "\nThere were no records in memory to clear." );
}


int fillFromFile( List* lst )
{
    FILE* fin = fopen( FNAME, "r" );
    if( fin )
    {
        Student stud;
        char* line;
        int i = 0;
        while( ( line = readLine( fin ) ) )
        {
            ++i;
            if( i == 1 )
            {
                stud.id= atof(line);
                free( line );
            }
            else if( i == 2 ) stud.last = line;
            else if( i == 3 ) stud.first = line;
            else if( i == 4 )
            {
                stud.test1 = atof(line);
                free( line );
            }
            else if( i == 5 )
            {
                stud.test2 = atof(line);
                free( line );
            }
            else if( i == 6 )
            {
                stud.assignment = atof(line);
                free( line );
            }
            else if( i == 7 )
            {
                stud.labtest = atof(line);
                free( line );
            }
            else
            {
                stud.finalexam = atof(line);
                free( line );
                push_backList( lst, &stud );
                i = 0;
            }
        }
        fclose( fin );
        lst->fileFlag = 0;
        return 1;
    }
    return 0;
}

void showAll( const List* lst )
{
    pStudent p = lst->head;

    if( p == NULL )
    {
        puts("\nNo records in memory at present.") ;
        return;
    }

    /* If reach here ... */

    while( p != NULL )
    {
        showStudent( p );
        if( tolower( takeInChar( "\nPress 'Enter' to continue "
                                 "or enter 'A' to abort: " )) == 'a' ) return;
        p = p->next;
    }
}

void add( List* lst ) /* ... and insertList in the proper place in the list */
{
    pStudent pS;
    int ID = takeInIntPosMax( "Enter ID          : ", INT_MAX );

    if( pFindID( lst, ID ) || ID <= 0  ) /* i.e. if pointer returned is NOT NULL, the ID IS used */
    {
        printf( "\n\n *** ID %d is NOT available *** \n\n\n", ID );
        return; /* Exit to showMemuGetChoice right now ... */
    }

    /* If program reaches here, the ID just entered is available to use. */
    pS = (pStudent) malloc( sizeof(Student) );
    myAssert( ( pS != NULL ), "\nmalloc failed in add student ... \n" );

    pS->id = ID;

    pS->last  = takeInLineMaxLen    ( "Enter Last  Name  : ", MAX_STR_LEN );
    strToTitleCase( pS->last);
    pS->first = takeInLineMaxLen    ( "Enter First Name  : ", MAX_STR_LEN );
    strToTitleCase( pS->first);

    pS->test1 = takeInIntPosMax     ( "Enter Test1       : ", 100 );
    pS->test2 = takeInIntPosMax     ( "Enter Test2       : ", 100 );
    pS->assignment = takeInIntPosMax( "Enter Assignmemt  : ", 100 );
    pS->labtest = takeInIntPosMax   ( "Enter Lab Test    : ", 100 );
    pS->finalexam = takeInIntPosMax ( "Enter Final Exam  : ", 100 );

    showStudent( pS );

    if( tolower( takeInChar( "Ok to add (y/n) ? 0" )) == 'y' )
        insertList( lst, pS );
    else
    {
        printf( "\nNot added ...\n" );
        clearStudent( pS );
    }
}

void viewDel( List* lst )
{
    int ID = takeInIntPosMax( "Enter the id number to View/Delete : " , INT_MAX );
    pStudent p = pFindID( lst, ID ); /* get pointer value or NULL */
    if( p ) /* i.e. if pointer returned above was not NULL ... */
    {
        showStudent( p );
        if( tolower( takeInChar( "\nDelete (y/n) ? " )) == 'y' )
            delList( lst, p) ;
    }
    else printf( "\nStudent with ID number %d not found.\n", ID );
}

void editStudent( List* lst, pStudent old_pS )
{
    pStudent pS;

    int idTmp = old_pS->id; /* Firstly get a backup copy of this id ... */

    int ID = takeInIntPosMax( "Enter EDITED ID          : ", INT_MAX );

    old_pS->id = -1; /* Now ... reset old id number to a dummy value ... */

    if( pFindID( lst, ID ) ) /* i.e. if pointer returned not NULL, this 'ID' IS USED */
    {
        printf( "\nid %d is NOT available.\n", ID );
        old_pS->id = idTmp; /* Restore the id since this pS was NOT  edited */
        return; /* Exit to showMemuGetChoice right now ... */
    }

    /* If reach hear ... insertList new pStudent in new memory with new data */

    pS = (pStudent) malloc( sizeof(Student) );
    myAssert( ( pS != NULL ), "\nmalloc failed in add student ... \n" );

    pS->id = ID;

    pS->last  = takeInLineMaxLen    ( "Enter EDITED Last  Name  : ",
                                         MAX_STR_LEN );
    strToTitleCase( pS->last);
    pS->first = takeInLineMaxLen    ( "Enter EDITED First Name  : ",
                                         MAX_STR_LEN );
    strToTitleCase( pS->first);

    pS->test1 = takeInIntPosMax     ( "Enter EDITED Test1       : ", 100 );
    pS->test2 = takeInIntPosMax     ( "Enter EDITED Test2       : ", 100 );
    pS->assignment = takeInIntPosMax( "Enter EDITED Assignmemt  : ", 100 );
    pS->labtest = takeInIntPosMax   ( "Enter EDITED Lab Test    : ", 100 );
    pS->finalexam = takeInIntPosMax ( "Enter EDITED Final Exam  : ", 100 );

    showStudent( pS );

    if( tolower( takeInChar( "Ok to add (y/n) ? 0" )) == 'y' )
    {
        delList( lst, old_pS );
        insertList( lst, pS );
    }
    else
    {
        old_pS->id = idTmp; /* Restore the id since this pS was NOT added */
        printf( "\nNot added ...\n" );
        clearStudent( pS );
    }
}

void viewEdit( List* lst )
{
    int ID = takeInIntPosMax( "Enter the id number to View/Edit : ", INT_MAX );

    pStudent p = pFindID( lst, ID ); /* if pointer exists, get value or NULL */
    if( p )     /* i.e. if pointer returned above was not NULL ... */
    {
        showStudent( p );
        if( tolower( takeInChar( "\nEdit (y/n) ? " )) == 'y' )
            editStudent( lst, p );
    }
    else printf( "\nStudent with ID number %d not found.\n", ID );
}

int writeFile( List* lst )
{
    int count = 0; /* to track the records actually written to the file */
    FILE* fp;
    pStudent p = lst->head;

    if( ! lst->size )
    {
        puts( "\nNo records available ... so NO records written to file." ) ;
        return 0;
    }

    fp = fopen( FNAME, "w" );
    if( fp == NULL )
    {
        printf( "\nError opening file %s.  Press 'Enter' to continue ... ", FNAME );
        takeInChar( "" );
        return 0;
    }

    while( p != NULL )
    {
        fprintf
        (
            fp,
            "%d\n"
            "%s\n"
            "%s\n"
            "%.2f\n"
            "%.2f\n"
            "%.2f\n"
            "%.2f\n"
            "%.2f\n",
            p->id, p->last, p->first,
            p->test1, p->test2, p->assignment,
            p->labtest, p->finalexam
        );

        ++count;
        p = p->next;
    }

    fclose( fp );
    return count; /* Number of records written. */
}

void writeAllToFile( List* lst )
{
    if( !lst->fileFlag )
    {
        printf( "\nThere was no need to update file %s\n", FNAME );
        return;
    }

    if( lst->size == writeFile( lst ) )
    {
        if( lst->size > 0 )
        {
            if( lst->size == 1 )
                puts( "\nThe 1 Student record was safely filed." );
            else
                printf
                (
                    "\nAll %d Student records safely filed.\n",
                    lst->size
                );
        }
        lst->fileFlag = 0;
    }
    else
    {
        printf
        (
            "\nAn error occured while filing the Student records."
            "\nPlease see the programmer for help with the problem.\n"
            "\nExiting the program now.  Press 'Enter' to continue ... "
        );
        takeInChar( "" ); /* Holds window open to show above message. */
        exit( 1 ); /* Return error number to system. */
    }
}

int exitNowYesNo( List* lst )
{
    int done = 0;
    if( tolower( takeInChar( "\nPress 'X' to eXit  ... "  )) == 'x' )
    {
        if( lst->fileFlag )
            writeAllToFile( lst );
        if( tolower( takeInChar( "\nPress 'Enter' to exit "
                                 "right now ... "  )) == '\n' )
            done = 1;
    }
    return done;
}


void scramble( char s[] )
{
    int i = 0;
    int code[] = {3,1,4,1,5,9,8,6,7,0,7,0,2,8,6,9,5,3,4,2};
    while( s[i]!=0 ) {
        s[i] = (char) ((int)s[i] - code[i]);
        ++i;
    }
}

void unscramble( char s[] )
{
    int i = 0;
    int code[] = {3,1,4,1,5,9,8,6,7,0,7,0,2,8,6,9,5,3,4,2};
    while( s[i]!=0 ) {
        s[i] = (char) ((int)s[i] + code[i]);
        ++i;
    }
}


int newPW( List* lst )
{
    FILE* fp;
    char* buffer1;
    char* buffer2;

    for( ; ; )
    {
        /* Get the new password into a string in buffer1. */
        buffer1 =  takeInLineMaxLen( "Enter the new password (" MIN_PW_LEN_STR
                                     " to " MAX_STR_LEN_STR
                                     " characters): ", MAX_STR_LEN );
        if( strlen(buffer1) >= MIN_PW_LEN )
            break;

        printf("\nYour password must be at least %d characters ...\n", MIN_PW_LEN );
        free( buffer1 ); /* ... and try again ...*/
    }

    /*
        Get a 'verify copy' of the new password into buffer2
        and compare it with the password in buffer1.
    */
    buffer2 = takeInLine( "Enter the new password again: " );

    if( strcmp( buffer1, buffer2 ) == 0 ) /* If true ... passwords matched. */
    {
        fp = fopen( FNAME_PW, "w" );
        if( fp == NULL )
        {
            printf( "Error opening file %s ... Press 'Enter' to exit ... ", FNAME_PW );
            takeInChar( "" );
            free( buffer2 );
            free( buffer1 );
            exit( 2 );
        }
        else
        {
            puts( "Match!\n" );
            scramble( buffer1 ) ;
            fprintf( fp, "%s", buffer1 );
            fclose( fp );
            free( buffer2 );
            free( buffer1 );
            lst->pwOK = 1;
            return 1; /* Report this match of passwords back ...*/
        }
    }

    /* If reach here ...*/

    takeInChar( "NO MATCH! ... Press 'Enter' to exit ... " );
    free( buffer2) ;
    free( buffer1 );
    exit( 3 ); /* Quit the program right now ... */
}

int passWord( List* lst )
{
    /*
        Get the password in the file, if it exists, into buffer2
        and compare it with the user entry in buffer1.
    */
    char* buffer1;
    char buffer2[MAX_STR_LEN+1];
    int attempts;

    FILE* fp = fopen( FNAME_PW, "r" );

    if( fp == NULL ) /* i.e. if file FNAME_PW doesn't exit ... */
    {
        newPW( lst ); /* get new password ...*/
        if( lst->pwOK == 1 ) return 1; /* report this match of passwords back ...*/
    }
    else /* File FNAME_PW does exist ... so ... */
    {
        fscanf( fp, "%s", buffer2 );
        fclose( fp );
        unscramble( buffer2 );

        /* Now ... get password entered by user into a string in buffer1. */
        for( attempts = 0; attempts < 3; ++attempts )
        {
            for( ; ; )
            {
                /* Get the new password into a string in buffer1. */
                buffer1 =  takeInLineMaxLen( "Enter your password (" MIN_PW_LEN_STR
                                             " to " MAX_STR_LEN_STR
                                             " characters): ", MAX_STR_LEN );
                if( strlen(buffer1) >= MIN_PW_LEN )
                    break;

                printf("\nYour password must be at least %d characters ...\n", MIN_PW_LEN );
                free( buffer1 ); /* ... and try again ...*/
            }



            if( strcmp( buffer1, buffer2 ) == 0 ) /* If true, passwords matched. */
            {
                puts( "Match!\n" );
                free( buffer1 );
                return 1; /* Report this match of passwords back ...*/
            }
            free( buffer1 );
        }
    }
    /* if reach here ...*/
    takeInChar( "NO MATCH! ... Press 'Enter' to exit ... " );
    exit( 4 ); /* Quit the program right now ... */
}
« Last Edit: November 06, 2016, 07:09:48 PM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: C SOME UTILITY functions ...
« Reply #16 on: June 05, 2013, 07:40:15 PM »
And here ... the program in this next file ...

"StudentGradesManagementList3.c"

needs to have 5 other files available to be included ...


1. "takeInLine.h" (and this includes file 2. "readLine.h")

http://developers-heaven.net/forum/index.php/topic,2580.msg3207.html#msg3207

2. and file "readLine.h" found at ...

http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

3. "Clist.h"

http://developers-heaven.net/forum/index.php/topic,2582.msg2877.html#msg2877

4. "Clist_func's.h"

http://developers-heaven.net/forum/index.php/topic,2582.msg2883.html#msg2883

5. "passWord.h" (see code for this file that follows here ... )


Note that the List code and insert into a List in sorted order code are now split off into two readily reusable generic List files called "Clist.h" and "Clist_func's.h"...

to facilitate code reuse :)


Code: [Select]
/* StudentGradesManagementList3.c */  /* revised 2016-11-02 */

/*
NOTE: Needs/Uses a total of 5 include files ...
1. passWord.h << NOTE that this includes takeInLine.h >>
2. & 3. takeInLine.h << Note that this includes file readLine.h >>
4. Clist.h
5. Clist_func's.h
*/

/*
    C/C++ students may like to see ...
    http://developers-heaven.net/forum/index.php/topic,127.0.html
    http://developers-heaven.net/forum/index.php/topic,134.0.html
    http://developers-heaven.net/forum/index.php/topic,106.0.html
    http://developers-heaven.net/forum/index.php/topic,46.0.html
*/

#include "passWord.h" /* and this includes takeInLine.h and  that includes readLine.h
                         and that includes... (see readLine.h ) */

#define HEADER1 "STUDENT GRADES MANAGEMENT SYSTEM"
#define HEADER2 \
    "1. (I)nitialize (i.e. CLEAR) Student record list in RAM\n" \
    "2. (A)dd i.e. INSERT into LIST inorder by name, a NEW Student record.\n" \
    "3. view/(D)elete a Student record\n" \
    "4. view/(E)dit a Student record\n" \
    "5. (S)how all Student records (as inserted BY NAME above)\n" \
    "6. (F)ile all Student records presently in RAM\n" \
    "7. (R)ead all Student records on file into RAM\n" \
    "8. set/change (P)assword\n" \
    "9. e(X)it"

#define FNAME "Studentinfo3.txt"

#define MAX_CSTR_LEN 23
#define MAX_CSTR_LEN_STR "23"


typedef struct myStudent
{
    int id;
    char* last;
    char* first;
    int test1;
    int test2;
    int assignment;
    int labtest;
    int finalexam;
    struct myStudent* next;
} Node ;

typedef Node* pNode;

void clearNode( pNode cur )
{
    free( cur->first );
    free( cur->last );
}

/* AFTER above 3 def'ns ... NOW ... can ... */
#include "Clist.h"
#include "Clist_func's.h"


/* some func's used here ... */
void showStudent( const pNode pS )
{
    float avg = ( pS->test1 + pS->test2 + pS->assignment +
                  pS->labtest + pS->finalexam ) / 5.0;
    printf
    (
        "ID number  : %d\n"
        "Last Name  : %s\n"
        "First Name : %s\n"
        "Test 1     : %d\n"
        "Test 2     : %d\n"
        "Assignment : %d\n"
        "Lab Test   : %d\n"
        "Final Exam : %d\n"
        "Final avg  : %.1f \n",
        pS->id, pS->last, pS->first,
        pS->test1, pS->test2, pS->assignment,
        pS->labtest, pS->finalexam,
        avg
    );
}

/* A function to compare two List records to permit sorting ... */
int StudentCmp( const pNode pS1, const pNode pS2 )
{
    int compare = strcmp( pS1->last, pS2->last );
    if( compare == 0 )
        return strcmp( pS1->first, pS2->first) ;
    return compare;
}

/* A function to compare two List records to permit finding ID ... */
int StudentCmpID( const pNode pS1, const pNode pS2 )
{
    return pS1->id - pS2->id;
}

pNode pFindID( Clist* lst, int id )
{
    Node tmp;
    pNode p = &tmp;
    p->id = id;
    return findClist( lst, p, StudentCmpID );
}


int showMenuGetChoice( Clist* lst ) ;
void initList2ndCheck( Clist* lst ) ;
int fillFromFile( Clist* lst ) ;
void showAll( const Clist* lst ) ;
void add( Clist* lst ) ;

void viewDel( Clist* lst  );

void editStudent( Clist* lst, pNode old_pS );
void viewEdit( Clist* lst );

int writeFile( const Clist* lst );
void writeAllToFile( const Clist* lst );

int exitNowYesNo( const Clist* lst );




int main() /* ************ BEGIN MAIN ******************* */
{
    int done = 0;
    Clist myLst;

    initClist( &myLst );
    initPrivateData();

    passWord();

    if( fillFromFile( &myLst ) )
    {
        if( myLst.size != 1  )
            printf( "There were %d records read from file ...\n", myLst.size );
        else
            printf( "There was %d record read from file ...\n", myLst.size );
        showAll( &myLst );
    }
    else
        printf( "There was some problem opening file %s ...\n"
                "??? perhaps it doesn't exist yet ???\n\n", FNAME );

    while( !done )
    {
        switch(  showMenuGetChoice( &myLst ) )
        {
        case '1' : case 'I' : case 'i' : initList2ndCheck( &myLst ); break;
        case '2' : case 'A' : case 'a' : add( &myLst ); break;
        case '3' : case 'd' : case 'D' : viewDel( &myLst ); break;
        case '4' : case 'e' : case 'E' : viewEdit( &myLst ); break;
        case '5' : case 'S' : case 's' : showAll( &myLst ); break;
        case '6' : case 'f' : case 'F' : writeAllToFile( &myLst ); break;

        case '7': case 'r': case 'R':
            if( myLst.size )
            {
                if( tolower( takeInChar( "Do you really want "
                                         "to over-ride present memory? "
                                         "(y/n) ? " )) == 'y' )
                {
                    clearClist( &myLst );

                    if( fillFromFile( &myLst ) )
                        printf( "\n%d records were read in from file %s\n",
                                myLst.size, FNAME );
                    else printf( "\nThere was a problem opening file %s\n",
                                     FNAME );
                }
                else printf( "\nOver-ride aborted ... \n" );
            }
            else
            {
                if( fillFromFile( &myLst ) )
                    printf( "\n%d records were read in from file %s\n",
                            myLst.size, FNAME );
                else printf( "\nThere was a problem opening file %s\n",
                                 FNAME );
            }
            break;

        case '9' : case 'X' : case 'x' : done = exitNowYesNo( &myLst ); break;

        case '8': case 'p': case 'P': newPW(); break;

        default : printf( "\nNOT implemented here yet ...\n" );
        }

    }

    clearClist( &myLst );

    return 0;
} /* ****************** END MAIN ************************ */





int showMenuGetChoice( Clist* lst )
{
    puts( HEADER1 );

    if( lst->size != 1 )
        printf( "Presently there are %d records.\n\n", lst->size );
    else
        puts( "Presently there is 1 record in RAM.\n" );

    puts( HEADER2 );
    return takeInChar( "\nPlease enter your selection  : " );
}


void initList2ndCheck( Clist* lst )
{
    pNode phead = lst->start;

    if( phead != NULL )
    {
        if( tolower( takeInChar( "\nDo you wish to overwrite the "
                                 "records in memory y/n ? " )) == 'y' )
        {
            clearClist( lst );
            PrivateData.fileFlag = 0;
        }
        else
        {
            if( lst->size == 1 )
                puts( "1 Student Record was left intact in memory." );
            else
                printf( "%d Student Records were left intact in memory.\n",
                        lst->size );
        }
    }
    else puts( "\nThere were no records in memory to clear." );
}


int fillFromFile( Clist* lst )
{
    FILE* fin = fopen( FNAME, "r" );
    if( fin )
    {
        Node stud;
        char* line1;
        char* line4;
       
        while( (line1 = readLine( fin )) && (stud.last = readLine( fin )) &&
               (stud.first = readLine( fin )) && (line4 = readLine( fin )) )
        {
                stud.id= atof(line1);
                free( line1 );
                sscanf( line4, "%d %d %d %d %d",
                        &stud.test1, &stud.test2, &stud.assignment,
                        &stud.labtest, &stud.finalexam );
                free( line4 );
                push_backClist( lst, &stud );
        }
        fclose( fin );
        return 1;
    }
    return 0;
}

void showAll( const Clist* lst )
{
    pNode p = lst->start;

    if( p == NULL )
    {
        puts("\nNo records in memory at present.") ;
        return;
    }

    /* If reach here ... */

    while( p != NULL )
    {
        showStudent( p );
        if( tolower( takeInChar( "\nPress 'Enter' to continue showing records, "
                                 "or enter 'A' to abort (showing): " )) == 'a' ) return;
        p = p->next;
    }
}

void add( Clist* lst ) /* ... and insertList in the proper place in the list */
{
    pNode pS;
    int ID = takeInIntPosMax( "Enter ID          : ", INT_MAX );

    if( pFindID( lst, ID ) || ID <= 0  ) /* i.e. if pointer returned is NOT NULL, the id IS used */
    {
        printf( "\n\n *** ID %d is NOT available *** \n\n\n", ID );
        return; /* Exit to showMemuGetChoice right now ... */
    }

    /* If program reaches here, the ID just entered is available to use. */
    pS = (pNode) malloc( sizeof(Node) );
    myAssert( ( pS != NULL ), "\nmalloc failed in add student ... \n" );

    pS->id = ID;

    pS->last  = takeInLineMaxLen       ( "Enter Last  Name  : ",
                                         MAX_CSTR_LEN );
    strToTitleCase( pS->last);

    pS->first = takeInLineMaxLen       ( "Enter First Name  : ",
                                         MAX_CSTR_LEN );
    strToTitleCase( pS->first);

    pS->test1 = takeInIntPosMax      ( "Enter Test1       : ", 100 );
    pS->test2 = takeInIntPosMax      ( "Enter Test2       : ", 100 );
    pS->assignment = takeInIntPosMax ( "Enter Assignmemt  : ", 100 );
    pS->labtest = takeInIntPosMax    ( "Enter Lab Test    : ", 100 );
    pS->finalexam = takeInIntPosMax  ( "Enter Final Exam  : ", 100 );

    showStudent( pS );
    /* insert_sortedClist( Clist*, pNode, int (*myCmp) (const pNode, const pNode) ); */
    if( tolower( takeInChar( "Ok to add (y/n) ?" )) == 'y' )
    {
        insert_sortedClist( lst, pS, StudentCmp );
        PrivateData.fileFlag = 1;
    }
    else
    {
        printf( "\nNot added ...\n" );
        clearNode( pS );
    }
}


void viewDel( Clist* lst )
{
    int ID = takeInIntPosMax ( "Enter the id number to View/Delete : " , INT_MAX );
    pNode p = pFindID( lst, ID ); /* get pointer value or NULL */
    if( p ) /* i.e. if pointer returned above was not NULL ... */
    {
        showStudent( p );
        if( tolower( takeInChar( "\nDelete (y/n) ? " )) == 'y' )
        {
            eraseClist( lst, p ) ;
            PrivateData.fileFlag = 1;
        }
    }
    else printf( "\nStudent with ID number %d not found.\n", ID );
}

void editStudent( Clist* lst, pNode old_pS )
{
    pNode pS;

    int idTmp = old_pS->id; /* Firstly get a backup copy of this id ... */

    int ID = takeInIntPosMax        ( "Enter EDITED ID         : ", INT_MAX );

    old_pS->id = -1; /* Now ... reset old id number to a dummy value ... */

    if( pFindID( lst, ID ) ) /* i.e. if pointer returned not NULL, this 'ID' IS USED */
    {
        printf( "\nid %d is NOT available.\n", ID );
        old_pS->id = idTmp; /* Restore the id since this pS was NOT  edited */
        return; /* Exit to showMemuGetChoice right now ... */
    }

    /* If reach hear ... insertList new pNode in new memory with new data */

    pS = (pNode) malloc( sizeof(Node) );
    myAssert( ( pS != NULL ), "\nmalloc failed in add student ... \n" );

    pS->id = ID;

    pS->last  = takeInLineMaxLen    ( "Enter EDITED Last  Name : ", MAX_CSTR_LEN );
    strToTitleCase( pS->last);
    pS->first = takeInLineMaxLen    ( "Enter EDITED First Name : ", MAX_CSTR_LEN );
    strToTitleCase( pS->first);

    pS->test1 = takeInIntPosMax     ( "Enter EDITED Test1      : ", 100 );
    pS->test2 = takeInIntPosMax     ( "Enter EDITED Test2      : ", 100 );
    pS->assignment = takeInIntPosMax( "Enter EDITED Assignmemt : ", 100 );
    pS->labtest = takeInIntPosMax   ( "Enter EDITED Lab Test   : ", 100 );
    pS->finalexam = takeInIntPosMax ( "Enter EDITED Final Exam : ", 100 );

    showStudent( pS );

    if( tolower( takeInChar( "Ok to add (y/n) ? " )) == 'y' )
    {
        eraseClist( lst, old_pS );
        insert_sortedClist( lst, pS, StudentCmp );
        PrivateData.fileFlag = 1;
    }
    else
    {
        old_pS->id = idTmp;
        printf( "\nNot added ...\n" );
        clearNode( pS );
    }
}

void viewEdit( Clist* lst )
{
    int ID = takeInIntPosMax( "Enter the id number to View/Edit : ", INT_MAX );

    pNode p = pFindID( lst, ID ); /* if pointer exists, get value or NULL */
    if( p )     /* i.e. if pointer returned above was not NULL ... */
    {
        showStudent( p );
        if( tolower( takeInChar( "\nEdit (y/n) ? " )) == 'y' )
            editStudent( lst, p );
    }
    else printf( "\nStudent with ID number %d not found.\n", ID );
}

int writeFile( const Clist* lst )
{
    int count = 0; /* to track the records actually written to the file */
    FILE* fp;
    pNode p = lst->start;

    if( ! lst->size )
    {
        puts( "\nNo records available ... so NO records written to file." ) ;
        return 0;
    }

    fp = fopen( FNAME, "w" );
    if( fp == NULL )
    {
        printf( "\nError opening file %s.  Press 'Enter' to continue ... ", FNAME );
        takeInChar( "" );
        return 0;
    }

    while( p != NULL )
    {
        fprintf
        (
            fp,
            "%d\n"
            "%s\n"
            "%s\n"
            "%d %d %d %d %d\n",
            p->id, p->last, p->first,
            p->test1, p->test2, p->assignment,
            p->labtest, p->finalexam
        );

        ++count;
        p = p->next;
    }

    fclose( fp );
    return count; /* Number of records written. */
}

void writeAllToFile( const Clist* lst )
{
    if( ! PrivateData.fileFlag )
    {
        printf( "\nThere was no need to update file %s\n", FNAME );
        return;
    }

    if( lst->size == writeFile( lst ) )
    {
        if( lst->size > 0 )
        {
            if( lst->size == 1 )
                puts( "\nThe 1 Student record was safely filed." );
            else
                printf
                (
                    "\nAll %d Student records safely filed.\n",
                    lst->size
                );
        }
        PrivateData.fileFlag = 0;
    }
    else
    {
        printf
        (
            "\nAn error occured while filing the Node records."
            "\nPlease see the programmer for help with the problem.\n"
            "\nExiting the program now.  Press 'Enter' to continue ... "
        );
        takeInChar( "" ); /* Holds window open to show above message. */
        exit( 1 ); /* Return error number to system. */
    }
}

int exitNowYesNo( const Clist* lst )
{
    int done = 0;
    if( tolower( takeInChar( "\nPress 'X' to eXit  ... "  )) == 'x' )
    {
        if( PrivateData.fileFlag )
            writeAllToFile( lst );
        if( tolower( takeInChar( "\nPress 'Enter' to exit "
                                 "right now ... "  )) == '\n' )
            done = 1;
    }
    return done;
}
« Last Edit: November 06, 2016, 07:17:36 PM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: C SOME UTILITY functions ...
« Reply #17 on: April 08, 2015, 09:31:14 AM »
And finally, the 5th file needed above: passWord.h

Code: [Select]
/* passWord.h */ /* 2016-11-01 */

#ifndef PASSWORD_H
#define PASSWORD_H

#include "takeInLine.h" /* and this includes readLine.h
                           and that includes... (see readLine.h ) */

#define FNAME_PW "StudentPW3.txt"
#define MIN_PW_LEN 8
#define MIN_PW_LEN_STR "8"

#ifndef MAX_STR_LEN
#define MAX_STR_LEN 40
#define MAX_STR_LEN_STR "40"
#endif

/* to 'avoid' the use of global var's ...
   'hidden' here as members of 'global' PrivateData */
struct myPrivateData
{
    int pwOK;
    int fileFlag;
} PrivateData;

void initPrivateData()
{
    PrivateData.pwOK = 0;
    PrivateData.fileFlag = 0;
}


/*
3.
141592653589793238462643383279502884197
1693993751058209749445923078164062862089
986280348253421170679

3,
1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5,0,2,8,8,4,1,9,7
*/

/* 's' can be up to max of 40 char's here (a NEW ENCODE/DECODE from before) */
void scramble( char s[] )
{
    int i = 0;
    int code[] = {3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,
                  3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,
                  9,5,0,2,8,8,4,1,9,7
                 };
    while( s[i]!=0 ) {
        s[i] = (char) ((int)s[i] - code[i]);
        ++i;
    }
}

void unscramble( char s[] )
{
    int i = 0;
    int code[] = {3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,
                  3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,
                  9,5,0,2,8,8,4,1,9,7
                 };
    while( s[i]!=0 ) {
        s[i] = (char) ((int)s[i] + code[i]);
        ++i;
    }
}


int newPW()
{
    FILE* fp;
    char* buffer1;
    char* buffer2;

    for( ; ; )
    {
        /* Get the new password into a string in buffer1. */
        buffer1 =  takeInLineMaxLen( "Enter the new password (" MIN_PW_LEN_STR
                                     " to " MAX_STR_LEN_STR
                                     " characters): ", MAX_STR_LEN );
        if( strlen(buffer1) >= MIN_PW_LEN )
            break;

        printf("\nYour password must be at least %d characters ...\n", MIN_PW_LEN );
        free( buffer1 ); /* ... and try again ...*/
    }

    /*
        Get a 'verify copy' of the new password into buffer2
        and compare it with the password in buffer1.
    */
    buffer2 = takeInLine( "Enter the new password again: " );

    if( strcmp( buffer1, buffer2 ) == 0 ) /* If true ... passwords matched. */
    {
        fp = fopen( FNAME_PW, "w" );
        if( fp == NULL )
        {
            printf( "Error opening file %s ... Press 'Enter' to exit ... ", FNAME_PW );
            takeInChar( "" );
            free( buffer2 );
            free( buffer1 );
            exit( 2 );
        }
        else
        {
            puts( "Match!\n" );
            scramble( buffer1 ) ;
            fprintf( fp, "%s", buffer1 );
            fclose( fp );
            free( buffer2 );
            free( buffer1 );
            PrivateData.pwOK = 1;
            return 1; /* Report this match of passwords back ...*/
        }
    }

    /* If reach here ...*/

    takeInChar( "NO MATCH! ... Press 'Enter' to exit ... " );
    free( buffer2) ;
    free( buffer1 );
    exit( 3 ); /* Quit the program right now ... */
}

int passWord()
{
    /*
        Get the password in the file, if it exists, into buffer2
        and compare it with the user entry in buffer1.
    */
    char* buffer1;
    char buffer2[MAX_STR_LEN+1];
    int attempts;

    FILE* fp = fopen( FNAME_PW, "r" );

    if( fp == NULL ) /* i.e. if file FNAME_PW doesn't exit ... */
    {
        newPW(); /* get new password ...*/
        if( PrivateData.pwOK == 1 ) return 1; /* report this match of passwords back ...*/
    }
    else /* File FNAME_PW does exist ... so ... */
    {
        fscanf( fp, "%s", buffer2 );
        fclose( fp );
        unscramble( buffer2 );

        /* Now ... get password entered by user into a string in buffer1. */
        for( attempts = 0; attempts < 3; ++attempts )
        {
            for( ; ; )
            {
                /* Get the new password into a string in buffer1. */
                buffer1 =  takeInLineMaxLen( "Enter your password (" MIN_PW_LEN_STR
                                             " to " MAX_STR_LEN_STR
                                             " characters): ", MAX_STR_LEN );
                if( strlen(buffer1) >= MIN_PW_LEN )
                    break;

                printf("\nYour password must be at least %d characters ...\n", MIN_PW_LEN );
                free( buffer1 ); /* ... and try again ...*/
            }



            if( strcmp( buffer1, buffer2 ) == 0 ) /* If true, passwords matched. */
            {
                puts( "Match!\n" );
                free( buffer1 );
                return 1; /* Report this match of passwords back ...*/
            }
            free( buffer1 );
        }
    }
    /* if reach here ...*/
    takeInChar( "NO MATCH! ... Press 'Enter' to exit ... " );
    exit( 4 ); /* Quit the program right now ... */
}

#endif



And now ... some new takeIns ...

with cleaner error checking with string take in for numbers ...

and take in loops until input data is in valid range.

Note!  This .h file needs the new readLine.h file available here:

http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

(Following this is a little test program.)

Code: [Select]
/* takeIns.h */  /* 2015-04-08 */

/* handy utilities for many C student coding problems ... */

#ifndef dwTAKEINS_H
#define dwTAKEINS_H

#include "readLine.h"

#include <limits.h> /* re. INT_MAX, etc... */
#include <float.h> /* re. DBL_MAX, etc... */
#include <math.h> /* re. log10(MAX_INT) */



int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    int c = takeInChr( "More (y/n) ? " );
if( c == 'n' || c == 'N' ) return 0;
/* else ... */
return 1;
}

char* takeInStr( const char* msg )
{
fputs( msg, stdout ); fflush( stdout );
return readLine( stdin );
}

/* Only NON empty strings of OK length accepted here ... */
char* takeInStrMaxLen( const char* msg, unsigned maxLen )
{
char* p = NULL;

    while( 1 )
{
p = takeInStr( msg );

/* if NOT 0 len and len ok ... */
if( p[0] && strlen( p ) <= maxLen )
break;
/* else ... */
        if( !p[0] ) printf( "\nBlank line NOT valid input here ... \n" );
else printf( "\nFor '%s', max len of %u char's was exceeded ... \n",
p, maxLen );
free( p );
}

return p;
}
char* takeInStrMinLen( const char* msg, unsigned minLen )
{
char* p = NULL;

    while( 1 )
{
p = takeInStr( msg );

/* if len ok ... */
if( strlen( p ) >= minLen )
break;
/* else ... */
        printf( "\nFor '%s', min len of %u char's was NOT met ... \n",
p, minLen );
free( p );
}

return p;
}
char* takeInStrLenInRange( const char* msg, unsigned minLen, unsigned maxLen )
{
char* p = NULL;
size_t len = 0;

    while( 1 )
{
p = takeInStr( msg );
len = strlen(p);

/* if len ok ... */
if( len >= minLen && len <= maxLen )
break;
/* else ... */
        printf( "\n'%s' needs to have %u..%u characters only.\n",
p, minLen, maxLen );
free( p );
}

return p;
}

/* a simple student way to handle numeric input ...
   so program won't crash on bad input */
int takeInInt( const char* msg )
{
    int val = 0;
    while( 1 ) /* loop forever until break is reached ... */
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
            break;
        else
        {
            printf( "\nInteger numbers only here please ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
    return val;
}
unsigned takeInUns( const char* msg )
{
    unsigned val = 0;
    while( 1 ) /* loop forever until break is reached ... */
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%u", &val ) == 1 && getchar() == '\n' )
            break;
        else
        {
            printf( "\nUnsigned numbers only here please ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
    return val;
}

float takeInFlt( const char* msg )
{
    float val = 0;
    while( 1 ) /* loop forever until break is reached ... */
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%f", &val ) == 1 && getchar() == '\n' )
            break;
        else
        {
            printf( "\nReal numbers only here please ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
    return val;
}
double takeInDbl( const char* msg )
{
    double val = 0;
    while( 1 ) /* loop forever until break is reached ... */
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%lf", &val ) == 1 && getchar() == '\n' )
            break;
        else
        {
            printf( "\nReal numbers only here please ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
    return val;
}


int isInRange( const char* min, const char* max, char* val )
{
    unsigned ok = 1, sign = 1,
             maxlen = strlen(max), minlen = strlen(min);
             
    if( val[0] == '+' ) memmove( val, val+1, strlen(val) );
    else if( val[0] == '-' ) sign = -1;
   
    if( sign == 1)
    {
        if( strlen(val) == maxlen )
            ok = (strcmp( val, max ) <= 0);
        else
            ok = (strlen(val) < maxlen);
    }
    else /* is negative ... */
    {
        if( strlen(val) == minlen )
            ok =  (strcmp( val, min ) <= 0);
        else
            ok = (strlen(val) < minlen);
    }

    if( !ok )
        printf( "\nValid range here is: %s..%s\n", min, max );

    return ok;
}

int takeInStrInt( const char* msg )
{
    int val = 0;
    char c;
    char* line = NULL;
    char* maxVal = newMem( log10(INT_MAX)+2 );
    char* minVal = newMem( log10(INT_MAX)+2 );
    sprintf( maxVal, "%d", INT_MAX );
    sprintf( minVal, "%d", INT_MIN );
    /* printf( "minVal = %s and maxVal = %s\n", minVal, maxVal ); */
    while( 1 ) /* loop forever until break is reached ... */
    {
        line = ltrim( takeInStr( msg ) );

        if( sscanf( line, "%d%c", &val, &c ) == 1 )
        {
            if( isInRange( minVal, maxVal, line ) )
            {
                free( line ); free( minVal ); free( maxVal );
                break;
            }
            else /* err msg printed in isInRange check  */
                free( line );
        }
        else
        {
            free( line );
            printf( "\nInteger numbers only here please ...\n" );
        }
    }
    return val;
}
unsigned takeInStrUns( const char* msg )
{
    unsigned val = 0;
    char c;
    char* line = NULL;
    char* maxVal = newMem( log10(UINT_MAX)+2 );
    char* minVal = newMem( 2 );
    sprintf( maxVal, "%u", UINT_MAX );
    sprintf( minVal, "%u", 0 );
    /* printf( "minVal = %s and maxVal = %s\n", minVal, maxVal ); */
    while( 1 ) /* loop forever until break is reached ... */
    {
        line = ltrim( takeInStr( msg ) );

        if( sscanf( line, "%u%c", &val, &c ) == 1 && line[0] != '-' )
        {
            if( isInRange( minVal, maxVal, line ) )
            {
                free( line ); free( minVal ); free( maxVal );
                break;
            }
            else /* err msg printed in isInRange check  */
                free( line );
        }
        else
        {
            free( line );
            printf( "\nUnsigned numbers only here please ...\n" );
        }
    }
    return val;
}


float takeInStrFlt( const char* msg )
{
    float val = 0;
    char c;
    char* line = NULL;

    /*printf( "minVal = %e and maxVal = %e\n", DBL_MIN, DBL_MAX ); */
    while( 1 ) /* loop forever until break is reached ... */
    {
        line = takeInStr( msg ) ;

        if( sscanf( line, "%f%c", &val, &c ) == 1 )
        {
            free( line );
            break;
        }
        else
        {
            free( line );
            printf( "\nReal numbers only here please ...\n" );
        }
    }
    return val;
}

double takeInStrDbl( const char* msg )
{
    double val = 0;
    char c;
    char* line = NULL;

    /*printf( "minVal = %e and maxVal = %e\n", DBL_MIN, DBL_MAX ); */
    while( 1 ) /* loop forever until break is reached ... */
    {
        line = takeInStr( msg ) ;

        if( sscanf( line, "%lf%c", &val, &c ) == 1 )
        {
            free( line );
            break;
        }
        else
        {
            free( line );
            printf( "\nReal numbers only here please ...\n" );
        }
    }
    return val;
}


int takeInStrIntInRange( const char* msg, const int min, const int max )
{
    int val = 0;
    while( 1 )
    {
        val = takeInStrInt( msg );
        if( val >= min && val <= max )
            break;
        /* else ... */
        printf( "\nValid range here is %d..%d\n", min, max );
    }
    return val;
}

double takeInStrDblInRange( const char* msg, const double min, const double max )
{
    double val = 0;
    while( 1 )
    {
        val = takeInStrDbl( msg );
        if( val >= min && val <= max )
            break;
        /* else ... */
        printf( "\nValid range here is %e..%e\n", min, max );
    }
    return val;
}

#endif
« Last Edit: November 06, 2016, 07:18:47 PM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: C SOME UTILITY functions ...
« Reply #18 on: April 08, 2015, 09:36:03 AM »
And now the little test program for the above new takeIns ...

Code: [Select]
/* test_takeIns.h.c */  /*  2015-04-08 */


#include "takeIns.h" /* includes readLine.h */


int main()
{
    do
    {
        int ival = 0;
        unsigned uval = 0;
        float fval = 0;
        double dval = 0;
       
        char* sval = takeInStrLenInRange( "Enter a line of text : ", 5, 10 );
        printf( "You entered '%s', with strlen = %u and my_strlen = %u\n",
                sval, (unsigned)strlen(sval), (unsigned)my_strlen(sval) );
        free( sval );
       
       
        ival = takeInStrInt( "Enter an integer : " );
        printf( "You entered: %d\n", ival );
       
        uval = takeInStrUns( "Enter an unsigned integer : " );
        printf( "You entered: %u\n", uval );
       
        fval = takeInStrFlt( "Enter a float decimal value : " );
        printf( "You entered: %e\n", fval );
       
        dval = takeInStrDbl( "Enter a double decimal value : " );
        printf( "You entered: %e\n", dval );
       
       
        ival = takeInStrIntInRange( "Enter an integer in range 10..100 : ",
                                    10, +100 );
        printf( "You entered: %d\n", ival );
       
        dval = takeInStrDblInRange( "Enter a double decimal value in range -100.1..+100.1 : ",
                                    -100.1, 100.1 );
        printf( "You entered: %e\n", dval );
       
       
    }
    while( more() );

    return 0;
}
« Last Edit: April 08, 2015, 09:43:38 AM by David »