Thread: Need help with a project

  1. #46
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Changed it to fseek, didint help, I think it has something to do with memory allocation? How else can you explain popping random numbers from function?

  2. #47
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    sum_v should have been initialised to 0, I think supposed to use 0.0f but I've never had an issue caused by using the former yet.

  3. #48
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    also I don't see suma declared anywhere, was that supposed to be sum_v?

  4. #49
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Quote Originally Posted by awsdert View Post
    also I don't see suma declared anywhere, was that supposed to be sum_v?
    Yea, it should be sum_v, sum_v was initialised to 0, I was changing everything, the function is still broken.

  5. #50
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    hmmm, I might try compiling it myself in a minute, could you post the header please.

  6. #51
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    If it doesn't work, could you tell me how to make a loop to sum up all the values? I guess it should work without function.

  7. #52
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by WoodSTokk View Post
    I hope you don't lose your interest in C programming because this little mistake
    Me, or Glonn? We all make mistakes, and dangling newlines are such a classic.

    I was aware of the scanf-with-space trick, but I wouldn't personally use it. I don't like the idea if a function muching up all my whitespace. Plud, if you screw it up and put some none matching input in, you'll end up in an infinite loop. You're reallly dependent on your user not screwing it up.

    I'm just defending my avoidance of scanf..... not advocating that we all stuff our code endless getchars() and checks, Obviously the flipside of the scanf infinuite look is that getchar and friends can quite happily block you waiting for input. I've certainly had to abandon using getchar() and friends because it just got too complicated knowing where the next newline was coming from,

    So although I accept your point, I think different ways of getting over this incrediblk nasty inconsistent weird behaviour suit different cicumstances. I know there are safe ways of using scanf, with the right checks and balances, but beeeeh. It's not that I can't be bothered, it's just there's always some corner case I've missed.

    Sorry if I'm rambling and not making much sense - I'm really tired but insomnia..... sucks.

  8. #53
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    This is what i have in "book.h" file:

    Code:
    #define MAXTYT 40
    #define MAXAUT 40
    #define MAXKS 10
    
    struct book {
        char title[MAXTYT];
        char author[MAXAUT];
        float value;
    };
    
    
    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", sum_v);
    }

  9. #54
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    ah so it was already included, my bad , give me a minute I'll try and see what my command line spits out, while I copy do you mind listing what you entered into the command line, I feel it may be easier to get matching results if I work with same data.

  10. #55
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    This time I entered:

    title1,author1,10
    title2,author2,20

    Total value: 18644746240...

    I removed books.dat file 2 items, entered exactly the sama input, and it worked normally, giving correct output, but in a moment it won't work again.
    Last edited by Glorn; 02-05-2015 at 05:51 PM.

  11. #56
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Well after some modifications to get it to compile I had no issues on both times I ran it, I also didn't like the fact sum was not told the counter so I added a variable for that, then there was the struct book used so much, for type safety I turned book into a typedef and made the size variable in main constant, I also took the liberty of forcing the array to be initialised before it is ever used thereby eliminating random data from the wild, finally bibl was too abbreviated so for sensible reading I did a replace on the name so that it now reads aBooks, here is the modified result:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXTYT 40
    #define MAXAUT 40
    #define MAXKS 10
    
    typedef struct book_
    {
      float value;
      char  title[MAXTYT];
      char  author[MAXAUT];
    } book;
    
    float sum( book *sr, int count );
    
    int main( int argc, char* argv[] )
    {
      book aBooks[MAXKS] = {{0}};
      int counter = 0;
      int index, counterp;
      FILE* pbooks;
      int const size = sizeof( 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( &aBooks[counter], size, 1, pbooks ) == 1 )
      {
        if ( counter == 0 )
        {
          fprintf( stdout, "Current content of file books.dat:\n" );
        }
    
        fprintf( stdout, "%s by %s: %.2f zl\n", aBooks[counter].title,
                 aBooks[counter].author, aBooks[counter].value );
        counter++;
      }
    
      counterp = counter;
    
      if ( counter == MAXKS )
      {
        fprintf( stdout, "File books.dat is full.\n", stderr );
        exit( 2 );
      }
    
      fprintf( stdout, "Put new titles of books.\n" );
      fprintf( stdout, "To exit press [enter]\n" );
    
      while ( counter < MAXKS && gets( aBooks[counter].title ) != NULL
              && aBooks[counter].title[0] != '\0' )
      {
        fprintf( stdout, "Enter author\n" );
        gets( aBooks[counter].author );
        fprintf( stdout, "Enter value\n" );
        fscanf( stdin, "%f", &aBooks[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", aBooks[index].title,
                 aBooks[index].author, aBooks[index].value );
    
      fwrite( &aBooks[counterp], size, counter - counterp, pbooks );
      fclose( pbooks );
      sum( aBooks, counter );
      system( "PAUSE" );
      return 0;
    }
    
    float sum( book *sr, int count )
    {
      int i = 0;
      float total = 0.0f;
      puts( "Starting sum at 0.00\n" );
    
      while ( i < count )
      {
        total = total + sr[i].value;
        // Wanted to examine total as it built up, seemed appropriate to add a quick average after
        printf( "+ #%d %s: %.2f\n", i, sr[i++].title, total );
      }
    
      printf( "Average value: %.2f\n", total / count );
    }

  12. #57
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    I got work tomorrow so I'm going to bed now, good luck.

  13. #58
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by smokeyangel View Post
    Me, or Glonn? We all make mistakes, and dangling newlines are such a classic.
    i decided Glonn.

    Quote Originally Posted by smokeyangel View Post
    I was aware of the scanf-with-space trick, but I wouldn't personally use it. I don't like the idea if a function muching up all my whitespace. Plud, if you screw it up and put some none matching input in, you'll end up in an infinite loop. You're reallly dependent on your user not screwing it up.

    I'm just defending my avoidance of scanf..... not advocating that we all stuff our code endless getchars() and checks, Obviously the flipside of the scanf infinuite look is that getchar and friends can quite happily block you waiting for input. I've certainly had to abandon using getchar() and friends because it just got too complicated knowing where the next newline was coming from,

    So although I accept your point, I think different ways of getting over this incrediblk nasty inconsistent weird behaviour suit different cicumstances. I know there are safe ways of using scanf, with the right checks and balances, but beeeeh. It's not that I can't be bothered, it's just there's always some corner case I've missed.
    No, you are right, there are other ways to read safely the input. My advice was only a quick&dirty solution.
    Personally I read in the complete line with fgets and search for the specific values in the readed string with sscanf.
    fgets will also take the newline, so there is no newline in stdin.
    Other have classes, we are class

  14. #59
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    No way -- I use fgets and parse stuff myself too. Just didn't want to come off as a total nutter I do use scanf a lot -- but when it comes diwn to serious stuff (or even unserious stuff that I know some nutter is going to type the wrong thing into), I wouud tend to fgets and parse. It's a bit labourious but sometimes you need to have a lot of confidence in what you've written.

    I actually also quite like C++ for such things (like variable length strings that you've no clue about. Buuuut, I'm one of those half breeds who writes 50/50 C and C++. It's like "ooooh, vectors!! Nice! How do I printf() them?"

  15. #60
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Thanks all for help, I passed my exam, didnt get the best mark(3.5/5.0), but still, it's nice

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