Thread: Structures on functions

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,041
    Quote Originally Posted by Kernelpanic
    Thanks for the tip. But it work once only, if twice and more then it comes to an error.
    Don't just say "it comes to an error". Describe the error. Clearly it isn't a compile error since it "work once", but what does it look like? For example, what is your test input, expected output, and actual output?

    Quote Originally Posted by Kernelpanic
    The code for that:
    You have some code that appears to be commented out... don't do that. It is perfectly fine when testing, but when showing your code to others, show your actual code, not commented out code because it is impossible for people to be certain if you uncommented that code or not when testing, so it is reasonable to assume that you in fact compiled the code as-is and so whatever errors you're talking about has to with the code as-is. Consequently, your claim that "But it work once only, if twice and more then it comes to an error." is unsubstantiated because you only used scanf with "%49[^\n]" once in your code, i.e., the situation in which you claim to have found an unspecified error never happened, so your claim must be false.
    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

  2. #17
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    34
    Quote Originally Posted by laserlight View Post
    Don't just say "it comes to an error". Describe the error. Clearly it isn't a compile error since it "work once", but what does it look like? For example, what is your test input, expected output, and actual output?
    . . .
    Consequently, your claim that "But it work once only, if twice and more then it comes to an error." is unsubstantiated because you only used scanf with "%49[^\n]" once in your code, i.e., the situation in which you claim to have found an unspecified error never happened, so your claim must be false.
    Right, it is not a compile error it are output errors.
    Two "%49":
    Code:
    //Daten einlesen - Input data
        printf("\nFirma    : ");
        scanf("%49[^\n]", wagen1.firma);
        printf("Modell   : ");
        scanf("%49[^\n]", wagen1.modell);
        
        printf("Anzahl PS: ");
        scanf(" %f", &wagen1.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen1.preis);
        fflush(stdin);
                
        printf("\nFirma    : ");
        fgets(wagen2.firma, sizeof(wagen2.firma), stdin);
        printf("Modell   : ");
        fgets(wagen2.modell, sizeof(wagen2.modell), stdin);
        
        printf("Anzahl PS: ");
        scanf(" %f", &wagen2.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen2.preis);
        fflush(stdin);
    Output:
    Structures on functions-auto-test1_2sc-jpg

    All "%49":
    Code:
    //Daten einlesen - Input data
        printf("\nFirma    : ");
        scanf("%49[^\n]", wagen1.firma);
        printf("Modell   : ");
        scanf("%49[^\n]", wagen1.modell);
        
        printf("Anzahl PS: ");
        scanf(" %f", &wagen1.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen1.preis);
        fflush(stdin);
                
        printf("\nFirma    : ");
        scanf("%49[^\n]", wagen2.firma);
        printf("Modell   : ");
        scanf("%49[^\n]", wagen2.modell);
            
        printf("Anzahl PS: ");
        scanf(" %f", &wagen2.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen2.preis);
        fflush(stdin);
    Output:
    Structures on functions-auto-test1_4sc-jpg

    Modell and Anzahl in one line. No input for Modell.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,327
    Like all the scanf modes, the %[ leaves the new-line in the buffer; unlike many of the scanf modes, %[ will not skip over that whitespace when it is used again (%f is fine, for instance, because it will explicitly throw away any new-lines or other white space before the number). So what I should have recommended was
    Code:
    scanf(" %49[^\n]", wagen1.modell);
    because the explicit space in the format string will then match any (or none) of that extra guff that's already there.

  4. #19
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    34
    Quote Originally Posted by tabstop View Post
    Like all the scanf modes, the %[ leaves the new-line in the buffer; unlike many of the scanf modes, %[ will not skip over that whitespace when it is used again (%f is fine, for instance, because it will explicitly throw away any new-lines or other white space before the number). So what I should have recommended was
    Code:
    scanf(" %49[^\n]", wagen1.modell);
    because the explicit space in the format string will then match any (or none) of that extra guff that's already there.
    That was it! I had already seen that, but did something wrong. Now it is going right. Thanks!
    I have it tested with three cars - I hope the comparisons are correct.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 50
    
    //New data type: AUTO
    typedef struct
    {
        char firma[MAX];
        char modell[MAX];
        float ps;
        float preis;
    }AUTO;
    
    //Structures as an argument of a function
    int vergleich(AUTO *zgr_wagen1, AUTO *zgr_wagen2, AUTO *zgr_wagen3);
    
    int main(void)
    {
        //Structur variables
        AUTO wagen1, wagen2, wagen3;
        
        //Pointers for an access of structures
        AUTO *zgr_w1, *zgr_w2, *zgr_w3;
        
        //Allacation of the adresses of both structures
        //Zuweisung der Adressen der beiden Strukturen
        zgr_w1 = &wagen1;
        zgr_w2 = &wagen2;
        zgr_w3 = &wagen3;
        
        int vergleich_ergebnis;
        
        //Daten einlesen - Input data
        printf("\nFirma    : ");
        scanf(" %49[^\n]", wagen1.firma);
        printf("Modell   : ");
        scanf(" %49[^\n]", wagen1.modell);
        
        printf("Anzahl PS: ");
        scanf(" %f", &wagen1.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen1.preis);
        fflush(stdin);
                
        printf("\nFirma    : ");
        scanf(" %49[^\n]", wagen2.firma);
        printf("Modell   : ");
        scanf(" %49[^\n]", wagen2.modell);
            
        printf("Anzahl PS: ");
        scanf(" %f", &wagen2.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen2.preis);
        fflush(stdin);    
        
        printf("\nFirma    : ");
        scanf(" %49[^\n]", wagen3.firma);
        printf("Modell   : ");
        scanf(" %49[^\n]", wagen3.modell);
            
        printf("Anzahl PS: ");
        scanf(" %f", &wagen3.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen3.preis);
        fflush(stdin);    
            
        vergleich_ergebnis = vergleich(zgr_w1, zgr_w2, zgr_w3);
        
        printf("\nVergleich von Kaufpreis zu PS\n\n");
        if(vergleich_ergebnis == 0)
            { printf("Das Verhaeltnis von Kaufpreis zur Anzahl PS ist gleich.\n"); }
        else if(vergleich_ergebnis == 1)
            { printf("Der %s hat das bessere Preis- Leistungsverhaeltnis.\n", wagen1.modell); }
        else if(vergleich_ergebnis == 2)
            { printf("Der %s hat das bessere Preis- Leistungsverhaeltnis.\n", wagen2.modell); }
        else
            { printf("Der %s hat das bessere Preis- Leistungsverhaeltnis.\n", wagen3.modell); }
        
        printf("\n\nGIMMICK:\n");
        printf("%5.2f PS sind %5.2f KW\n", zgr_w1->ps, (zgr_w1->ps / 1.36));    
        printf("%5.2f PS sind %5.2f KW\n\n", zgr_w2->ps, (zgr_w2->ps / 1.36));
        
        return(0);
    }
    
    int vergleich(AUTO *zgr_wagen1, AUTO *zgr_wagen2, AUTO *zgr_wagen3)
    {
        float wagen1_ps, wagen2_ps, wagen3_ps;
        float wagen1_preis, wagen2_preis, wagen3_preis;
        
        float preis_leistung1;
        float preis_leistung2;
        float preis_leistung3;
        
        //'->' arrow operator - is equal with '(*zgr_wagen1).ps'
        wagen1_ps = zgr_wagen1->ps;
        wagen1_preis = zgr_wagen1->preis;
        
        wagen2_ps = zgr_wagen2->ps;
        wagen2_preis = zgr_wagen2->preis;
        
        wagen3_ps = zgr_wagen3->ps;
        wagen3_preis = zgr_wagen3->preis;
             
        //Price pro PS/KW
        preis_leistung1 = (wagen1_preis / wagen1_ps);
        preis_leistung2 = (wagen2_preis / wagen2_ps);
        preis_leistung3 = (wagen3_preis / wagen3_ps);
             
        //Compare purchase price to PS
        //How many Dollar/Euro for one PS/KW
        if(preis_leistung1 == preis_leistung2 && preis_leistung1 == preis_leistung3)
        {
            return(0);
        }
        else if(preis_leistung1 < preis_leistung2 && preis_leistung1 < preis_leistung3)
        {
            return(1);        
        }
        else if(preis_leistung2 < preis_leistung1 && preis_leistung2 < preis_leistung3)
        {
            return(2);
        }
        else { return(3); }        
    }
    Structures on functions-autotest3ok-jpg

  5. #20
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Please don't use fflush(stdin) !!!
    I know that Microsoft has extend the function that is also usable for stdin, but this is only valid an OSes from Microsoft!
    We discuss in this forum the language C in general and without OS specific extensions.
    It is good to know it, but you don't know what OS the other people here are using.

    One OS independent approach is:

    Code:
    void flush_stdin (void) {
        int c;
        while ((c = getchar(stdin)) != '\n' && c != EOF);
        return;
    }
    You can use this function whenever you need fflush(stdin).
    Other have classes, we are class

  6. #21
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    34
    Quote Originally Posted by WoodSTokk View Post
    Please don't use fflush(stdin) !!!
    . . .
    It is good to know it, but you don't know what OS the other people here are using.
    One OS independent approach is:

    Code:
    void flush_stdin (void) {
        int c;
        while ((c = getchar(stdin)) != '\n' && c != EOF);
        return;
    }
    You can use this function whenever you need fflush(stdin).
    Yes, fflush() is not popular. But it worked. Your function works too now. Thanks! There was only a little mistake - I get an error "too many arguments":
    Code:
    getchar(stdin)
    This works fine now:
    Code:
    void flush_stdin (void);
    . . .
    //Daten einlesen - Input data
        printf("\nFirma    : ");
        scanf(" %49[^\n]", wagen1.firma);
        printf("Modell   : ");
        scanf(" %49[^\n]", wagen1.modell);
        
        printf("Anzahl PS: ");
        scanf(" %f", &wagen1.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen1.preis);
        flush_stdin();
        
                
        printf("\nFirma    : ");
        scanf(" %49[^\n]", wagen2.firma);
        printf("Modell   : ");
        scanf(" %49[^\n]", wagen2.modell);
            
        printf("Anzahl PS: ");
        scanf(" %f", &wagen2.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen2.preis);
        flush_stdin();    
        
        printf("\nFirma    : ");
        scanf(" %49[^\n]", wagen3.firma);
        printf("Modell   : ");
        scanf(" %49[^\n]", wagen3.modell);
            
        printf("Anzahl PS: ");
        scanf(" %f", &wagen3.ps);
        printf("Preis    : ");
        scanf(" %f", &wagen3.preis);
        flush_stdin();
    
    void flush_stdin (void) 
    {
       int c;
       while ((c = getchar()) != '\n' && c != EOF);
       return;
    }
    Last edited by Kernelpanic; 5 Hours Ago at 09:47 AM.

  7. #22
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    34
    This was the error:

    Structures on functions-getchar-fehler-jpg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and Functions
    By r895 in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2014, 01:55 PM
  2. help me with structures using functions
    By magnetpest2k7 in forum C Programming
    Replies: 2
    Last Post: 10-07-2009, 01:32 AM
  3. Functions in Structures
    By pritin in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2007, 01:40 AM
  4. functions and structures
    By subflood in forum C Programming
    Replies: 2
    Last Post: 06-11-2005, 06:15 PM
  5. structures and functions
    By student2005 in forum C++ Programming
    Replies: 1
    Last Post: 10-16-2003, 04:12 PM

Tags for this Thread