Strukturen-Buchliste



  • Hallo 🙂

    Ich habe ein Programm zum Erstellen einer Buchliste erstellt und würde diese Liste gerne auf verschiedene Art und Weise ordnen.
    Dazu habe ich ein Menü mit einem switch() command erstellt und das Ordnen läuft über Pointer auf verschiedene Buchtitel. Ich wollte dasselbe auch für die Preise machen, jedoch gibt der Compiler eine Fehlermeldung, wenn ich versuche die Pointerreihe den verschiedenen floatwerten zuzuweisen.
    Also Option 1 und 2 funktionieren bei Option 3 gibts ne Fehlermeldung und ich verstehe nicht warum.

    Vielleicht könnt ihr mir ja helfen 😉

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    #define MAXTITL   40
    
    #define MAXAUTL   40
    
    #define MAXBKS   100             
    
    char *ptstr[MAXTITL]; 
    float *ptstrn[MAXTITL]; 
    
    struct book {                     
    
        char title[MAXTITL];
    
        char author[MAXAUTL];
    
        float value;
    
    };
    void stsrtn(float *strings[], int num);
    void stsrt(char *strings[], int num);
    
    int main(void)
    
    {
        int choice;
    
        struct book library[MAXBKS]; 
    
        int count = 0;
    
        int index;
    
        printf("Please enter the book title.\n");
    
        printf("Press [enter] at the start of a line to stop.\n");
    
        while (count < MAXBKS && gets(library[count].title) != NULL
    
                              && library[count].title[0] != '\0')
    
        {
            ptstr[count] = library[count].title; 
    
            printf("Now enter the author.\n");
    
            gets(library[count].author);
    
            printf("Now enter the value.\n");
    
            scanf("%lf", &library[count].value);
            ptstrn[count] = library[count].value; // hier gibt der Compiler einen  Fehler aus
    
            count++;
    
            while (getchar() != '\n')
    
                continue;          
    
            if (count < MAXBKS)
    
            printf("Enter the next title.\n");
    
        }
    
        if (count > 0)
    
        {
    
            puts("How do you want your books sorted?");
            puts("Please choose one of the options below");  
            printf("1)Input ordered               2) Alphabetized\n3)Increased value\n");
    
            while (scanf("%d", &choice) == 1 && choice > 0 && choice < 4)
            {
            switch(choice)
    
                {
    
                    case 1    :
    
            for (index = 0; index < count; index++)
    
            printf("%s by %s: $%.2f\n", library[index].title,
    
                library[index].author, library[index].value);
    
                                  break;
    
                    case 2 : stsrt(ptstr, count);
    
            for (index = 0; index < count; index++)
            puts(ptstr[index]);
    
                                  break;
    
                    case 3 : 
    
                    stsrtn (ptstrn, count); 
    
                    for (index = 0; index < count; index++)
                    printf("%lf", ptstrn[index]);
    
                                  break;
                }
    
    puts("Please choose a number or press enter to quit");
    puts("Please choose one of the options below");  
    printf("1)Input ordered               2) Alphabetized\n3)Increased value\n");
                }
    
    }
    
        else
    
              printf("No books? Too bad.\n");
    
        getch();
        return 0;
    
    }
    
    void stsrt(char *strings[], int num)
    
    {
    
        char *temp;
    
        int top, seek;
    
        for (top = 0; top < num-1; top++)
    
            for (seek = top + 1; seek < num; seek++)
    
                if (strcmp(strings[top],strings[seek]) > 0)
    
                {
    
                    temp = strings[top];
    
                    strings[top] = strings[seek];
    
                    strings[seek] = temp;
    
                }
    
    }
    
    void stsrtn(float *strings[], int num)
    
    {
    
        float *temp;
    
        int top, seek;
    
        for (top = 0; top < num-1; top++)
    
            for (seek = top + 1; seek < num; seek++)
    
                if (strings[top] > strings[seek])
    
                {
    
                    temp = strings[top];
    
                    strings[top] = strings[seek];
    
                    strings[seek] = temp;
    
                }
    
    }
    

    Many thanks in advance 😉



  • Kennst du in C // bzw. /* */ ?
    Wenn Ja, dann nutze sie.
    Wenn Nein, lies ein C Buch.

    Wozu ist ptstrn da?
    Was wilst du mit einem Array of Pointer to float?

    Was gibst du bei printf("%lf", ptstrn[index]); aus, wenn ptstrn ein Array of Pointer ist?

    Was soll das "%lf"? Das ist nicht definiert, wird aber meist falsch benutzt für double-Werte.



  • Dein Code lässt (neben vielen programmiertechnischen Fehlern) auf einen Denkfehler schließen.
    Deine Nutzdaten sind ein Array von Strukturen. Wenn du die nach einem Strukturelement sortieren willst, kannst du das machen, musst aber natürlich dann den ganzen jeweiligen Strukturinhalt bewegen und nicht nur das Element, nach dem du gerade sortierst.


Anmelden zum Antworten