Thread: After while the method it's not returning anything

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    4

    After while the method it's not returning anything

    Hello,guys

    I'm new on this forum...sorry for any misstake.

    I have a linked list where i add cars that have name and speed.

    After that, i want to get the number of cars from the linked list, so i have a method that goes throw every element with a while, adds 1 to a integer(count++) and than returns the integer.

    The problem is that the integer it's not returned and i don't know why.
    This is the method:
    Code:
    int GetNumberOfElements(RacingCar *start){
        RacingCar *currentCar = start;
        int count = 0;
        while(currentCar != NULL){
            currentCar = currentCar->next;
            count++;
        }
        return count;
    }
    In main:
    Code:
    int test = GetNumberOfElements(start);
                printf("%d",test);
    Well...nothing it's printed after this.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,861
    When you say "nothing is printed" do you mean that the program reaches that function call and then never terminates, or do you mean that the program reaches that function call, then somehow does not go on to the printf statement despite terminating?

    For the former, perhaps you made a mistake in earlier code such that your linked list somehow became a circular linked list when it wasn't supposed to be one. For the latter, perhaps you are mistaken: perhaps the function wasn't even called. Use a debugger or add a printf statement before the function call to find out.
    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

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    4
    I mean that the method it's not returning an integer.
    It goes on method and while(i used a printf statement)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,861
    Quote Originally Posted by IoanAndrei
    I mean that the method it's not returning an integer.
    It goes on method and while(i used a printf statement)
    That is impossible except in the face of undefined behaviour. From what I see, both your implementation and use of GetNumberOfElements is correct, so the mistake lies elsewhere. I suggest that you post the smallest and simplest compilable program that demonstrates the error.

    EDIT:
    That said, before you do that, perhaps you should change your printf statement to:
    Code:
    printf("\nThis is the value of test=%d\n", test);
    For all we know you merely missed the number printed because it appeared on the end of the previous line or appeared at the start of the next line.
    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

  5. #5
    Registered User
    Join Date
    Jun 2018
    Posts
    4
    This is the code


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct S_RacingCar{
        char name[8];
        int speed;
        struct S_RacingCar *next;
        struct S_RacingCar *prev;
    } RacingCar;
    
    //The method with problems
    int GetNumberOfElements(RacingCar *start){
        RacingCar *currentCar = start;
        int count = 0;
        while(currentCar != NULL){
            count++;
            currentCar = currentCar->next;
        }
        return count;
    }
    
    //Adding the car
    RacingCar *AddCar(RacingCar *previous){
        printf("Enter name and speed: ");
        char input[16];
        fgets(input,15,stdin);
    
        RacingCar *newCar = malloc(sizeof(RacingCar));
        sscanf(input,"%s %d",newCar->name, &newCar->speed);
        printf("Added: %s Speed: %d\n\n",newCar->name,newCar->speed);
        newCar->next = NULL;
        newCar->prev = NULL;
        if(previous != NULL){
            previous->next = newCar;
            newCar->prev = previous;
        }
        return newCar;
    }
    
    int main()
    {
        char command[16];
        char input[16];
        char name[16];
        RacingCar *start = NULL;
        RacingCar *newest = NULL;
    
        while(fgets(input,15,stdin)){
            sscanf(input,"%s",command);
    
            if(strncmp(command,"quit",4) == 0){
                printf("\n\nBreaking...\n\n");
                break;
            }else if(strncmp(command,"print",5) == 0){
                PrintList(start);
            }else if(strncmp(command,"add",3) == 0){
                if(start == NULL){
                    start = AddCar(NULL);
                    newest = start;
                }else{
                    newest = AddCar(newest);
                }
    //where i call the problem method
            }else if(strncmp(command,"test",4) == 0){
                int test = GetNumberOfElements(start);
                printf("%d",test);
            }
        }
        return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,861
    It works for me:
    Code:
    add
    Enter name and speed: car1 20
    Added: car1 Speed: 20
    
    test
    1quit
    
    
    Breaking...
    Notice the number that I wrote in boldface.
    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

  7. #7
    Registered User
    Join Date
    Jun 2018
    Posts
    4
    Ok,that's strange...Let me check my code one more time -.-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning pointer to an object from a method
    By High Voltage in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2017, 04:19 PM
  2. Method returning NULL fails?
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2010, 11:34 AM
  3. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  4. Replies: 5
    Last Post: 06-30-2008, 02:48 PM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM

Tags for this Thread