Thread: Problem with printing arrays

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    10

    Problem with printing arrays

    The problem im having is, my program takes the name (and various other details) in from a data file. Works out the total price for each week (adding if there are two or more sales in one week) and then outputs the total along with the week number. That all runs perfectly.

    The hard part is when I try to print the largest single sale. I successfully print the week number, the price and the number of units. However I cant work out how to print the name. my fprintf statement is as follows.

    Code:
     
    fprintf(output, "\nThe largest single sale was in week %i, when %i units of %s were sold at %g per unit.\n", largest.week, largest.units, &largest.name[0] (float)largest.price/100) ;
    Any help is appreciated.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Is Largest.name[0] char* ?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assuming largest.name is an array of char, instead of "&largest.name[0]" use the (equivalent) "largest.name".

    If largest.name is a pointer, the same applies, but you need to allocate storage to hold the data read in from the file (pointers don't magically point at anything unless you make them do so).

    Then add a comma immediately after (before "(float)largest.price/100"). It is that missing comma that is making your compiler complain.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    its part of a struct

    Code:
     struct sale
      {
        int week  ;
        char name[30] ;
        int units ;
        int price ;
        int total ;
      } ;
    basically, I want to limit it to 30 characters, ive tried using %30s in the past, however it limited it to 30 but also required 30 characters to be entered, which obviouslly isnt helpful :P


    as for the comma, its there in the program, must have deleted it while I was copying it accross program compiles and the response I get is

    The largest single sale was in week 0, when 514 units of Ú· were sold at 3.25 per unit.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by anxty View Post
    I want to limit it to 30 characters, ive tried using %30s in the past, however it limited it to 30 but also required 30 characters to be entered
    I do not get it how limiting the output of the string to 30 chars also requires to input 30 chars during input? there are 2 different operations with 2 different limitations...

    also your array is suitable to store only 29 chars long strings (since you need a place for nul-terminator).
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Im not sure why it did that either. Obviouslly I did something wrong from the start.

    though right now im more concerned with printing a name read from input.

    I dont know how to do it, all my attemps just get an odd output. I can show all of my code if that will help?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anxty
    I can show all of my code if that will help?
    Yes, though if it is too long, show the smallest and simplest compilable program that demonstrates the problem.
    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. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
      FILE *output ;
      FILE *input ;
    
      struct sale
      {
        int week  ;
        char name[30] ;
        int units ;
        int price ;
        int total ;
      } ;
    
      void result(int week[])
    {
      for(int i = 0 ; i < 13 ; i++)
      {
        fprintf(output, "\n%i        %g", i, ((float)week[i]/100)) ;
      }
    }
    
    
    void close(void)
    {
      fclose(input) ;
      fclose(output) ;
    }
    
    
    
    int main(int argc, char *argv[])
    {
    
      int week[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ;
    
    
      struct sale info ;
      struct sale largest ;
    
      info.units = 0 ;
      info.price = 0 ;
      largest.price = 0 ;
      largest.units = 0 ;
    
      input = fopen(argv[1], "r") ;
      output = fopen(argv[2], "w") ;
    
      if(( fopen(argv[1], "r")) == NULL)
      {
        printf("Sorry, this file cannot be opened.\n") ;
        exit(1) ;
      }
      else if(( fopen(argv[2], "w")) == NULL)
      {
        printf("Sorry, this file cannot be opened.\n") ;
        exit(2) ;
      }
    
      while(fscanf(input, "%i%30s%i%i", &info.week, info.name, &info.units,
            &info.price) != EOF)
      {
    
        week[info.week] += info.units * info.price ;
        info.total = week[info.week] ;
        if(info.price * info.units > largest.units * largest.price)
          {
            largest.price = info.price ;
            largest.units = info.units ;
            largest.week = info.week ;
            info.name[info.week] = largest.name[1] ;
          }
      }
    
      fprintf(output, "WEEK   TOTAL SALES") ;
    
      result(week) ;
      fprintf(output, "\nThe largest single sale was in week %i, when %i units of %30s were sold at %g per unit.\n", largest.week, largest.units,
              largest.name, (float)largest.price/100) ;
    
      close() ;
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    info.name[info.week] = largest.name[1] ;
    C doesn't know how to do string = string.

    You probably want to use strcpy() to copy the name.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    would it work just saying

    strcpy(info.name[info.week], largest.name[1]) ;

    or is that wishful thinking :P

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, this looks wrong:
    Code:
      input = fopen(argv[1], "r") ;
      output = fopen(argv[2], "w") ;
    
      if(( fopen(argv[1], "r")) == NULL)
      {
        printf("Sorry, this file cannot be opened.\n") ;
        exit(1) ;
      }
      else if(( fopen(argv[2], "w")) == NULL)
      {
        printf("Sorry, this file cannot be opened.\n") ;
        exit(2) ;
      }
    It should be something like:
    Code:
    input = fopen(argv[1], "r");
    if (input == NULL)
    {
        printf("Sorry, '%s' cannot be opened.\n", argv[1]);
        exit(1);
    }
    
    output = fopen(argv[2], "w");
    if (output == NULL)
    {
        printf("Sorry, '%s' cannot be opened.\n", argv[2]);
        fclose(input);
        exit(2);
    }
    But you also should be checking argc before you acces argv[1] and argv[2].

    Quote Originally Posted by anxty
    would it work just saying

    strcpy(info.name[info.week], largest.name[1]) ;
    More likely:
    Code:
    strcpy(info.name, largest.name);
    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

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    ok new code reads

    Code:
    if(info.price * info.units > largest.units * largest.price)
          {
            largest.price = info.price ;
            largest.units = info.units ;
            largest.week = info.week ;
            strcpy(info.name,  largest.name) ;
          }
    new output reads

    The largest single sale was in week 0, when 514 units of Tªx· were sold at 3.25 per unit.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try:
    Code:
    while (fscanf(input, "%i%29s%i%i", &info.week, info.name, &info.units, &info.price) == 4)
    {
        week[info.week] += info.units * info.price;
        info.total = week[info.week];
        if (info.price * info.units > largest.units * largest.price)
        {
            largest = info;
        }
    }
    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

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anxty View Post
    ok new code reads

    Code:
    if(info.price * info.units > largest.units * largest.price)
          {
            largest.price = info.price ;
            largest.units = info.units ;
            largest.week = info.week ;
            strcpy(info.name,  largest.name) ;
          }
    new output reads
    Code:
    strcpy(largest.name, info.name);
    Laserlight's solution is better...

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    On second thought, it should be more like:
    Code:
    while (fscanf(input, "%i%29s%i%i", &info.week, info.name, &info.units, &info.price) == 4)
    {
        week[info.week] = info.total = info.units * info.price;
        if (info.total > largest.total)
        {
            largest = info;
        }
    }
    The += thing kind of tricked me: you're just adding to 0, but your intention is really to assign, not to add to the current value.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to arrays problem
    By key4life in forum C Programming
    Replies: 11
    Last Post: 02-13-2010, 05:56 PM
  2. Problem with Arrays
    By dldsob in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2009, 08:43 PM
  3. Problem with printing instruction pointer
    By Subsonics in forum C Programming
    Replies: 0
    Last Post: 10-11-2009, 03:33 PM
  4. Printing 2d array problem
    By Rob4226 in forum C Programming
    Replies: 3
    Last Post: 10-01-2009, 03:56 PM
  5. Homework problem...structures or arrays?
    By tortan in forum C++ Programming
    Replies: 21
    Last Post: 08-30-2006, 01:26 AM

Tags for this Thread