Thread: control reaches end of non void function in bool function

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    47

    control reaches end of non void function in bool function

    Code:
    bool check_if(informationT a[],int size){int i;
    char sur[20],name[15];
    printf("Give patient's last name \n");
    gets(sur);
    printf("Give patient's first name \n");
    gets(name);
    
    
    
    
    for (i=0; i<size; i++){
        if ((sur == a[i].epwnymo) && (name == a[i].onoma))
           return true;
        else
            return false;
    }
    }
    i want, if the name and surname is found in the array, i want to return that the patient is found, otherwise he is not found.
    i have tried break; too, but nothing changes.
    All kind of help is welcome
    Thanks

  2. #2
    Guest
    Guest
    I suspect the compiler isn't sure whether either return statement gets triggered. If size is 0 for instance, it won't (the program never enters the loop). You need to ensure that there's an unconditional return statement somewhere in your function.

    Since the loop terminates early via return when a match is found, and since all patients need to be checked otherwise, you should simply move the return false; out of the loop. Right now the logic isn't sound.

    Also, make sure you indent your code properly, for those you want help from, and for your own sanity.
    Code:
    bool check_if(informationT a[], int size)
    {
        int i;
        char sur[20], name[15];
        printf("Give patient's last name \n");
        gets(sur);
        printf("Give patient's first name \n");
        gets(name);
    
        for (i = 0; i < size; i++)
        {
            if ((sur == a[i].epwnymo) && (name == a[i].onoma))
                return true;
            else
                return false;
        }
    }

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,114
    More problems:

    1) We cannot see what the struct, "informationT" is. You have not provided a small program sufficient to allow us to see and test the program.

    2) DO NOT use gets()!!! Please use fgets() or other functions instead. See: Why gets() is bad / Buffer Overflows

    Code:
    if ((sur == a[i].epwnymo) && (name == a[i].onoma))
    3) Without seeing the details of the struct, it appears you are attempting to compare two strings with the '==' operator. You need to use the strcmp() function to compare strings.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    i have recently learned fgets, so i have to get familiar with it.
    here is the sturct.
    Code:
    typedef struct{    char epwnymo[20]; //surname
        char onoma[15]; //name
        char dieythinsi[30]; //adress
        int poso_plirwmis; //money he payed to hospital
    }informationT;
    just used strcmp too, i always get the answer as false.
    just the code a bit more
    here it is
    Code:
    bool check_if(informationT a[],int size){
    int i;
    char sur[20],name[15];
    bool x;
    printf("Give patient's last name \n");
    gets(sur);
    printf("Give patient's first name \n");
    gets(name);
    
    
    
    
    for (i=0; i<size; i++){
        if ((strcmp(sur,a[i].epwnymo)) && strcmp(name,a[i].onoma) ){
                  x = true;
            break;
        }
            else{
                 x = false;
                break;
            }
    
    
    }
    return x;
    }
    Code:
     
    bool check; // it's in the top of the program
    check = check_if(stoixeia,N);    if (check){
            printf("PATIENT FOUND");
        }
        else{
            printf("PATIENT NOT FOUND");
        }
    Last edited by Giwrgows x; 12-19-2015 at 10:16 AM.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,114
    In your code:
    Code:
    if ((strcmp(sur,a[i].epwnymo)) && strcmp(name,a[i].onoma) )
    From the output from `man strcmp`:
    The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
    I think the statement above is the opposite of what you want! Please run tests to confirm, as you still have not provided us with a complete program we can compile and test ourselves.

    If "sur" , and "a[i].epwnymo" are exactly the same, then strcmp() would return, "0", and the if statement would be false!

    EDIT: Also, each function should do one thing, and one thing only.

    For the function "bool check_if(informationT a[],int size)", I would expect that this only "Checks" some data, not Input data, and then compare that data. This should be split into at least two different functions.
    Last edited by rstanley; 12-19-2015 at 10:44 AM.

  6. #6
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Hi,
    your second problem lies in this part.
    Code:
    for (i=0; i<size; i++){
        if ((strcmp(sur,a[i].epwnymo)) && strcmp(name,a[i].onoma) ){
            x = true;
            break;
        }
        else{
            x = false;
            break;
        }
    }
    You check if the patient is found, but found or not found in both cases you use a break statement to leave the loop.

    Cheers
    Last edited by talahin; 12-19-2015 at 10:50 AM.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    Code:
    #include <stdio.h>#include "string.h"
    #include "simpio.h"
    #include "stdbool.h"
    #define N 3
    
    
    typedef struct{
        char epwnymo[20];
        char onoma[15];
        char dieythinsi[30];
        int poso_plirwmis;
    }informationT;
    
    
    void eisagwgi_stoixeiwn(informationT a[],int size);
    void print_katastasi(informationT a[],int size,char kliniki[]);
    informationT max(informationT a[],int size);
    void print_best_astheni(informationT a);
    int calc_sum(informationT a[],int size);
    void emfanisi_astheni(informationT asthenis);
    bool check_if(informationT a[],int size);
    
    
    int main(){
        informationT stoixeia[N],asthenis,patient;
        int i,eispraksi;
        char kliniki1[] = "Elpis";
        char kliniki2[] = "Galinos";
        bool check;
        eisagwgi_stoixeiwn(stoixeia,N);
        print_katastasi(stoixeia,4,kliniki1);
        print_katastasi(stoixeia,3,kliniki2);
        asthenis = max(stoixeia,N);
        print_best_astheni(asthenis);
        eispraksi = calc_sum(stoixeia,N);
        for (i=0; i<100; i++){ printf("-"); }
        printf("\n");
        printf("Sinoliki eispraksi %d\n",eispraksi);
        for (i=0; i<100; i++){ printf("-"); }
        for (i=0; i<2; i++){printf("\n");}
        patient = stoixeia[0];
        emfanisi_astheni(patient);
        printf("\n");
        check = check_if(stoixeia,N);
        if (check){
            printf("PATIENT FOUND");
        }
        else{
            printf("PATIENT NOT FOUND");
        }
    
    
    return 0;
    }
    
    
    void eisagwgi_stoixeiwn(informationT a[],int size){
        int i;
        for (i=0; i<size; i++){
            printf("dwse ta stoixeia tou astheni %d\n",i+1);
            printf("Dwse epwnymo ");
            gets(a[i].epwnymo);
            printf("Dwse onoma ");
            gets(a[i].onoma);
            printf("Dwse dieythinsi ");
            gets(a[i].dieythinsi);
            printf("Dwse poso plirwmis ");
            a[i].poso_plirwmis = GetInteger();
        }
    for (i=0;i<2;i++){printf("\n");}
    }
    
    
    void print_katastasi(informationT a[],int size,char kliniki[]){
         int i;
    printf("------ %s ------\n",kliniki);
    printf("%-5s %-9s %-11s %-16s \n","EPWNYMO","ONOMA","DIEYTHINSI","POSO" );
    for (i=0; i<100; i++){
            printf("-");
    }
    printf("\n");
    for (i=0; i<size; i++){
        printf("%-5s  %-9s  %-11s  %-14d \n",a[i].epwnymo,a[i].onoma,a[i].dieythinsi,a[i].poso_plirwmis);
    
    
    }
    for (i=0; i<2; i++){ printf("\n"); }
    
    
    }
    
    
    informationT max(informationT a[],int size){
        int i;
        informationT best_asthenis;
        strcpy( best_asthenis.dieythinsi,a[0].dieythinsi);
        strcpy(best_asthenis.epwnymo,a[0].epwnymo);
        strcpy(best_asthenis.onoma,a[0].onoma);
        best_asthenis.poso_plirwmis = a[0].poso_plirwmis;
        for (i=1; i<size; i++){
            if (best_asthenis.poso_plirwmis < a[i].poso_plirwmis)
            strcpy( best_asthenis.dieythinsi,a[i].dieythinsi);
            strcpy(best_asthenis.epwnymo,a[i].epwnymo);
            strcpy(best_asthenis.onoma,a[i].onoma);
            best_asthenis.poso_plirwmis = a[i].poso_plirwmis;
        }
    return best_asthenis;
    }
    
    
    void print_best_astheni(informationT a){
        int i;
        printf("Kalhteros asthenis \n");
        printf("Epwnymo   Onoma   Dieythinsi   Poso \n");
        for (i=0; i<100; i++){
                printf("-");
        }
        printf("\n");
        printf("%-7s %-20s %-10s %-5d \n",a.epwnymo,a.onoma,a.dieythinsi,a.poso_plirwmis);
        for (i=0; i<2; i++){ printf("\n");}
    
    
    }
    
    
    int calc_sum(informationT a[],int size){
        int sum,i;
        sum = 0;
        for (i=0; i<size; i++){
            sum += a[i].poso_plirwmis;
        }
    return sum;
    }
    
    
    void emfanisi_astheni(informationT asthenis){
        printf("%-7s %-20s %-10s %-5d \n",asthenis.epwnymo,asthenis.onoma,asthenis.dieythinsi,asthenis.poso_plirwmis);
    }
    
    
    bool check_if(informationT a[],int size){
    int i;
    char sur[20],name[15];
    bool x;
    printf("Give patient's last name \n");
    gets(sur);
    printf("Give patient's first name \n");
    gets(name);
    
    
    
    
    for (i=0; i<size; i++){
        if ((strcmp(sur,a[i].epwnymo)) && strcmp(name,a[i].onoma) ){
                  x = true;
            break;
        }
    
    
            else{
                 x = false;
                break;
            }
    
    
    
    
    }
    return x;
    here is the whole program
    since i'm doing an exercise for uni, it asks me to ask for information in the function, that's why i do it inside

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,114
    Please make the corrections that we have recommended, first, test your code THOROUGHLY, then come back to us if you still have problems.

    Also, please turn on, and/or turn up your compiler's warning levels to the highest settings, and correct any errors or warning that you see, before asking for additional help.

  9. #9
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    Code:
    bool check_if(informationT a[],int size){
    int i;
    char sur[20],name[15];
    bool x;
    printf("Give patient's last name \n");
    gets(sur);
    printf("Give patient's first name \n");
    gets(name);
    
    
    
    
    for (i=0; i<size; i++){
        if ( (strcmp(sur,a[i].epwnymo) == 0) && strcmp(name,a[i].onoma) == 0 ){
                  x = true;
    
    
        }
            else{
                 x = false;
    
    
            }
    }
    return x;
    }
    i did it, in case anyone else has any similar problem
    thanks everyone.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is good to hear, but as Guest noted in post #2, you really need to format your code properly, especially where indentation is concerned. For example:
    Code:
    bool check_if(informationT a[], int size) {
        int i;
        char sur[20], name[15];
        bool x;
        printf("Give patient's last name \n");
        gets(sur);
        printf("Give patient's first name \n");
        gets(name);
        for (i = 0; i < size; i++) {
            if ((strcmp(sur, a[i].epwnymo) == 0) && strcmp(name, a[i].onoma) == 0) {
                x = true;
            }
            else {
                x = false;
            }
        }
        return x;
    }
    rstanley made it clear in post #3 that you must not use the gets function. This function is inherently vulnerable to buffer overflow, and in fact it has been removed from the newest version of the C standard library.

    Next, it looks like you have made an oversight in your implementation of check_if: the same set of data could well result in check_if returning true under one ordering, and result in check_if returning false under another ordering. The reason is that your loop simply sets x to true or false, and then goes on to the next iteration.

    Typically, such a function would be designed to return true if and only if a condition holds true for all the elements, or it would be designed to return true if and only if a condition holds true for any element. For the former, a typical approach would be:
    Code:
    for element in container
        if condition is false for the element
            return false
    return true
    For the latter, a typical approach would be:
    Code:
    for element in container
        if condition is true for the element
            return true
    return false
    Of course, instead of returning within the loop, you could initialise a flag (in this case your variable named x) to the default return value, then set it within the loop and break out of the loop once it is set.

    Note that "check_if" is a very generic name. You should use a more descriptive name, e.g., "check_if_patient_exists". Likewise, while "i" as a loop/array index is fine, "x" is not a descriptive name. Even a relatively generic name like "flag" would be better, though it would be best if you could use a name related to the purpose of the function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    while i use "gets" the result is correct. if i use fgets the result is wrong.

    Code:
    bool check_if(informationT a[],int size){int i;
    char sur[20],name[15];
    bool flag;
    printf("Give patient's last name \n");
    fgets(sur,sizeof(sur),stdin);
    printf("Give patient's first name \n");
    fgets(name,sizeof(name),stdin);
    
    
    
    
    for (i=0; i<size; i++){
        if ( (strcmp(sur,a[i].epwnymo) == 0) && strcmp(name,a[i].onoma) == 0 ){
                  flag = true;
    
    
        }
            else{
                 flag= false;
    
    
            }
    }
    return flag;

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Did you remove the end of line character from the string when using fgets()? With the fgets() function the end of line character is considered a valid character and is "added" to the string if there is room for this character. You need to manually remove this character if it is not needed.

    Jim

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Giwrgows x
    while i use "gets" the result is correct. if i use fgets the result is wrong.
    fgets will store the newline from entering the line if there is space to store it, hence you should remove it, e.g.,
    Code:
    name[strcspn(name, "\n")] = '\0';
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    tried
    Code:
    name[strcspn(name, "\n")] = '\0';
    still wrong result
    tried this also, but the same

    Code:
    if (sur[strlen(sur) - 1] == '\n') {
        sur[strlen(sur) - 1] = '\0';
    }

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the current code of your function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-11-2014, 09:51 AM
  2. Replies: 3
    Last Post: 07-07-2013, 01:41 AM
  3. difference between void and static void function
    By mahaju in forum C++ Programming
    Replies: 7
    Last Post: 12-27-2011, 04:02 AM
  4. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  5. warning: control reaches end of non-void function
    By franziss in forum C Programming
    Replies: 5
    Last Post: 01-29-2005, 11:46 PM