C++ :: Array Of Structures - Storing Product Information

Nov 5, 2013

I have been dealing with this problem for quite some time, I've been assigned to develop a program that can store products' information within structures (the number of products is undefined). I thought I should use an array of structures, but I don't know how to declare it properly. This is what I thought would work:

struct product {
string name;
string in_stock;
float sale_cost;
int id; }
prod [n]; //n being the undefined number of products the user will register/delete/modify

I already saw an example of array of structures, but it uses a defined number.

View 13 Replies


ADVERTISEMENT

C :: Storing Array Of Structures

May 4, 2013

So i have a program with the structure

Code:

char record[20];
typedef struct{
int id;
char title[20];
char subject[20];
char faculty [20];
int level;
char fname[25];
}report;
int recno,i;
report list[100];

I need to store the hundred records into a file. What are the functions and elements i should use/introduce.

View 2 Replies View Related

C++ :: Incorporate Array Of Structures To Read User Input Then Reprint That Information To Screen

Nov 18, 2014

I have to create a program that incorporates an array of structures in order to simply read input from the user and then reprint that information to the screen. This is a fairly simple problem, but the issue is that the professor requires that we solve it in a very specific way.

I must have three functions in total: one to receive input from the user, one to print the received values, and of course the main function which calls the other two. Also, the input and output functions must be defined using the following prototypes:

void inputData(Component C1);
void printData(Component *C1);

Where Component is a structure defined above as:

typedef struct// Component {
float cost;
int code;
char name[30];

[Code] ....

I know that this problem could be easily solved by simply passing the entire 'comp' array to the inputData function, or passing a pointer to it, or having it return a pointer, but I am not allowed to do any of these things. I must use the function as defined by the prototype.

View 3 Replies View Related

C++ :: Dynamic Data Structures And Information Input

Feb 17, 2013

I want to have input data stored in a database. All information entered is to be stored under a single block. That way whenever I want to recover that information all I have to do is point to that block. Think of it like this. I want a large square block that can hold certain information. Inside of that large block are a finite amount of smaller blocks in which the user entered information about a single subject. Lets say block 1 has a name, address, and phone number. The second block will have the same TYPE of information, but different values as entered by the user. I.E:

BLOCK 1: Name 1, Address 1, Phone 1
BLOCK 2: Name 2, Address 2, Phone 2
etc.

The large block contains all of the smaller blocks, we will name the large block "Address Book" ... How would I implement that? Of course it would be just a simple console program that would erase all information when the program shut down, but I've been trying to figure out how to do it, but just can't seem to grasp a good notion of how to do it. What would be the best way to do that? Structures? How would I make it dynamic so that I wouldn't have to define every block, so that it would be added on the fly when someone inputs new information.

View 2 Replies View Related

C++ :: Create Structures To Store Information Of The People To Be Matched

Nov 9, 2014

I need to be able to make matches from the data i input, and also classifying between male and female. matches for person with specific traits (not one in the program) i really tried for the first part and now the rest is mentally disturbing me. and also to generate a report of partners found.

#include<string.h>
#include <iostream>
using namespace std;
char username[20];
char storedusername[20];
char password[20];
char storedpassword[20];
int loginresult;
char name[20];

[Code] .....

View 1 Replies View Related

C++ :: Storing Information In String Without User Input?

Oct 9, 2014

Is it possible to store information in a string without user input? If so, what code would I use for it?

View 2 Replies View Related

C++ :: Storing Numbers Into 2D Array - Invalid Types Int For Array Subscript

May 17, 2014

