1
typedef struct{

    int moviesRented;
    char title[20][20];

} Movie;

typedef struct{
    int accNumber;
    char name[20];
    Movie movie_rental;
} Customer;


int main(){
    int i;
    int j;
    int customerRecords;
    Customer *pCustomer;


    printf("Enter amount of customer records to be kept: ");
    scanf("%d", &customerRecords);

    pCustomer = malloc(customerRecords * sizeof(Customer));

//This will begin asking for input relating to the customer

    for(i = 0; i < customerRecords; ++i){
        printf("Enter account number, name, and movies rented: \n");
        scanf("%d\n %s\n %d", &(pCustomer + i)->accNumber, &(pCustomer +i)->name, &(pCustomer + i)->movie_rental.moviesRented);

//for loop below is asking for multiple movie titles depending on how many movies have been rented

        for( j = 0; j < (pCustomer+i)->movie_rental.moviesRented; ++j){ 

        //asking for input of movie titles and trying to add into string array

            printf("Enter Movie titles: \n");
            scanf("%s", &(pCustomer+i)->movie_rental.title[j]);

        }

    }
        printf("Displaying information: \n");

    for(i = 0; i < customerRecords; ++i){
        printf("Name: %s\nAcc. Number: %d\nNo. Movies Rented: %d\n",(pCustomer+i)->name, (pCustomer+i)->accNumber, (pCustomer+i)->movie_rental.moviesRented);

//for loop below does not display correctly. Only displays last entry in first iteration

            for(j = 0; j < (pCustomer+i)->movie_rental.moviesRented; j++){ 
               printf("Movies rented: %s\n", (pCustomer+i)->movie_rental.title[j]);
            }
        return 0;
    }
avaz5695
  • 29
  • 3
  • 1
    You really need to learn about array notation for pointers. This `&(ptr+i)` stuff is really jarring. Instead, consider `ptr[i]`. Also `char name[4][5]` allocates 4 arrays of 5 characters, not a 4 and 5 character array. Try compiling this with `-Wall` because there's a whole lot of type conflicts in here. – tadman Feb 05 '19 at 16:27
  • appreciate the comment. This is the way my professor had done it in a pdf but I will try to replace them. Should I be deleting the ampersands as well if I put ptr[i]? – avaz5695 Feb 05 '19 at 16:30
  • I also don't think you need a pointer for that. you can do this in a very simpler manner without using pointer. – roottraveller Feb 05 '19 at 16:32
  • 1
    Unfortunately it sounds like your professor is teaching you an arcane C style that nobody has used in decades. The ampersands are part of the problem, as is meaningless names like `ptr`. Try splitting this code up into smaller, more self-contained sections, and tackle each problem in turn. Right now this is just a sprawling mess of highly interconnected code. Remember indentation is extremely important for readability and understanding. Clean code makes bugs more obvious. – tadman Feb 05 '19 at 16:32
  • 2
    It's also worth mentioning that you [shouldn't cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc), and that this is a very common misconception. As roottraveller points out dynamic allocation isn't necessary here, so you can simplify this code dramatically. – tadman Feb 05 '19 at 16:33
  • What exactly is it that you are trying to do here? Your question sounds a bit ambiguous to me. – ats Feb 05 '19 at 16:40
  • I have tried switching it to movie_rental.title[j] but when I display it in the loop below it only displays the second entry from the input and nothing in the next print statement: – avaz5695 Feb 05 '19 at 16:48
  • OT: regarding the `typedef struct {` definitions. Most debuggers use the struct 'tag' name to access the individual fields in the struct. However, in the posted code, the 'tag' name is missing – user3629249 Feb 06 '19 at 03:14
  • the posted code does not compile! Amongst other things, it is missing the needed `#include` statements for the needed header files. Are you expecting us to guess as to which header files you actually used? – user3629249 Feb 06 '19 at 03:18
  • when asking about a run time problem, as this question is doing, please post a [mcve] so we can reproduce the problem and help you to debug it. – user3629249 Feb 06 '19 at 03:19
  • OT: regarding: `scanf("%s", &(pCustomer+i)->movie_rental.title[j]);` 1) when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. Note: in the above statement, any value other than 1 being returned is an error. 2) when using the input format specifiers '%s' and/or '%[...]' always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer because those specifiers always append a NUL byte to the input. This also avoids any undefined behavior from buf overflow – user3629249 Feb 06 '19 at 03:24
  • regarding: `for(j = 0; j < (pCustomer+i)->movie_rental.moviesRented; j++){` the index variable is 'j', but the stop condition is using index variable 'i'. Did you actually mean to use 'i' or should it be 'j'? – user3629249 Feb 06 '19 at 03:27

1 Answers1

1

The problem is with your indexing in movie_rental_title.

scanf("%s", &(ptr+i)->movie_rental.title[i]);

This line overwrites the movie name every time no matter how many movies for each customer. What you want is movie_rental.title[j] because i will never change for the duration of the loop.

In the display you also want to change movie_rental.title[i] to movie_rental.title[j]

Also try to keep variable names as descriptive as possible so you can avoid hard to detect errors like this.

I Funball
  • 380
  • 1
  • 8