2016-07-17 6 views
1

Ich bin neu in C und ich entwickle dieses grundlegende Projekt, während ich es studiere, um mir zu helfen Dinge zu fixieren ... also bitte, ertragen Sie mit mir, wenn mein Code immer noch nett aussieht dumm.Harte Zeit Drucken Strukturelemente mit einer Funktion

Das gesagt, habe ich Probleme mit einer Funktion zum Drucken von Strukturelementen. Ich habe eine Funktion zum Registrieren von Buchdetails und eine separate zum Drucken der Details erstellt.

Wenn ich die Details innerhalb der RegisterBook-Funktion drucke, werden sie korrekt gedruckt.

Wenn ich jedoch die Methode printBook aufrufen, ist alles, was ich bekomme, "Müll". Und es ist immer die gleichen Zeichen,

Der Code ist wie folgt:


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

struct Books { 
    char title[30]; 
    char author[20]; 
    int book_id[10]; 
    char subject[50]; 
} Books; 

int main() { 

    struct Books Book1; 
    struct Books Book2; 

    registerBook(Book1); 
    printBook(Book1); 
    registerBook(Book2); 
    printBook(Book2); 

    int exit = 0; 
    while(exit == 0) { 
     scanf("%p", exit); 
    } 

    return 0; 
} 

void printBook(struct Books a){ 
    printf("\nTitle: %s", a.title); 
    printf("\nAuthor: %s", a.author); 
    printf("\nISBN: %d", a.book_id); 
    printf("\nSubject: %s", a.subject); 
} 

void registerBook(struct Books a){ 
    printf("\nTitle?"); 
    scanf("%s", &a.title); 
    printf("\nAuthor?"); 
    scanf("%s", &a.author); 
    printf("\nISBN?"); 
    scanf("%d", &a.book_id); 
    printf("\nSubject?"); 
    scanf("%s", &a.subject); 
} 

Alles, was ich bekommen ist:


Titel eines

Autor zwei

ISBN 3

Betreff vier

Titel:? Ç Autor: `ISBN: 6.356.340 Betreff:? Ç @ Titel fünf

Autor sechs

ISBN? 7

Betreff acht

Titel: & Ý = w¬8wÝ = wÃÊpï Autor: ISBN: 6.356.340 Betreff:


Könnte jemand bitte raten?

+0

Entfernen Sie die & s aus den scanf-Funktionen in registerBook. Die Funktion erwartet ein char *, das denselben Wert wie das Array hat. – Alden

+0

Hey, danke für die schnelle Antwort, Alden. Leider bleibt es für printBook() gleich. – wailuanalu

+0

Danke, ich habe es versucht, aber ich bekomme immer noch die gleichen Zeichen wie die Ausgabe. Wenn ich a.title zum Beispiel innerhalb von registerBook(); drucke, wird es korrekt gedruckt. Aber wenn ich versuche, in main printf zu drucken, gibt es genau den gleichen "Müll" wie printBook() ;. – wailuanalu

Antwort

0

In der RegisterBook-Funktion sollten Sie die Parameter als Referenz und nicht als Wert übergeben, damit Sie die Änderungen nach dem Ende der Funktion beibehalten können.

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

struct Books { 
    char title[30]; 
    char author[20]; 
    int book_id; 
    char subject[50]; 
} Books; 

void printBook(struct Books a){ 
    printf("\nTitle: %s", a.title); 
    printf("\nAuthor: %s", a.author); 
    printf("\nISBN: %d", a.book_id); 
    printf("\nSubject: %s", a.subject); 
} 

void registerBook(struct Books* a){ 
    printf("\nTitle?"); 
    scanf(" %s", a->title); 
    printf("\nAuthor?"); 
    scanf(" %s", a->author); 
    printf("\nISBN?"); 
    scanf(" %d", &a->book_id); 
    printf("\nSubject?"); 
    scanf(" %s", a->subject); 
} 



int main() { 

    struct Books Book1; 
    struct Books Book2; 

    registerBook(&Book1); 
    printBook(Book1); 
    registerBook(&Book2); 
    printBook(Book2); 


    return 0; 
} 

Ich habe Ihre Exit-Schleife nicht aufgenommen, da es nichts mit Ihrem Problem hier zu tun hat.

+0

Danke, George Bou! Das hat das Problem behoben! Jetzt verstehe ich, was ich falsch gemacht habe. :) Das Einzige, was immer noch falsch ist, ist die book_id, die immer die gleiche Nummer zurückgibt, egal was ich schreibe. – wailuanalu

+0

Oh, book_id kann kein Array sein = X mein Fehler. Danke noch einmal ! – wailuanalu

+0

Sie sind herzlich willkommen! :) –

Verwandte Themen