#include <iostream>
#include<fstream>
int decryption(int);
int multiply(int,int[][2]);
using namespace std;
main(){
int n;
ifstream inFile;
inFile.open ("out.txt");

[Code] .....

I was trying to store numbers read from a text file into 2D array but I am getting the error above.here is where the error occurs:

line 33: cout<<m[i][j]<<" ";

View 4 Replies View Related

C :: Array Containing Pointers To Structures?

Aug 9, 2013

How do I store pointers to a struct in an array ? I am using sprintf to concatenate some values together and then output it to an array in its 1st argument. A portion of my code is shown below.

Code:

int count = 0;
struct addx {
int a;

[Code].....

View 1 Replies View Related

C/C++ :: Initializing Array Of Structures

Feb 25, 2014

How to initialize this array of structures in my code

struct CustomerAddress; {
string street;
string city;
string state;
int zip;

[Code] ....

I am going to be using a boolean variable to mark whether or not a specific field has had data entered into it. I figure the best way to do that is to initialize all the elements of the structures to 0. However, with strings and with the nested structure, I'm not sure how to do this.

View 6 Replies View Related

C++ ::  Storing Entire One Array Into One Cell Of Another Array

Jul 31, 2013

Take for example two arrays.

int A[2][2][2] = { {{1, 2}, {3, 4}},{{ 5, 6}, {7, 8}} };
int B[1]

I want to store the entire array "A" into B[1]. Is it possible?

View 2 Replies View Related

C :: Reading File To Array Of Structures

Jun 9, 2013

I have to write a program that reads from a text file, which contains a list of stock hourly prices and the company names. Something like this:

78.52 82.56 75.10 71.97 Water Company
22.40 25.68 21.37 22.96 Mega Shipping Inc

There's suppose to one array of companies, where each company will be kept in a structure that contains: a pointer for the name, an array of the prices, and the average price for the day. The structures will be kept in an array of structures.

My question is, how do I read the data from the file and put the data from each line into the next structure in the array of structures? I read the numbers in fine. I just use:

Code: fscanf(fp, "%f", &companyAry[i].hourlyPrices[j]);

But my program crashes when I try to read the name.

Code: fscanf(fp, "%[^]", &companyAry[i].companyName);

I'm thinking it has something to do with the fact the companyName is a pointer.

My structure looks like this:

Code:

typedef struct {
char *companyName;
float hourlyPrices[NUM_PRICES];
float avgPrice;
}
COMPANY;

View 8 Replies View Related

C :: How To Find Duplicates In Array Of Structures

Jul 21, 2014

I have an array of structures with structure def that looks like.

Code:
struct {
char * name;
char * school;
int age;
}

there are multiple people with same name but different ages so i want to list them like.

Name.
age 1
age 2 and so on

The array is sorted by name already.

View 3 Replies View Related

C++ :: Read In Name Of A Vendor Into Array Of Structures

Feb 26, 2013

so im trying to read in the name of a vendor into an array of structures. and write the name into a file.

#include <iostream>
#include <cstring>
#include <cctype>
#include <fstream>
using namespace std;
struct booth

[code]....

View 5 Replies View Related

C++ :: How To Delete Elements In Array Of Structures

Apr 4, 2013

Any example to delete an element in an array of structure?Is it same as deleting an element in an array.ie like this....???

// to delete an element in an array
#include<iostream.h>
#include<conio.h> // for clrscr() and getch()
void main() {
clrscr();
int a[50],n,t,index,i;

[Code] .....

View 1 Replies View Related

C/C++ :: How To Add Info To Array Of Nested Structures

Dec 4, 2014

I am making a basic music library that stores information rather than files. I need to read from a .txt file the artist's name, the album name, the song name, and the song length. What I am confused about is storing the information in an array of Artist structures.

struct Song{
string songName;
int songLength;
};
struct Album{

[Code] ...

How to read the information into an array of Artists. Also, how would I be able to figure out if the artist already exists in the array when adding a song and if the artist does, add the album under the existing artist.This is how I am reading in the info:

Artist newArtist;
Album newAlbum;
Song newSong;
ifstream inF("library.txt");
while(!inF.eof()) {

[Code] ....

View 1 Replies View Related

C/C++ :: Using (delete) Operator On Array Of Structures?

Feb 27, 2013

Suppose I have the following structure

struct b
{
char fullname[100];
char title[100];
bopname
};

and i declare a pointer to the struct as follows

b* bp;

and at runtime,

bp = new b[5];

In the end, is the statement delete[ ] bp enough to release the dynamically allocated memory. Or do i need to separately delete bp[0],bp[1]...

Does delete[ ] bp indicate that the array[ ] pointed by bp has to be deleted?? [I am confused as to how this happens, since the pointer only points at the 1st struct address]

View 2 Replies View Related

C/C++ :: How To Initialize Array Of Structures At Runtime

Aug 26, 2014

I somewhere read "You cannot initialize a structure like that at run time."

Example:
struct item_info {
      char itemname[15];
      int quantity;
      float retail;
      float wholesale;

[Code] ....

But if you want to assign values at run time then you have to do it manually like:

strcpy(item[0].itemname, "rice");
item[0].quantity = 10;
item[0].retail = 40;
item[0].wholesale = 30;

I tried in internet but am unable to know the differences. I want to know the difference between those two in terms of run time and compile time. Explanation required also for below one. Is this run time or compile time? How does we actually decide which is run time and which is compile time!

struct item_info {
      char itemname[15];
      int quantity;
      float retail;
      float wholesale;
      //int quatityonorder;

[Code] ....

View 3 Replies View Related

C++ :: How To Read Information From Text File To Array

Apr 4, 2014

Perhaps my original wording on this was confusing so I will ask a different way.

if I have a text file called

data.txt

and it has the following in it.

12345
67890
12345
67890

how would i read this information into an array called

int data[4][5]

This is in C.

View 10 Replies View Related

C/C++ :: Printing Struct Array After Inputting Information?

Aug 28, 2014

I am doing a data structures program on C++ that is suposse to consist of declaring an array of size five for my credit card structure, that allows the user to input information of five diferent credit cards. I am having a problem with my program, because it runs but after I input the information of the credit card, it does not print out the values that I had input.I dont know where I went wrong with the code or if it is that my approach to solving it is wrong. My code is the following:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct TarjetaCredito

[code].....

View 2 Replies View Related

C :: Reading Data File Into Array Of Structures?

Nov 2, 2014

I'm reading the following file:

Osgood,Marcus 298542123 CHM FR mosgood@whatever.edu
Cronk,Melissa 873489021 BIO SR mcronk@whatever.edu
Pry,Seth 349908431 MTH SO spry@whatever.edu
Langlais,Susan 783323545 ME SR slanglais@whatever.edu
Davis,Nicole 987543345 PHY FR ndavis@whatever.edu

It's supposed to split it up into name, ID number, major, year, and email. The file reads it without any errors, and assigns name to the first part of the structure. However, ID gets assigned the ID, major, year, and email. Then Major gets assigned major, year, and email. Year gets assigned year and email, while email just gets assigned email. I don't know if it has something to do with the loop. For example, this is what I get what I print just the name and the ID.

Cronk,Melissa 873489021BIOSRmcronk@whatever.edu

Pry,Seth 349908431MTHSOspry@whatever.edu

Langlais,Susan 783323545ME

Davis,Nicole 987543345 PHYFRndavis@whatever.edu

Anyway. This is my function code for reading the array. I have it printing the ID number just to see if I can catch the errors earlier:

Code:
#include <stdio.h>
#include <string.h>
#include "functions.h"
void read_data(char filename[], studentinfo_t s[], int *size) {
FILE *infilep;
int countstudents = 0;
infilep = fopen("student_data", "r");

[Code].....

the name is reading correctly, but not anything else!

View 1 Replies View Related

C :: Allocating And Freeing Memory For Array Of Structures

Jul 27, 2014

I have to allocate memory for an array of structures, and my structure looks as following:

Code:
typedef struct {
char *name;
Uint *start_pos;
Uint len;
}
example_struct;

And now I want to allocate memory, for a variable number (so an array) of example_struct, so I first do:
Code:

example struct *all_struct;
int total_num = 3;
//will be set somehow, but for the example I set it on 3 all_struct = malloc (sizeof(example_struct) * total_num);

And now, as far as I now, I will have to allocate for each field of the structure memory, in order to be able to use it later. But I have problem at this point, a problem of understanding:

- I just allocated memory for 3 structures, but don't I have to allocate then memory for each structure separately, or can I just now allocate the fields like this:

Code: all_struct[0].name = malloc.....

But if yes, why the hell this works...

View 10 Replies View Related

C :: Significance Of Int Casting In Program With Array Of Structures

Jun 18, 2013

I am doing an exercise which has to do with International country codes.The user must give a code and the programm will display the corresponding country.

Code:
#include <stdio.h>
#define COUNTRY_COUNT
((int) (sizeof(country_codes) / sizeof(country_codes[0])))
[code]....

View 14 Replies View Related

C :: Array Of Structures And Passing Pointers To Structs

Feb 24, 2013

Background: I'm writing a convolutional encoder (and decoder, eventually) for a microprocessor (PIC24), for which I'm using structs and pointers to move from state to state. So far as I'm aware, everything I'm using in the PIC involves nothing other than ANSI C.

I have a little experience with structures, having written a linked-list program for a class a couple years back, but nothing since and never used structure arrays. I have the feeling I'm missing something basic here, which is what's so frustrating. The most confusing error (and I suspect the root of most of them) is the 'state undeclared', which I just can't figure.

The errors I'm getting are:

encoder.c:11: warning: 'struct memstate' declared inside parameter list
encoder.c:11: warning: its scope is only this definition or declaration, which is probably not what you want
encoder.c: In function 'state_init':
encoder.c:22: error: two or more data types in declaration specifiers
encoder.c:25: error: 'state' undeclared (first use in this function)
encoder.c:25: error: (Each undeclared identifier is reported only once

[Code]....

Code:

Code: //Includes
#include <stdlib.h>
//------------------------------------------------------------------------------
//Creates state machine and passes back pointer to 00 state
void state_init(struct memstate* startpoint)
{
extern struct memstate
{
char output0; //output if next input is 0

[code]...

NB: I'm aware that at the moment, this code will do nothing except spin round that do-while loop. Once it's actually compiling I'll drop in some simple button-based test code so it'll check for the correct output.

View 9 Replies View Related

C++ :: Putting Text File Into Array Of Structures

Feb 14, 2015

I am at a loss with an assignment. I am supposed to read from a text file, with an input of something like this: alphaproleone,stroke,42 1 and Store it into an array of structures and then output it with each word/number starting on a new line. My current code prints out only the first part, and the "a" in alphaproleone is the actual number "21".

#include <iostream>
#include <string>
#include<fstream>
#include<iomanip>
using namespace std;
typedef struct drugtype {
string name, target;
int effectiveness, toxicity;

[Code]...

View 1 Replies View Related

C++ :: Employee List That Holds Information - Add Or Delete Array?

Mar 26, 2013

So I'm trying to make an employee list that holds the information so I can then delete or add from that array. I'm getting some errors regarding overloading function.

#include "employeelist.h"
const Employee & employeelist::operator [](int index) const {
return ( (this->Employees)[index] );
} Employee & employeelist::operator [](int index) {
return ( (this->Employees)[index] );

[Code] .....

View 2 Replies View Related

C/C++ :: Array Of Structs To Store The Information From Input File?

Mar 10, 2015

I'm trying to read the data from a file i/o and put them into an array of structs. But when I run the program, it gives me a bunch of "garbage" as an output. my code and see

#include<stdio.h>
#include <stdlib.h>
#include<string.h>

[Code].....

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved