I can't seem to remember everything I should about constructors. I'm looking for a way to create an array of structs, using a constructor. My code should explain.
struct myStruct
{
private:
int structInt1, structInt2;
I am having a lot of trouble being able to get data from a file and input it into given structs and arrays of structs and then outputting the file. We are given a file that contains 96 lines and looks like this:
Arzin, Neil 2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6 Babbage, Charles 2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5
This file continues for 24 different people and then repeats with different scores (the second line). The first number, in this case is 2.3 for both people is a difficulty rating. The next 6 numbers are scores.
We are given this data in order to set up our structs and arrays and my code:
#include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cmath> using namespace std; int main () { ifstream inFile; inFile.open("C://diveData.txt);
I have an assignment where I need to use pointers to do a few things and I am a little confused on the syntax of it all. My question is how do you use a pointer to point to an array of structs.
For example
struct info{ char firstName[15]; char lastName[15]; }; main() { info person[4]; cout << "The third letter of the second persons first name is: "; // ????? }
how would I define a pointer to point to the third letter of first name the second person in the "person" array.
what I am trying to do is to pass to a function the address of an array of structs, so I can later modify the items within the struct, within the array
Code: typedef struct { //A struct of name auctionint bidder;float bid;} auction; void myFunction (auction * auctionItem[]){(*aucItem[x]).bid = y;(*aucItem[x]).bidder = z;} int main(){auction theItems[10]; myFunction(theItems);} Where x, y, and z can be any number.
When I try to run my code the IDE (I'm using Code::Blocks 12.11) does not give me any errors, but it does give me a warning:
warning: passing argument 3 of '<function name>' from incompatible pointer type [enabled by default]
and the note:
note: expected 'struct <struct name> **' but argument is of type 'struct <struct name> *'.Also, when I run the program, it will crash and return garbage.
I have a structure product_array *pa that contains a pointer *arr to an array of structs and count that adds 1 when a new product is added (set to NULL initially). I have to write a function which adds a new product entry to that array. One product entry has *title, *code, stock and price parameters. The array is dynamically allocated and I’m supposed to:
1. Reallocate space for array. 2. Update product_array. 3. Initialize it.
Also, code should be truncated to 7 characters.Products can be added multiple times, so the initial size is unknown.
Code:
void add_product(struct product_array *pa, const char *title, const char *code, int stock, double price) { for (int i = 0 ;; i++){ pa->arr = realloc(pa->arr, sizeof(struct product_array));
Where course code is the 4 numbers after the name together and the term is the letter and year in the last two pieces of info. I got this to work:
int main() { FILE *p; p = fopen("input.csv", "r+"); if(p == NULL) { puts("The file could not be opened");
[Code] ......
But lets say i dont know how many lines i have in my file and i want to count them and then use that size for my array so i tried this by:
int main() { FILE *p; int lines = 1; char ch; p = fopen("input.csv", "r+"); if(p == NULL) {
[Code] .....
But the second program is not working for unknown reasons. I do not get any errors but its not scanning the info because when i print the info later on it prints out random symbols.
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.
void readFile(struct course *d, char* filename){ FILE* fp; char buffer[100]; int i = 0, array_size = 100; struct course *temp;
[code]....
I will be using this to read data from a file. I start with an array of 100 structures being passed to the readfile function. Once it reads 100 lines (i == array_size), I want to double the array size until I have finished reading the file.
Two questions.
1)My initial thought was that I needed to keep track of the lines read with my variable, i. However, is there a better way?
2)My program is crashing right now at the call to double_array_size function. What is wrong with my code? Never dealt with dynamically allocated array of structures and functions.
I read online that I should change my code in the following manner.
I can paste the "error messages" if you like, but it is a page full of stuff I have never seen. glibc detected, Backtrace, Memory Map, and a bunch of numbers and hexadecimal stuff like addresses.
And I want to make adding,deleting, and searching functions.Something like
Code:
void add();//Add name and address to a file, //and add more to the same file if I want to. void del();//Delete or Change some neighbor's name or address //in the same file(Can I?) void search();//Search name and show detail
So I started to code adding function first, but I don't know that I need to use pointer to code each functions relations, and I don't know how to check that my input's already exists yet. But I started some code below...
Code: void add() { int i=0; FILE *fp = fopen("neighborhood.txt", "at"); if ( fp != NULL ) { do{
I am increasing my array in multiples of 5 (or in this case, my constant BLOCK). This is the result of my printout (t1-t40 is correct, the rest of the values should be 1):
t1 1 1 t2 1 1 t3 1 31329 t4 31297 1 t5 1 31249 t6 31217 1 and so on .....
I noticed that in my test, realloc was called 7 times. My array has 7 distorted records. Each time I call realloc, it is distorting one of my records.
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
i am writing this bank accounts program using structures. i haven't implemented the function before that i want to check if the data is being read and printed. When i build and run the program in visual studio it gives me the following error. "No constructor could take the source type, or constructor overload resolution was ambiguous". Now whats wrong in this program?
/* Bank Accounts Program */ #include <iostream> #include <string> #include <fstream> #include <cstdlib>//needed to use system() function using namespace std; const int MAX_NUM = 50; struct Name{
I am making a tictactoe program that requires me to have a 3 by 3 two dimensional array of integers, in which the constructor should initialize the empty board to all zeroes. The program complies, but it keeps outputting garbage values and i'm not entirely sure why, My print function isn't complete yet, since I want to print out the array in a tic tac toe format, but i'm more concerned with why it's printing garbage values, here is my code:
I want to pass an array to a constructor, but only the first value is passed--the rest looks like garbage. Here's a simplified version of what I'm working on:
#include <iostream> class board { public: int state[64]; board(int arr[])
[Code] ....
Why this doesn't work and how to properly pass an array? Notes: I don't want to copy the array. Also, I'm using MSVC++ 2010 Express.
I am currently practicing designing classes. In one exercise, I am trying to store 15 words in an array, and randomly print one (using the rand() functions and seeding it with crime. I have a header file, a respective .cpp file, and a main .cpp file. When I try to compile the code using g++ GuessWord.cpp UseGuessWord.cpp -o UseGuessWord, I get the following error in my constructor: expected primary-expression before ‘{’ token
One can initialize a dynamically created array in the following way:
unsigned int * vec; // ... do something to vec double * a = (double *) malloc(4*sizeof(double)); a = (double[3]){(double[3]){0.0,10.0,20.0}[vec[0]],
[Code] ....
While there is no compilation error for the first assignment, the memory a is pointing to seems to change, surprisingly to me. This seems to solve the problem though:
memcpy(a, (double[3]){(double[3]){0.0,10.0,20.0}[vec[0]], (double[3]){1.0,2.0,3.0}[vec[1]], (double[6]){-2.0,-1.0,0.0,1.0,2.0,3.0}[vec[2]] }, 3*sizeof(double)); // NO C COMPILER ERROR
What does the first assignment do and why does it cause memory to change later in the program?
I know everything works except my copy constructor! There are no errors. The program crashes when it goes to access the copy constructor. why the copy constructor isn't working?
#ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS #endif #include <cstring> // access to C str functions #include "String.h" // access String class using namespace std; String::String( int size )// default constructor
Need a C++ constructor to initialize each members of an array. how to give value for for each elements of an array declared as a class object according to the users input.
I am making a tic tac toe program in which they are asking me to have a 3x3 2 dimensional array of integers and have the constructor initialize the empty board to all zeros. They also want me to place a 1 or 2 in each empty board space denoting the place where player 1 or player 2 would move The problem I'm having is, my code initializes the board to all zeros for each element of the array, and prints it out just fine, but I can't figure out how to re-initialize the values in the array to show where each player moves on the board... I was thinking about having a default constructor that has each value set to zero, and then use a setGame function that can change the values on the board to one or two depending on where the player moves....but I don't know if that's possible.....