Solved

How to print an array using structures

Posted on 2011-03-16
11
409 Views
Last Modified: 2012-05-11
I've already loaded the file now I'm trying to print out the contents that I have stored into a structure. I'm having  a little trouble as to what I'm supposed to pull it from. I've put table.cust_id in my function at the very bottom but that doesn't work. I'm not sure what I'm supposed to put there. The code I'm having problems with is at the very bottom called listFile.
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <stdio.h>

struct customers
{
	int cust_id;
	char cust_name[19];
	char state[3];
	char discount;
	double balance_due;
	int order_out;
};

struct myRecords
{
	customers rec[10];
};

using namespace std;

//functions go here
void loadFile(FILE *, struct myRecords *, char[]);
void listFile(struct myRecords *);


int main()
{	char buffer[50];
	int counter = 0;



	myRecords table, *point_table;
	point_table = &table;

	FILE *fp;
	fp = fopen("ASSIGNV1.DAT", "r");
	if (fp == NULL)
	{
		printf ("Error opening file! ");
	}
	else
	{
		loadFile(fp, point_table, buffer);
		listFile(point_table);
	}
		

}


void loadFile(FILE *fp, struct myRecords * table, char buffer[])
{
	int counter = 0;
	while(feof(fp))
	{
		fgets(buffer, 20, fp);
		table->rec[counter].cust_id = atoi(buffer);
		fgets(table->rec[counter].cust_name, 20, fp);
		fgets(table->rec[counter].state, 20, fp);
		fgets(buffer, 20, fp);
		table->rec[counter].discount = buffer[0];
		fgets(buffer, 20, fp);
		table->rec[counter].balance_due = atof(buffer);
		fgets(buffer, 20, fp);
		table->rec[counter].order_out = atoi(buffer);
		++counter;
	}
	fclose(fp);
}
void listFile(struct myRecords * table)
{
	int x;
	for (x=0; x < 10; x++)
	{
		printf ("\n%i\n%s\n%s\n%s\n%f\n%i", table.cust_id, table.cust_name, table.state, table.discount, table.balance_due, table.order_out);
	}
}

Open in new window

0
Comment
Question by:reesee324
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 35150320
in loadFile, you refer to table->rec[counter].cust_id
but in listFile, you only use table.cust_id
0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35150344
try this:

printf ("\n%i\n%s\n%s\n%c\n%f\n%i", table[x].cust_id, table[x].cust_name, table[x].state, table[x].discount, table[x].balance_due, table[x].order_out);

Open in new window


remember table is an array, you must access it as such.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 35150350
Hi Reesee,

In the listFile function, you loop on the table, but never index into it.  Consequently, you should always print the first record.

This is a little bit of an unusual way to structure your data.  More common would be to define the customers structure (as you've done) then just create an array to hold the customer rows instead of a structure.

struct customers
{
      int cust_id;
      char cust_name[19];
      char state[3];
      char discount;
      double balance_due;
      int order_out;
};

struct customers customer_table[10];


  or even:

type struct
{
      int cust_id;
      char cust_name[19];
      char state[3];
      char discount;
      double balance_due;
      int order_out;
} customer_t;

customer_t  customer_table[10];


Kent
0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35150430
I agree with Kdo.  Using just one struct for your program would be a much better practice than two, and it will be much less confusing to anyone reading your code as well as yourself on how you are accessing the array.  Either way you should just need to add [x] to each variable in your print statement and it should work.  Also, in your struct, you have a variable that is just a char, and you should use %c to print that out, instead of %s...prob not required, but again, just better practice.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35150556

In my second example, it should be

  typedef struct

instead of

  type struct


Apologies...



Also, amatzinger is correct that you must use %c to print a character instead of %s.  Otherwise you're likely to experience a hardware fault (illegal address).


Kent
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:reesee324
ID: 35150641
Ok  Kdo well this may be a stupid question but how would I go about changing my loadFile if I change my structures?? When I do it like this struct customers customer_table[10]; and go back and change where I had myRecords and put customers it doesn't work. I get an error saying that the class customer has no rec and when I change it to table.rec[counter].cust_id = atoi(buffer); it says that table must have a class type.

Sorry I'm new to all the structures and still somewhat confused on all of them. Thanks for the advice though I understand the structures better the way you set them up just not sure how to incorporate it now.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35150672
Hi Reesee,

If you'll use the "typedef struct" definition in my example it becomes quite easy.  You'll treat the array of customers exactly as you'd treat any other array.


Is this classwork?  (I just need to know how much actual code that I can provide.)


Kent
0
 

Author Comment

by:reesee324
ID: 35150709
Yeah it's a program for school. I've been trying to post as much code as possible because I know ya'll can't just give answers but like I said I"m new to this and find that the book is not much help since most of C is logic but when I get stuck I get stuck :) Plus trying to do this for a long period of time can turn your brain to mush!
0
 

Author Comment

by:reesee324
ID: 35150781
Ok when I do the structure as you said with the typedef when I type in my code like this:
void loadFile(FILE *, struct customer_table *, char[]);


int main()
{
      char buffer[50];
      int counter = 0;

      customer_table table, *point_table;
      point_table = &table;

I get an error at customer_table table, *point_table saying that it expected a ';'
I'm just confused on what I'm supposed to declare there once I change my structure because it seems to no longer work. Below is the whole updated code.
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <stdio.h>

typedef struct 
{
      int cust_id;
      char cust_name[19];
      char state[3];
      char discount;
      double balance_due;
      int order_out;
} customer_t;

customer_t  customer_table[10];



using namespace std;

//functions go here
void loadFile(FILE *, struct customer_table *, char[]);


int main()
{
	char buffer[50];
	int counter = 0;

	customer_table table, *point_table;
	point_table = &table;

	FILE *fp;
	fp = fopen("ASSIGNV1.DAT", "r");
	if (fp == NULL)
	{
		printf ("Error opening file! ");
	}
	else
	{
		loadFile(fp, point_table, buffer);
	}
		

}


void loadFile(FILE *fp, struct customer_table * table, char buffer[])
{
	int counter = 0;
	while(feof(fp))
	{
		fgets(buffer, 20, fp);
		table->rec[counter].cust_id = atoi(buffer);
		fgets(table->rec[counter].cust_name, 20, fp);
		fgets(table->rec[counter].state, 20, fp);
		fgets(buffer, 20, fp);
		table->rec[counter].discount = buffer[0];
		fgets(buffer, 20, fp);
		table->rec[counter].balance_due = atof(buffer);
		fgets(buffer, 20, fp);
		table->rec[counter].order_out = atoi(buffer);
		++counter;
	}
	fclose(fp);
}
void listFile(struct myRecords * table)
{
	int x;
	for (x=0; x < 10; x++)
	{
		printf ("\n%i\n%s\n%s\n%c\n%f\n%i", table->rec[x].cust_id , table->rec[x].cust_name, table->rec[x].state, table->rec[x].discount, table->rec[x].balance_due, table->rec[x].order_out);
	}
}

Open in new window

0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 500 total points
ID: 35150945
Ok.

References to data items in the table will now look list this:


  customer_t  customer_table[10];

  customer_table[index].item;


So when you load the table (near line 49) the function header should be:

  void loadFile(FILE *fp, customer_t customer_table table[], char buffer[])


Line 55 should be:

 table[counter].cust_id = atoi(buffer);


The other references to table items should look similar.




0
 

Author Comment

by:reesee324
ID: 35151065
Ok I got it now. Thanks a lot for the help :) It is a lot easier to read it like that now!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now