2

I need to make a program that outputs all lines that contain a matching string "target" and sum up the number of matches along with the total cost,

The problem is that the for loop stops at the first iteration regardless of there being a match

I have tried using strstr() instead of strcmp() and it works, but since this is a school assignment, i cant use strstr()

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

struct data{
    
    char product[100];
    char name[100];
    int price;

};

int main(){

    FILE *f;
    
    f = fopen("Customers.txt", "r");
    int x;
    
    
    scanf("%d",&x);
    
    struct data arr[x];
    
    for(int i=0;i<x;i++){
        fscanf(f,"%[^,], %[^,], %d",arr[i].product,arr[i].name,&arr[i].price);
    }
    
    char target[100];
    int res;
    int count=0;
    int total=0;
    
    scanf("%s",target); 

    for(int j=0;j<x;j++){
        
        res=strcmp(arr[j].product,target);
        
        if(res==0){
            printf("%s, %s, %d",arr[j].product,arr[j].name,arr[j].price);
            count++;
            total = total + arr[j].price;
        }
        else{
            continue;
        }
    }
    printf("\nTotal Buyers: %d\n",count);
    printf("Total Amount: %d\n",total);

}

File:

Gem, Alice, 2000
Gold, Bob, 3000
Gem, Cooper, 2000

Input: 3 (No. of lines in the file) Gem (target)

Expectd output:

Alice 2000
Cooper 2000
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    Use your debugger or put `printf("<%s> <%s>\n", arr[j].product, target);` right before the line with `strcmp` and let us know what happens. – Jabberwocky Feb 03 '22 at 08:44
  • 2
    Welcome to SO. In every program where you deal with user input, your first step should be to verify what you got as input and check if it is what you expected. What do you enter as `target` and what is in your variable afterwards? Also what is variable content of in your array? Does that match? Without looking closer, I would expect you have some extra newlines or spaces in one of the strings. – Gerhardh Feb 03 '22 at 08:44
  • Beside your actual problem you might reconsider the use of a `continue` statement at the end of your loop – Odysseus Feb 03 '22 at 08:47
  • @Explosivegamer29 you didn't react to my first comment. – Jabberwocky Feb 03 '22 at 09:00
  • @Jabberwocky i tried your printf the result is Alice 2000 – Explosivegamer29 Feb 03 '22 at 09:02
  • @Explosivegamer29 you need to put that __into the question__. Anyway, I answered the question. – Jabberwocky Feb 03 '22 at 09:05
  • 1
    @Explosivegamer29 A minor point: the `else { continue; }` part is redundant because you don't do anything after it. – JeremyP Feb 03 '22 at 09:12

1 Answers1

3

The fscanf format string is wrong, it should be:

"%[^,], %[^,], %d\n"

Note the final \n. Without that, the \n (new line) will not be absorbed and the first item of the next string read will start with \n.

Or even better: use this format string:

" %[^,], %[^,], %d"

Note the space at the beginning. With that format string all leading and trailing whitespace, including the newline, will be absorbed.

Furthermore you absolutely need to check if fopen fails, in your case it apparently doesn't, but if the file cannot be opened for some reason, and you do not any checks, the subsequent operations on f wont end well.

So you need at least this:

...
f = fopen("Customers.txt", "r");
if (f == NULL)
{
  printf("Can't open file\n");
  return 1;
}
...
the busybee
  • 10,755
  • 3
  • 13
  • 30
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115