Filter and process data Download the file population.txt from webcourse.
The file is a formatted text file with 3 columns: country, city and population. Read the file and create a short c program to do the following:
a.display the name and the population of the city with the highest population
b. Display on the screen the sum of all the population of all the cities



     FILE *fp;
     char country[26];
     char cities;
     long num;
     long sum = 0;
     long current;
     long highest;

    fp = fopen("F:\\Programming with Persistent Data\\population.txt", "r");
    //fp_even = fopen("F:\\Programming with Persistent Data\\even_lines.txt", "w");
    //fp_odd = fopen("F:\\Programming with Persistent Data\\odd_lines.txt", "w");

    if (fp == NULL)
        printf("Cannot open file\n");
    while((fscanf(fp, "%s %s %ld",country, &cities, &num))!= EOF)
        printf("  %ld \n", num);
        current = num;
        printf(" \ncurrent is %ld\n", current);
        sum = sum + num;
        if(num > current)
           highest = num;
       printf(" \n highest is %ld\n", highest); 
    printf("\n The sum of all population of all cities is - %ld", sum);
    printf("\n\n The city with highest population - %ld", highest);



Program works, but it's display that highest is = 0; I can't find the error. What is wrong with my code?

On line 27, you make current and num the same. So the condition on line 30 will never be true, and line 32 will never happen.

Is the condition on line 30 the correct thing to be comparing? Shouldn't you be comparing something against highest?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.