Thread: Need help with a project

  1. #31
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I have no idea what that is!

  2. #32
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    ??? As far as I know msw is common abbreviation of microsoft windows and cmdline a common abbreviation of command line, is there anything else confusing you?

  3. #33
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    @smokeyangel

    At the moment, I stopped working on this project because my function to read grades is not working properly and no one seems to know why is that. I can't continue without that part of the program.
    Last edited by Glorn; 02-05-2015 at 07:48 AM.

  4. #34
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by awsdert View Post
    I didn't even know that was a msw command, then again I never use the cmdline for doing anything other than viewing data anyway.
    Ahhhhh. I read it as "I didn't even know there was an msw command. I am no longer confused, just feel a bit silly

    I'll give this to "pause" -- it's ridiculously easy. I believe Windows also has a getch function, which resumes as soon as you press a key (unlike getc/getchar, which needs you to press enter). Out of boredom and curiosity, I did figure out how to do the same things in Linux. I did it with the Curses library, which isn't rocket science but it's sure as hell much more complicated than "pause".

  5. #35
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Nice, as for the feeling silly, we all have our moments

  6. #36
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Glorn View Post
    @smokeyangel

    At the moment, I stopped working on this project because my function to read grades is not working properly and no one seems to know why is that. I can't continue without that part of the program.
    I'm sorry to hear that. I did a bit of digging at your code and I'm pretty sure I know what the problem is. I think you have trailing newlines after your grade entry. You know how you have to press enter before the program will continue? Well, the %f in the scanf (or more properly the %d....) is a pattern matching mechanism -- it doesn't just read stuff into the variable, it reads *only* stuff that matches its pattern. THe newline will just sit in the stdin buffer waiting for something to happen.

    Frustratingly, all compilers/computers seem to have a slightly different take on how to deal with this. This is why I think other people haven't been able to reproduce the same problem. It could be even worse.... could be that their machines think a newline is a perfectly valid float. You wouldn't be able to tell anyway, since honestly, reading floats into integer variables is just not going to work.

    So, I know you said you have decided to stop working on it, but if you have time/motivation you could do a very quick test to see if I'm right.

    This bit of code here:
    Code:
    int grades_f(struct student* grades_in)
    {
        int i;
        for (i = 0; i < MAX; i++, grades_in++) {
            printf("Grades of student %d: \n", i + 1);
            scanf("%f", grades_in->grades);
        }
    }

    Could you change it to:
    Code:
    int grades_f(struct student* grades_in)
    {
    	int i;
    	for (i = 0; i < MAX; i++, grades_in++) {
    		char c;
    		printf("Grades of student %d: \n", i + 1);
    		scanf("%f", grades_in->grades);
    		c = getchar();
    		printf("  %d   ", c);
    	}
    }
    All this is doing is picking up the stray newline, assuming I'm right about this. The printf is just to test the theory -- if I'm right then you should see a spurious newline character code (that's a "10") after you enter the grade. I would expect you to be able to enter the other grades, too.

    Like I said, the printf is pointless, so if this works for you then just take it out, There are several ways of dealing with this problem -- getchar() is fine, there are also some other methods. If this helps, then I suggest googling "scanf trailing newline".

    Good luck with it -- hope this was of some help and hope that I didn't waste a load of your time only to find I was wrong. To be honest I was saddened a bit by your post -- we should all be programmers!! 24/7! YEEEAH!!

  7. #37
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by awsdert
    If you intended grades_in to be a constant pointer then you're mis-understanding something, const modifier makes the content of a variable constant not the variable itself so in this instance the struct is constant but not the pointer.
    A clarification is in order: it is this particular placement of the const keyword that declares that what the pointer points to is const in that context. You had written:
    Code:
    int grades_f(struct student* const grades_in);
    Then it would declare that the pointer parameter itself is const, but of course this is not relevant information to the caller so it would normally be left out of the function declaration, and would be placed in the function definition, if it is used at all.

    Quote Originally Posted by Glorn
    I've got a different issue now, the function works, but I can't enter grades for more than 1 student, after that it's just scrolls down and the program is over.
    When you fix something and still have a problem, remember to post the updated code. It can also help for you to post your test input so that say, another person can compile your program, enter your test input, and observe the same thing (or not).

    Quote Originally Posted by awsdert
    no ideas right now but for iterators should get in the habit of putting ++ on left side for speed (unless you have good reason to put on right hand side like in your first loop)
    Good advice for C++, but this is C though, so there's no difference when the expressions are standalone.
    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

  8. #38
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Glorn View Post
    Code:
    int grades_f(struct student* grades_in)
    {
        int i;
        for (i = 0; i < MAX; i++, grades_in++) {
            printf("Grades of student %d: \n", i + 1);
            scanf("%f", grades_in->grades);
        }
    }
    As others have pointed out, the formatstring should contain %d for integer.
    To ignore newlines in front of the integer, you can insert a space in formatstring.
    A scace in formatstring represent all whitespaces (spaces, newlines, carriage returns, horizontal tab, vertical tab, etc…).
    BTW, the member graces in the struct is an array of integers, not an single integer.
    You should give an index to graces.
    Your scanf call should then look like:
    Code:
    …
            scanf(" %d", grades_in->grades[j]);
    …
    Now, you see i have take the index 'j' for graces.
    You need an inner loop to iterate over the graces of one student.
    The scanf call take place in this inner loop.
    If you don't want to fill all graces in, you need a sentinel value to break the loop.
    In the loop, you check every inputed integer against the sentinel value and if the value is true brake the loop and go forward to the next student.

    I hope you don't lose your interest in C programming because this little mistake
    Other have classes, we are class

  9. #39
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Thank you everyone for help, I'll definitely not lose interest in C!

    At the moment I got different program, I wrote some, some I took from the book, this program saves structure into the file. Everything is good except one thing, I've got a function that sums up values of all the books given to the structure, and what is very strange, the function shows correct results 5 times, than it shows some random numbers like 234233234234, after that it can show correct result or random, anyone has idea what the problem might be?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "ksiazka.h"
    #define MAXTYT 40
    #define MAXAUT 40
    #define MAXKS 10
    
    struct ksiazka {
        char tytul[MAXTYT];
        char autor[MAXAUT];
        float wartosc;
    };
    
    
    
    float srednia(struct ksiazka* sr);
    int main(int argc, char* argv[])
    {
        struct ksiazka bibl[MAXKS];
        int licznik = 0;
        int index, licznikp;
        FILE* pksiazki;
        int rozmiar = sizeof(struct ksiazka);
        if ((pksiazki = fopen("ksiazki.dat", "a+b")) == NULL) {
            fprintf(stdout,"Nie moge otworzyc pliku ksiazki.dat\n", stderr);
            exit(1);
        }
        rewind(pksiazki);
        while (licznik < MAXKS && fread(&bibl[licznik], rozmiar, 1, pksiazki) == 1) {
            if (licznik == 0)
               fprintf(stdout,"Biezaca zawartosc pliku ksiazki.dat:");
            fprintf(stdout,"%s by %s: %.2f zl\n", bibl[licznik].tytul,
                   bibl[licznik].autor, bibl[licznik].wartosc);
            licznik++;
        }
        licznikp = licznik;
        if (licznik == MAXKS) {
            fprintf(stdout,"Plik ksiazki.dat jest pelny.", stderr);
            exit(2);
        }
        fprintf(stdout,"Podaj nowe tytuly ksiazek.\n");
        fprintf(stdout,"Aby zakonczyc, wcisnij [enter] na poczatku wiersza.\n");
        while (licznik < MAXKS && gets(bibl[licznik].tytul) != NULL
               && bibl[licznik].tytul[0] != '\0') {
            fprintf(stdout,"Teraz podaj autora. \n");
            gets(bibl[licznik].autor);
            fprintf(stdout,"Teraz podaj wartosc.\n");
            fscanf(stdin,"%f", &bibl[licznik++].wartosc);
            while (getchar() != '\n')
                continue;
            if (licznik < MAXKS)
                fprintf(stdout,"Podaj nastepny tytul.\n");
        }
        fprintf(stdout,"Oto lista Twoich ksiazek: ");
        for (index = 0; index < licznik; index++)
            fprintf(stdout,"%s, autor: %s, cena: %.2f zl\n", bibl[index].tytul,
                   bibl[index].autor, bibl[index].wartosc);
        fwrite(&bibl[licznikp], rozmiar, licznik - licznikp, pksiazki);
        fclose(pksiazki);
        srednia(bibl);
        system("PAUSE");
        return 0;
    }
    
    float srednia(struct ksiazka* sr)
    {
        int i;
        float suma;
        for (i = 0; i < MAXKS; i++) {
            suma = suma + sr[i].wartosc;
        }
        printf("Laczna wartosc: %.2f", suma);
    }

    Normally the program is in 2 files, structure and function are in different file, sorry that language "inside" is not english, hope its not too much of a problem.

  10. #40
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    line 43, seems weird to me that you don't reset licznik to 0 before you loop through the students again, though I might understand better if you translate all the variables to english

  11. #41
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    If I reset licznik at line 43 it will not print a list of all the names at the end. Christ I'm totally furious, how it is possible that this function can work 20 times than not work 5 times? There is no pattern at all, doesnt matter what i enter.

  12. #42
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Well as long as your code is in whatever language it's in myself an most of the people here will not be able to help you because we cannot grasp what each section is doing/trying to do properly

  13. #43
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "book.h"
    #define MAXTYT 40
    #define MAXAUT 40
    #define MAXKS 10
    
    struct book {
        char title[MAXTYT];
        char author[MAXAUT];
        float value;
    };
    
    
    
    float sum(struct book* sr);
    int main(int argc, char* argv[])
    {
        struct book bibl[MAXKS];
        int counter = 0;
        int index, counterp;
        FILE* pbooks;
        int size = sizeof(struct book);
        if ((pbooks = fopen("books.dat", "a+b")) == NULL) {
            fprintf(stdout,"Can't open file books.dat\n", stderr);
            exit(1);
        }
        rewind(pbooks);
        while (counter < MAXKS && fread(&bibl[counter], size, 1, pbooks) == 1) {
            if (counter == 0)
               fprintf(stdout,"Current content of file books.dat:");
            fprintf(stdout,"%s by %s: %.2f zl\n", bibl[counter].title,
                   bibl[counter].author, bibl[counter].value);
            counter++;
        }
        counterp = counter;
        if (counter == MAXKS) {
            fprintf(stdout,"File books.dat is full.", stderr);
            exit(2);
        }
        fprintf(stdout,"Put new titles of books.\n");
        fprintf(stdout,"Tu exit press [enter] \n");
        while (counter < MAXKS && gets(bibl[counter].title) != NULL
               && bibl[counter].title[0] != '\0') {
            fprintf(stdout,"Enter author \n");
            gets(bibl[counter].autor);
            fprintf(stdout,"Enter value\n");
            fscanf(stdin,"%f", &bibl[counter++].value);
            while (getchar() != '\n')
                continue;
            if (counter < MAXKS)
                fprintf(stdout,"Enter next title.\n");
        }
        fprintf(stdout,"List of your books: ");
        for (index = 0; index < counter; index++)
            fprintf(stdout,"%s, author: %s, price: %.2f zl\n", bibl[index].title,
                   bibl[index].author, bibl[index].value);
        fwrite(&bibl[counterp], size, counter - counterp, pbooks);
        fclose(pbooks);
        sum(bibl);
        system("PAUSE");
        return 0;
    }
    
    float sum(struct ksiazka* sr)
    {
        int i;
        float sum_v;
        for (i = 0; i < MAXKS; i++) {
            suma = sum_v + sr[i].wartosc;
        }
        printf("Total value: %.2f", suma);
    }

    Here it is, user gives name of the book, author and its value as input, the whole structure is stored in a file.

    As i said, the program originally is in 2 files, in a file "books.h" i'm keeping structure and a function.
    Last edited by Glorn; 02-05-2015 at 04:58 PM.

  14. #44
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Thank you, give me a minute, just looking through your code now

  15. #45
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Well for starters there is the "a+b" of books.dat, that tells the file API that the file should ALWAYS be at the end, any reference will explain that parameter in more detail.
    Next is your usage of rewind, that's for if you've just read/wrote something, better to just use fseek( file, 0, SEEK_CUR ); instead to put the pointer at the very start of the file.
    Continuing to look through your code now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-02-2013, 06:45 PM
  2. Replies: 5
    Last Post: 02-23-2013, 03:37 PM
  3. Classes Project (Not class project)
    By adam.morin in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2011, 01:48 AM
  4. Paid project - The Free Marketing Project
    By sharefree in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 10-27-2010, 02:15 PM
  5. your 11th & 12th grade c++ project or similar project
    By sleepyheadtony in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-13-2002, 05:14 PM

Tags for this Thread