Thread: Help with my code...somethings Wrong..

  1. #1
    Learning C.
    Guest

    Arrow Help with my code...somethings Wrong..

    I don't seem to get what's wrong..
    When i try to test what comes out of choice and HelpP->id
    I'll get choice = a3, but HelpP->id = t <----???

    And another thing Can it be done a easier way than this.
    I need to take some data of from the file, and spilt the stings
    up, so I can use fx the birthday to calculate with.
    And later on the should be a sort, som one can search after diff. kinds og sportspeople.
    tks hope someone can help me..

    my code is:

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

    #define TEXTFILE "person.txt"

    int case3(void)
    {
    FILE *infile;
    char choice[4];
    int ch, entries;
    char personbuffer[1000];

    typedef struct person personT;
    struct person
    {
    char id;
    char name[40];
    int day, month, year;
    };
    personT *PpersonT, *HelpP;

    infile = fopen(TEXTFILE, "r");
    printf("Her e is a list from the file:\n\n");
    while ((ch=fgetc(infile)) !=EOF)
    {
    fputc(ch,stdout);
    }
    printf("\n\n");
    printf("How many persons do you want?: ");
    scanf("%d", &entries);
    fflush(stdin);
    /* Allocation */
    PpersonT = (personT*)malloc(sizeof(personT));

    for (HelpP=PpersonT; HelpP-PpersonT < entries; ++HelpP)
    {
    printf("Make your choice .\n"
    "Ex: [ a3 for Tiger Woods]: ");
    scanf("%s", &choice);
    fflush(stdin);
    if ((infile = fopen(TEXTFILE, "r")) !=NULL)
    {
    while(fgets(personbuffer,(int)sizeof personbuffer, infile) !=NULL)
    {
    if(sscanf(personbuffer, "%[^,],%[^,],%d.%d.%d", &HelpP->id,
    &HelpP->name,&HelpP->day,
    &HelpP->month, &HelpP->year) == 5)
    /* printf("Choice: %s\n", choice); TEST */
    /* printf("Rec->id: %s\n", HelpP->id); TEST */

    if(strcmp(&HelpP->id, choice) == 0)
    {
    printf("Your Choice:\n");
    printf("%s %s %d %d %d", &HelpP->id, &HelpP->name,&HelpP->day,
    &HelpP->month, &HelpP->year);

    }
    else
    printf("No luck");
    }
    }
    }

    getch();
    return 1;
    }

  2. #2
    Learning C
    Guest
    BTW:

    The file I reading from are something about this:

    a1, Thomas Bjoern, 18.02.1971
    a2, Adam Scott, 16.07.1980
    a3, TigerWoods, 30.12.1975
    a4, Matt Kuchar, 21.06.1978

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    The only things I can see is wrong..
    Maybe it's mistypes:

    IN your struct:
    struct person
    {
    char id; <------------ you need to write [4] or so...
    char name[40];
    int day, month, year;
    };

    if(strcmp(&HelpP->id, choice) == 0)

    maybe you should take away then &.....!!


    I can't say how to make the code more easier or more clever.
    It's up to an advanced person...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM