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()) {
I'm trying to make a program that you input your login info and it writes that info to a text file. Then, later on once I get my problem fixed, the program will read the info to the user. my code is as follows(the input part is a bit lengthy):
Example : Code: struct x { int v[4]; }; const x test = { 0, 1, 2, 3 };
Why can I do this? How does the compiler know to write to this in the proper way? I get that v would be contiguous. Does that have something to do with it?
I wrote this code, and everything was working well, but part of the assignment is that it must include nested loops. Once I added the nested while loop, which is basically an if statement, my life was ruined. I am trying to nest a loop in the code that will basically tell the compiler that if the value "loopVol" were to exceed the value of "final" after adding an increment, to run the program for the "final". How can I do that?
Example:
initial = 10 final = 123 increment = 10
as of now, the program would stop at 120, but I want to nest a loop that will tell the compiler to calculate at the final if this happens.
I've been able to write the program completing all requirements except for one... creating a function that accepts a nested structure array. Here's the program:
This is a test program that takes a number of arguments from the command prompt and concatenates them into a string object. I was looking into the possibility of using the range-based for loop for this purpose. Can it be done with pointer based arrays? I am mainly doing this because I want to have a firm understanding of range-based for, but also would like to do this with least amount of code possible.
This is my working program:
#include <string> #include <iostream> int main(int argc, char *argv[]) { if (argc > 1) { std::string concatenatedArgs;
[Code] ....
Can I somehow replace my while-loop with a range-based for? I tried the following but the compiler points out that begin and end were not declared in the scope of the range-based for loop.
#include <string> #include <iostream> int main(int argc, char *argv[]) { if (argc > 1) { std::string concatenatedArgs;
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.
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.
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:
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]
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!
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:
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:
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.
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.
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;
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.
I want to do this but do not know exactly how to do it:
Create an array of space ship objects and write a program that continually updates their positions until they all go off the screen. Assume that the size of the screen is 1024 pixels by 768 pixels.
I have begun to write a bit code but the problem is when you want to see the space ship moving step for step on the screen I do not know how to create the ship visible so you can see it is moving. Here's my code so far
Code: #include <iostream> using namespace std; struct SpaceShips { int x_coordinate; int y_coordinate;
I have run across what I believe to be a syntax problem which I don't understand. I have a structure with two character array and I need to be able to change the size of those array dynamically. I have to use character arrays and I think the dot notation. I am not sure if I can use arrow notation. I can not do this problem using strings and vectors.
#include <iostream> #include <cstring> using namespace std;