Problem with printing arrays

This is a discussion on Problem with printing arrays within the C Programming forums, part of the General Programming Boards category; The problem im having is, my program takes the name (and various other details) in from a data file. Works ...

  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
    452
    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
    5,852
    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%.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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).
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    20,971
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,971
    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);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 Tx were sold at 3.25 per unit.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,971
    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;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,971
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 04: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21