0

400000000000;499999999999;VISA;

50000000;59999999;MASTERCARD;

67000000;67999999;MAESTRO;

fields: 1. Range Start 2. Range End, 3 Name.

[Start Range] and [End Range] fields can be from 1 to 16 characters (digits) in length.

The program's objective is as follows:

First Request to enter 16-digit card number.

 Card number input, verification and processing use char [n] type (Simbol array)

Second:Check for an entry corresponding to the entered card number can be found in a text file if I enter 45000000000 it's between 400000000000 and 499999999999 so i need to put a text in a autput name VISA. And i can't use long long types... as i undrstand i need to use arrays... Third Request to enter the amount in the format "nnnn.mm", where nnnn-1 to 4 digits long amount of lats, but mm - 2-digit amount santims.

char input[32]; // = "100;200;first";
char name[10];
int min, max, c, i, k;

FILE *file;
file = fopen("gg.txt","r");

i=0;

while ((c=getc(file))!= EOF)
{
    k=(int)c;
    input[i]=k;
    i++;
}

char* result = NULL;
char delims[] = ";";

result = strtok(input, delims);

// atoi() converts ascii to integer.
min = atoi(result);
result = strtok(NULL, delims);
max = atoi(result);

result = strtok(NULL, delims);

strcpy(name, result);

printf("Min=%d, Max=%d, Name=%s\n", min, max, name);
printf("input=%s\n",input);
printf("%d\n",i);

getch();
return 0;

this code given me by varunl works vith smal numbers ( the containing of gg.txt file is: 100;200;first), but a need smt else, enybody, can help me?

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Duplicate(ish) of earlier question: http://stackoverflow.com/questions/7784679/reading-and-comparing-numbers-from-txt-file-c – Burhan Ali Oct 16 '11 at 21:08

1 Answers1

1

The trick is in padding the numbers to 16 digits, so you can compare them as strings. So if you read this:

67000000;67999999;MAESTRO;

in reality you have to consider it like this:

0000000067000000;0000000067999999;MAESTRO;

The same for the user input.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void FindCCType(const char *pnumber);

int main(int argc, char *argv[])
{
    FindCCType("67000001");
    return 0;
}

#define MAXLENGTH 16

char* PadLeft(const char *pnumber)
{
    char *pnumber2 = (char*)malloc(MAXLENGTH + 1);
    size_t len = strlen(pnumber);

    if (len > MAXLENGTH)
    {
        /* Too many digits in credit card number */
        exit(1);
    }

    strcpy(pnumber2 + MAXLENGTH - len, pnumber);

    char *pbegin = pnumber2, *plast = pnumber2 + MAXLENGTH - len;

    while (pbegin < plast)
    {
        *pbegin = '0';
        pbegin++;
    }

    return pnumber2;
}

void FindCCType(const char *pnumber)
{
    printf("Input: %s\n", pnumber);

    char *pnumber2 = PadLeft(pnumber);

    FILE *file = fopen("gg.txt","r");

    char pline[1000];

    while (fgets(pline, sizeof(pline), file) != NULL)
    {
        if (strlen(pline) + 1 == sizeof(pline)) 
        {
            /* Line too much long */
            exit(2);
        }

        char *pmin = strtok(pline, ";");
        char *pmax = strtok(NULL, ";");
        char *pname = strtok(NULL, ";");

        printf("Min: %s, Max: %s, Name: %s", pmin, pmax, pname);

        char *pmin2 = PadLeft(pmin);
        char *pmax2 = PadLeft(pmax);

        if (strcmp(pnumber2, pmin2) >= 0 && strcmp(pnumber2, pmax2) <= 0)
        {
            printf(" ** FOUND **\n");

            free(pmin2);
            free(pmax2);
            break;
        }

        printf(" ** NO GOOD **\n");

        free(pmin2);
        free(pmax2);
    }

    fclose(file);
    free(pnumber2);
}
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • @edGGars What compiler are you using? – xanatos Oct 16 '11 at 17:13
  • I am new to C and I compile with deafoult compiler of DEV-C++, but I have task that I cant use long long types... –  Oct 16 '11 at 17:51
  • @edGGars If this is homework, and at this point it's quite clear it's homework, you should edit your post and put the tag homework. – xanatos Oct 16 '11 at 18:06