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);
}
'''