-1

After reading the material in the text file, if there is a corresponding name through the search, print out the name and money. However, I can only read the first line of the text file. If I want to make it read another line and find the corresponding name what can I do?

my text file has 15 lines.

Jane 50
Bruno 100
Kim 200
Young 150
Will 250
Jane 50
Bruno 100
Kim 200
Young 150
Will 250
Jane 50
Bruno 100
Kim 335
Young 455
Will 555
#include <stdio.h>
#include <string.h>
#include <stdlib.h>



typedef struct user{
    char name [10];
    int money;
} userinformation;



int main()
{
    userinformation myvar[100];
    int x=0;
    char find_name[100];
    int i = 0;
    int idx = 0;
    char buffer[1001],*token;
    char* ptr;
    userinformation* userinfo;
    int cents_50=0;
    int cents_20=0;
    int cents_10=0;
    int cents_5=0;
    
    FILE * srcFile = fopen("coins.txt", "r");
    
    
    if(srcFile == NULL)
    {
        printf("fail to open file");
        return -1;
    }
    
    //file read from coins.txt
    while (!feof(srcFile))
    {
        i =0;
        fgets(buffer, 1001, srcFile);
        token = strtok(buffer, " ");
        while (token != NULL)
        {
            if(i == 0)
            {
                strcpy(myvar[idx].name, token);;
            }
            else if (i == 1)
            {
                myvar[idx].money = atoi(token);
            }
            i++;
            token = strtok(NULL, " ");
        }
        idx++;
    }
    
    
    for(int i = 0; i <idx; i++)
    {
        printf("%s %d\n", myvar[i].name, myvar[i].money);
    }
    fclose(srcFile);
    
    
    
    while(x != 2)
    {
        printf("1.Enter name\n");
        printf("2.Exit\n");
        scanf("%d%*c", &x);
        
        if(x ==1)
        {
            printf("Name: ");
            scanf("%s%*c", find_name);
            for(i=0; i<idx; i++)
            {
                ptr = strstr(myvar->name, find_name);
            }
            if(ptr != NULL)
            {   
                printf("Customer: \n");
                printf("%s %d\n", myvar->name, myvar->money);
                printf("Change: \n");
                //calculate 50cents;
                if(myvar->money>50) 
                {
                     myvar->money/50;
                     cents_50++;
                     printf("50 cents : %d \n", cents_50);
                }
                //calculate 20cents;
                if((myvar->money/50)<50 && myvar->money >=20)
                {
                    if((myvar->money%50/20)>=1)
                    {
                        (myvar->money%50/20);
                        cents_20++;
                        printf("20 cents : %d \n", cents_20);
                    }
                }
                //calculate 10cents;
                if((myvar->money%50%20/10)<20 && myvar->money >=10)
                {
                    if((myvar->money%50%20/10)>=1)
                    {
                        (myvar->money%50%20/10);
                        cents_10++;
                        printf("10 cents : %d \n", cents_10);
                    }
                }
                if((myvar->money%50%20%10/5)<10 && myvar->money >=5)
                {
                    if((myvar->money%50%20%10/5)>=1)
                    {
                        (myvar->money%50%20%10/5);
                        cents_5++;
                        printf("5 cents : %d \n", cents_5);
                    }
                }
            }
            else if(ptr != myvar->name)
            {
                printf("Not founded\n");
            }
        }
        else if(x ==2)
            break;
    }
    
    
    return(0);
}

'''

YoungHun
  • 1
  • 1
  • 2
    Where did you get the assignment or exercise from? What is the *full* text of the assignment or exercise? Does it really tell you that you are only allowed to read one single line from the file? – Some programmer dude May 20 '21 at 07:44
  • It is a part of the programming assignment. Am I not allowed to ask this question? – YoungHun May 20 '21 at 07:46
  • 2
    Your question "What should I do?" is answered by "Ask the author of that assigment whether you really can only read the first line and then are expected to print information from potentially a different line." Because at leat two users here think it is simply impossible. – Yunnosch May 20 '21 at 07:48
  • It's my first time using StackOverflow. I think the code I made can't read the text on the second or another line. For example, the name Ray exists, but if I search for Ray, Not found is output. – YoungHun May 20 '21 at 07:49
  • Is "you can only read the first line of the text file" part of the assignment? Or do you instead mean "But the code I wrote fails to read anything but the first line, how can I fix that?". – Yunnosch May 20 '21 at 07:51
  • 2
    To begin, please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Weather Vane May 20 '21 at 07:52
  • 2
    Please cleanly separet the assignment (which you might want to format as a quote, see https://stackoverflow.com/editing-help ) from the question you ask here. "I can only read the first line of the text file." still can be misread as part of the assigment. – Yunnosch May 20 '21 at 07:52
  • The code I wrote fails to read another lane. – YoungHun May 20 '21 at 07:53
  • 1
    ...the loop control should be `while(fgets(buffer, sizeof buffer, srcFile) != NULL)` note that I also removed the hard-coded repetiton of the buffer size. Then *print every line* read otherwise you have no way of knowing it "only reads the first line" unless you deploy a debugger. – Weather Vane May 20 '21 at 07:53
  • 1
    You can ask the question, that's okay. But please show us the original assignment text, copy-pasted in full and complete. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please don't forget the *minimal* part of your [mcve]. – Some programmer dude May 20 '21 at 10:38

1 Answers1

0

This seems wrong:

        for(i=0; i<idx; i++)
        {
            ptr = strstr(myvar->name, find_name);
        }

myvar is an array but you don't use an array index. In other words - you always use the first element of the array (i.e. myvar[0].name).

Besides that, I would expect that the loop should stop when strstr return a non-NULL value.

Did you intend to do:

        for(i=0; i<idx; i++)
        {
            ptr = strstr(myvar[i].name, find_name);
            if (ptr != NULL) break;
        }

(btw - do you really want to use strstr? I would expect a string compare using strcmp ).

The same problem, i.e. missing array index, is present in all the code that follows the above code.

BTW: The way you read from the file isn't 100% correct. See Why is “while ( !feof (file) )” always wrong?

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63