By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,946 Members | 2,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,946 IT Pros & Developers. It's quick & easy.

Help for C++

P: n/a
AKF
I am trying to use data structures to open a file (void
display_Stock_Item(Stock_Item si);) from a text file. I also need to
be able to write to the file and also to delete from the file using
arrays. Can anyone help?
/*Declare header files to be used in the program*/
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>

const int SIZE=20;

//Declaring structures to be used in the program
struct Stock_Item;
struct Bill;
struct Bill_Item;
struct Stock;

struct Stock_Item //Function prototype for a Stock data type
{
int code;
char description[25];
float price;
};

struct Stock
{
Stock_Item si[SIZE];
int nStock_Item;
};

struct Bill_Item //Function prototype for a Bill_Item data type
{
int code;
char description[25];
float weight;
float cost;
};

struct Bill //Function prototype for a Bill data type
{
Bill_Item bi[SIZE];
int nBill_item;
};
//Function Prototypes available to the data structure
void add_Bill_Item(Bill_Item& bi, int* code, char* desc, float* w,
float* c);
void set_Stock_Item (Stock_Item& si, int* code, char* desc, float* p);
void display_Bill_Item(Bill_Item bi);
void display_Stock_Item(Stock_Item si);
void create_Stock_Item(Stock_Item& si);
void create_Stock(Stock& s);
int isfull(Stock_Item& si);
//initialising Stock Items to zero for the array
void create_Stock(Stock& s)
{
s.nStock_Item = 0;
}

//Setting stock_Item

void set_Stock_Item(Stock_Item& si, int code, char desc, float p)
{
cout << "Enter a code:";
cin >> si.code;
cout << "Enter a description:";
cin >> si.description;
cout << "Enter a price:";
cin >> si.price;
}

//Display Stock_Item structure operations
void display_Stock_Item(Stock_Item &si, int c, char d, float p)
{
si.code = c;
si.description[SIZE] = d;
si.price = p;
cout << si.code << si.description << si.price <<endl;
}
//Setting Bill_Item

void add_Bill_Item(Bill_Item& bi)

{
cout << "Enter a code:";
cin >> bi.code;
cout << "Enter a description:";
cin >> bi.description;
cout << "Enter a weight:";
cin >> bi.weight;
cout << "Enter a cost:";
cin >> bi.cost;
}
//Display Bill_Item Structure Operations
void display_Bill_Item(Bill_Item bi)
{
cout << bi.code << bi.description << bi.weight <<bi.cost <<endl;
}
//*****************THE ACTUAL PROGRAM STARTS HERE**************

void main()
{
// Create a pointer of type FILE and initialise to NULL
// FILE is a data structure in stdio.h
FILE *per_file = NULL;
char temp;
// Open the file for reading
// Parameter 1 = filename and path if required
// Parameter 2 = r (read mode)
per_file = fopen("C:\\prices.txt","r");
//While temp is equal
while
((temp=fgetc(per_file))!=EOF)
cout<<temp;

// Always close a file after use
fclose(per_file);

}
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
AKF wrote:
I am trying to use data structures to open a file (void
display_Stock_Item(Stock_Item si);) from a text file. I also need to
be able to write to the file and also to delete from the file using
arrays. Can anyone help?
Yes, probably, but where do you have problems?
I dont really know what help you expect. I have only a few
comments about your code.

/*Declare header files to be used in the program*/
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
Please use this headers instead:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>

Please be advised, that various functione live now in
Namespace std. So you also need to add:

using namespace std;

In general, reading through your code I had the feeling,
that you are programming C. C and C++ have very different
ways of programming.
I suggest you read a book about object-oriented design in C++.
const int SIZE=20;

//Declaring structures to be used in the program
struct Stock_Item;
struct Bill;
struct Bill_Item;
struct Stock;

struct Stock_Item //Function prototype for a Stock data type
{
int code;
char description[25];
float price;
};

struct Stock
{
Stock_Item si[SIZE];
int nStock_Item;
};

struct Bill_Item //Function prototype for a Bill_Item data type
This is no function prototype, your comment is completely
misleading.
{
int code;
char description[25];
float weight;
float cost;
};

struct Bill //Function prototype for a Bill data type
ditto
{
Bill_Item bi[SIZE];
int nBill_item;
};
//Function Prototypes available to the data structure
void add_Bill_Item(Bill_Item& bi, int* code, char* desc, float* w,
float* c);
void set_Stock_Item (Stock_Item& si, int* code, char* desc, float* p);
void display_Bill_Item(Bill_Item bi);
void display_Stock_Item(Stock_Item si);
void create_Stock_Item(Stock_Item& si);
void create_Stock(Stock& s);
int isfull(Stock_Item& si);
//initialising Stock Items to zero for the array
void create_Stock(Stock& s)
{
s.nStock_Item = 0;
}

//Setting stock_Item

void set_Stock_Item(Stock_Item& si, int code, char desc, float p)
{
cout << "Enter a code:";
cin >> si.code;
cout << "Enter a description:";
cin >> si.description;
cout << "Enter a price:";
cin >> si.price;
}

//Display Stock_Item structure operations
void display_Stock_Item(Stock_Item &si, int c, char d, float p)
{
si.code = c;
si.description[SIZE] = d;
si.price = p;
cout << si.code << si.description << si.price <<endl;
}
//Setting Bill_Item

void add_Bill_Item(Bill_Item& bi)

{
cout << "Enter a code:";
cin >> bi.code;
cout << "Enter a description:";
cin >> bi.description;
cout << "Enter a weight:";
cin >> bi.weight;
cout << "Enter a cost:";
cin >> bi.cost;
}
//Display Bill_Item Structure Operations
void display_Bill_Item(Bill_Item bi)
{
cout << bi.code << bi.description << bi.weight <<bi.cost <<endl;
}
//*****************THE ACTUAL PROGRAM STARTS HERE**************

void main()
int main()

main is required to return int.
{
// Create a pointer of type FILE and initialise to NULL
// FILE is a data structure in stdio.h
FILE *per_file = NULL;
char temp;

In C++ you normally use a stream to read from a file.
// Open the file for reading
// Parameter 1 = filename and path if required
// Parameter 2 = r (read mode)
per_file = fopen("C:\\prices.txt","r");
//While temp is equal
while
((temp=fgetc(per_file))!=EOF)
cout<<temp;
Dou you have a problem here with parsing the input?
// Always close a file after use
fclose(per_file);

}

hth at least a little bit,

Christoph
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.