I have no idea what that is!
I have no idea what that is!
??? 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?
@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.
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".
Nice, as for the feeling silly, we all have our moments
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:
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.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); } }
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!!
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:Originally Posted by awsdert
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.Code:int grades_f(struct student* const grades_in);
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).Originally Posted by Glorn
Good advice for C++, but this is C though, so there's no difference when the expressions are standalone.Originally Posted by awsdert
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:
Now, you see i have take the index 'j' for graces.Code:… scanf(" %d", grades_in->grades[j]); …
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
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.
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
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.
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
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.
Thank you, give me a minute, just looking through your code now
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.