0

I have a problem in this function(int populationTotal(villes ville[], int n, char nom[])), I've created a structure of city have a name and number of poeple and name of his country, and I want from the user to gives me a name of country and I'll give him the total of the poeple in this country.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ville
{
    char nom[50];
    int population;
    char pays[30];
}villes;

void chargement(villes villes[], int n)
{
    int i;
    for(i=0; i<n; i++)
    {
        printf("Entrez le nom de la ville n° %d: \n", i+1);
        gets(villes[i].nom);
        printf("Entrez la population de la ville n° %d:\n", i+1);
        scanf("%d", &villes[i].population);
        getchar();
        printf("Entrez le pays de la ville n° %d:\n", i+1);
        gets(villes[i].pays);
    }
}

int populationTotal(villes ville[], int n, char nom[])
{
    int Total=0, i;
    for(i=0; i<n; i++)
    {
        if(strcmp(ville[i].pays, nom))
            Total += ville[i].population;
    }
    return Total;
}
int main()
{
    villes ville[50];
    int n;
    char pays[30];
    printf("Entrez le nombre de villes: \n");
    scanf("%d", &n);
    getchar();

    if( n < 1 || n > 50)
        printf("Le nombre doit etre...");
    else
    {
        chargement(ville, n);
        printf("Entrez le pays: \n");
        gets(pays);
        printf("La population total est: %d", populationTotal(ville, n, pays));

    }
}

enter image description here

  • 2
    By the way, you shouldn't use `gets()`, which has unavoidable risk of buffer overrun, deprecated in C99 and removed from C11. – MikeCAT Mar 03 '19 at 15:03
  • The function not always return 0. Example: https://wandbox.org/permlink/ieNv4lQYdSUkRbod – MikeCAT Mar 03 '19 at 15:05
  • 1
    Note that `strcmp()` returns `0` for equality, so you can't use it as if the return value is `true` for a match. See `if(strcmp(ville[i].pays, nom)) Total += ville[i].population;` – Weather Vane Mar 03 '19 at 15:08
  • add some values of poeple in each city and try to get the total after –  Mar 03 '19 at 15:09
  • i used strcmp just for comparing two strings i din't used for get the number of poeple i used ville[].population –  Mar 03 '19 at 15:10
  • My point stands - you are using `strcmp` as if a non-0 return value means a match, but it means they don't match. – Weather Vane Mar 03 '19 at 15:13
  • Did you really want `if(strcmp(ville[i].pays, nom) == 0)` ? – Support Ukraine Mar 03 '19 at 15:14
  • i want just to see if the name that has the user enters it equal any name of country that there are in my array –  Mar 03 '19 at 15:15
  • https://i.stack.imgur.com/iALkB.png –  Mar 03 '19 at 15:20
  • 1
    In that case please read the documentation for `strcmp` because the error has been pointed out several times. – Weather Vane Mar 03 '19 at 15:23
  • Also in `scanf("%d", &villes[i].population);` check the return value from `scanf` to make sure it has actually read the population count. – Weather Vane Mar 03 '19 at 15:25

1 Answers1

1

You're not checking the pays string correctly:

if(strcmp(ville[i].pays, nom))

The strcmp function returns 0 if the two strings match and non-zero if they don't match. Since a conditional is considered true if it evaluates to non-zero, the if portion is only entered when pays does not match each ville[i].pays. And because you entered the same pays string for each village as well as the separate pays string, they all match so the if condition is never entered.

If you entered a different pays for one village then the if would be entered for that one and you would get a non-zero return value from the function.

You need to compare the result of strcmp with 0 to see if the strings match.

if(strcmp(ville[i].pays, nom) == 0)

Also, never use gets but instead use fgets.

dbush
  • 205,898
  • 23
  • 218
  • 273