简体   繁体   中英

Not displaying all data in C using struct

So I have this simple program in C that I am working on to help me understand structures. There is nothing too complicated about it. However when it gets to the point where it should display the data, it only displays part of the data. The program asks a user for a first and last name, then an amount. Then it should display the first and last name, as well as the amount. It does not display the last name. I am sure this is probably something simple, but I am not sure what I am missing here.

Here is the code:

    #include <stdio.h>
    #include <stdlib.h>

    #define NAMESIZE 30 

    struct data{
         float amount;
         char firstName[NAMESIZE];
         char lastName [NAMESIZE];
    }record;

    int main()
    {
         printf("\nEnter the donor's first and last names \n");
         printf("Separate names by a space: ");
         scanf("%s, %s", record.firstName, record.lastName);
 
         char c;
         while ( (c = getchar()) != '\n' && c != EOF )
              {
         }
 
         // At this point the program does not work correctly
         // It will just print the first name not the last name
         printf("\nEnter the donation amount: ");
         scanf("%f", &record.amount);

         // Display the information
         printf("\nDonor %s %s gave $%.2f \n", record.firstName, record.lastName, record.amount);

    return 0;
     }

Any suggestions on this would be greatly appreciated. Thank you

Once I got rid of the extra comma in the first scanf call, it worked. Here is the line that got corrected:

             scanf("%s %s", record.firstName, record.lastName);

I had a comma between the two %s, and that was incorrect.

Or maybe can use fgets that have buffer overflow protected and use strtok to split the spacing

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

    #define NAMESIZE 30 

    struct data{
         float amount;
         char firstName[NAMESIZE];
         char lastName [NAMESIZE];
    }record;

    int main()
    {
        char *name = malloc(NAMESIZE);
        if (name == NULL) {
        printf("No memory\n");
        return 1;
        }

         printf("\nEnter the donor's first and last names \n");
         printf("Separate names by a space: ");
         //scanf("%s, %s", record.firstName, record.lastName);
         fgets(name, NAMESIZE, stdin);

        if ((strlen(name) > 0) && (name[strlen (name) - 1] == '\n'))
            name[strlen (name) - 1] = '\0';

        //split name
    int init_size = strlen(name);
    char delim[] = " ";

    char *ptr = strtok(name, delim);
    int idx = 0;
    while(ptr != NULL)
    {
        printf("%d '%s'\n",idx, ptr);
                if(idx == 0){
          strcpy(record.firstName, ptr);
                }
                else{
                  strcpy(record.lastName, ptr);
        }
        ptr = strtok(NULL, delim);
        idx += 1;
    }

     /*
         char c;
         while ( (c = getchar()) != '\n' && c != EOF )
              {
         }
         */
         // At this point the program does not work correctly
         // It will just print the first name not the last name
         printf("\nEnter the donation amount: ");
         scanf("%f", &record.amount);

         // Display the information
         printf("\nDonor %s %s gave $%.2f \n", record.firstName, record.lastName, record.amount);

    free(name);
    return 0;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM