GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Oct-2006, 23:53
nic.nmd nic.nmd is offline
New Member
 
Join Date: Aug 2006
Posts: 12
nic.nmd is on a distinguished road
Unhappy

printing an array of structures


I am writing a program that reads columns of info from a text file, then sorts the info by alphabetical order from the first column. This won't compile, I highlited the problem below, the code where i try to print the sorted data. I tried changing the '.'s to '->'s but that didn't work. Please help!

CPP / C++ / C Code:
//data.txt

Site Date Reading Rating
Ka5.445 1/02/2006 12.3323 1
Ka2.43345 2/12/2006 23.5534 2
Ka1.43245 23/12/2006 -54.6345 5
J2.3345 1/02/2006 12.1887 3
Gs.7785 2/12/2006 45.0987 6
Mv.57737 4/09/2006 54.8876 1
J2.33444 2/12/2006 12.0086 1
Mv.78868 4/07/2006 0.00984 2
Gs.6654 1/02/2006 334.5667 2
Te.57737 1/05/2006 3.5564 5
J2.33888 6/04/2006 3.7764 4
Te.979949 21/08/2006 443.7757 7
Gs.88632 2/12/2006 33.6655 8
Gs.563636 1/02/2006 1.0086 4
Ha.895848 23/09/2006 -2.7756 4
J2.7745 2/12/2006 -12.5543 8
Ha.895848 5/06/2006 43.8865 2
Ge.646623 2/12/2006 23.7757 1
J2.3545 5/06/2006 0.6634 5

CPP / C++ / C Code:
#include<stdio.h>
#include<string.h>



struct table
{
  char site[15];
  char date[15];
  float reading;
  int rating;
};

void readData(struct table*tableArray);
void printData(struct table*tableArray);
void sortSite(struct table*tableArray);



int main(void)
{
  int choice;
  struct table tableArray[20];
  readData(tableArray);
  while(choice!=4)
  {
    printData(tableArray);
    printf("\n1. Sort by site\n");
    printf("2. Quit\n");
    printf("\nEnter your choice (1 or 2) ");
    scanf("%d",&choice);
    {
      switch(choice)
      {
        case 1:
          sortSite(tableArray);
          break;
        default:
          printf("Invalid entry\n");
          break;
      }
    }
  }
  return(0);
}


void readData(struct table*tableArray)
{
  FILE*data;
  int x=0;
  char site[15];
  char date[15];
  char reading[15];
  char rating[15];
  data=fopen("data.txt","r");
  if(data==NULL)
  {
    printf("Failed to open data.txt");
    exit(0);
  }
  fscanf(data,"%s %s %s %s\n",site,date,reading,rating);
  for(x=0;x<=18;x++)
  {
    fscanf(data,"%s %s %f %d\n",tableArray[x].site,tableArray[x].date,&tableArray[x].reading,&tableArray[x].rating);
  if((fscanf(data,"%s %s %f %d\n",tableArray[x].site,tableArray[x].date,&tableArray[x].reading,&tableArray[x].rating))!=4)
  {
    printf("\n\aError in line %d of data.txt\n",x);
    exit(0);
  }
  }
  fclose(data);
}


void printData(struct table*tableArray)
{
  int x=0;
  printf("Site Date Reading Rating\n");
  for(x=0;x<=18;x++)
  {
    printf("%s %s %f %d\n",tableArray[x].site,tableArray[x].date,tableArray[x].reading,tableArray[x].rating);
  }
}


void sortSite(struct table*tableArray)
{
  int x,y;
  struct table siteSorted;
  for(x=0;x<18;x++)
    for(y=0;y<17;y++)
      if((strcmp(tableArray[y].site,tableArray[y+1].site))>=0)
      {
        siteSorted=tableArray[y+1];
        tableArray[y+1]=tableArray[y];
        tableArray[y]=siteSorted;
      }
  printf("Site Date Reading Rating\n");
  printf("%s %s %f %d\n",siteSorted[x].site,siteSorted[x].date,siteSorted[x].reading,siteSorted[x].rating); // <-problem!?!
}



  #2  
Old 19-Oct-2006, 01:18
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: printing an array of structures


Guideline #2 please....
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 19-Oct-2006, 01:33
nic.nmd nic.nmd is offline
New Member
 
Join Date: Aug 2006
Posts: 12
nic.nmd is on a distinguished road

Re: printing an array of structures


Compiler: Bloodshed Dev-C++
Error msg: "subscripted value is neither array nor pointer"
I changed the code but it still doesn't compile. It is supposed to print out the siteSorted array of structures.

New code (just sortSite function)
CPP / C++ / C Code:
void sortSite(struct table*tableArray)
{
  int x,y;
  struct table siteSorted;
  for(x=0;x<18;x++)
    for(y=0;y<17;y++)
      if((strcmp(tableArray[y].site,tableArray[y+1].site))>=0)
      {
        siteSorted=tableArray[y+1];
        tableArray[y+1]=tableArray[y];
        tableArray[y]=siteSorted;
      }
  printf("Site Date Reading Rating\n");
  for(x=0;x<=18;x++)
  {  
/* problem-> */    printf("%s %s %f %d\n",siteSorted[x].site,siteSorted[x].date,siteSorted[x].reading,siteSorted[x].rating);
  }
}
  #4  
Old 19-Oct-2006, 07:53
rastwi rastwi is offline
New Member
 
Join Date: Oct 2006
Posts: 3
rastwi is on a distinguished road

Re: printing an array of structures


quite alot of errors in ur pgm!

readData(tableArray); \\no reference given...
similarly for other functions too...


then declaring like: struct table siteSorted;
and using it like: printf("%s %s %f %d\n",siteSorted[x].site,siteSorted[x].date,siteSorted[x].reading,siteSorted[x].rating); //

just go through it again!
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing a 2D array as a grid to stdout trouble nic.nmd C Programming Language 6 06-Oct-2006 06:45
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
Array printing Cat C++ Forum 19 27-Oct-2005 19:38
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:19.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.