Infinite "fgets" calls.

Hey guys, as the title suggests, I'm having problem with fgets and my program in general. Here is the question I'm trying to do;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Your task this week is to write a program to assist a biologist to tally their counts of migrating whales.
As pods of migrating whales swim past the biologist types the number and species of whales.

Here is an example of the input the biologist will provide

15 humpbacks
3 killer whales
2 sperm whales
19 belugas
2 humpbacks
2 sperm whales
7 killer whales
4 pygmy right whales
7 humpbacks

You will notice the biologists enters one line of input for each pod. Each line contains first the number of whales in the pod followed by the species of whales.

When the end of input is reached the program then must print a tally of how many of each species of whale were seen. For the above input, it should print:

24 humpbacks
10 killer whales
4 sperm whales
19 belugas
4 pygmy right whales

Notice, tallies of species are listed in the order they were first seen.

Write a C program, whales.c, which reads input in the above format and prints output as indicated. 


The thing is, even though I put a boundary on the loop for fgets my compiler scans forever. Also, I am concerned about the logic of my coding, I had a little trouble with it and will be happy if it's correct, thanks for your time and help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CHAR 130
#define NUM 128
#define SIZE 1000

int main(int argc, char *argv[]){

    int i;
    int j;
    int q=1;
    char string2[NUM][CHAR];
    char strings[10][SIZE];
    int counter = 0;
    int counter3 = 0;
    
    for(i=0;i<10;i++){
        fgets(strings[i], CHAR, stdin);
        counter3++;
    }
    
    for(i=0;i<counter3;i++){
        for(j=0;j<=counter3;j++){
            if(i==0){
                j=1;
            }
            if(strcmp(strings[i], strings[j])<2){
                strings[i][0]+=strings[j][0];
            }
        }
    }
    
    strcpy(string2[0], strings[0]);
    
    for(i=0;i<counter3;i++){
        for(j=i-1;j>=0;j--){
            if(strcmp(strings[i], strings[j])>1){
                counter++;
                if(counter==i){
                    strcpy(string2[q], strings[i]);
                     q++;
                }
            }
        }
        counter = 0;
    }
    
    for(i=0;i<=counter;i++){
        printf("%s", string2[i]);
    }
    
    return 0;
}
The thing is, even though I put a boundary on the loop for fgets my compiler scans forever.

Your loop for your fgets() should stop after you enter 10 strings. However I recommend something like:
1
2
   while(counter3 < 10 && (fgets(strings[counter3], CHAR, stdin) != NULL))
      ++counter3;


The above would allow you to stop the entry by signaling end of file by using CTRL-Z, or CTRL-D depending on what operating system you are using. It'll also stop after 10 entries.

Last edited on
I understand the logic and that you used and I will use this in the future. However, I am still getting the same error, here is the output of a test:

This is the input; > > 24 humpbacks
> 10 killer whales
> 4 sperm whales
> 19 belugas
> 4 pygmy right whales
My output:






Topic archived. No new replies allowed.