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


ADVERTISEMENT

C++ :: Constructor Not Initializing Array?

Mar 6, 2015

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:

Code:

// header file
#include <iostream>
#include <array>

[Code] ....

View 7 Replies View Related

C++ :: Initializing Double Pointer Array?

Aug 1, 2013

How to initialize double pointer array..?

int (**aLines)[2];

View 3 Replies View Related

C++ :: Initializing 2D Dynamic Array With All Entries 0?

Dec 29, 2013

is it possible to create a dynamic array with all entries in it equal to 0?

int** adjMatrix;
adjMatrix= new int*[numOfVertices];
for(int i=0; i<numOfVertices; i++){
adjMatrix[i]=new int[numOfVertices];
}

View 1 Replies View Related

C++ :: Initializing Local One Dimension Array?

Jul 31, 2013

Want to initialize a local one dimensional array. How can I do the same without a loop?

Found from some links that
int iArrayValue[25]={0};
will initialize all the elements to ZERO. Is it?
If yes then can we write below code to initialize the elements to a non-ZERO value?

int iArrayValue[25]={4};

Do we have some other way to initialize an array to a non-ZERO value? Memset can initialize the element to ZERO.

View 7 Replies View Related

C++ :: Initializing Multidimensional String Array?

Nov 15, 2014

In one of my programs I have a 3x3 array of string that I use to display the outcome to rock, paper, scissors, and another 1x3 used for a number guessing game. I have the arrays declared in the header file as follows:

//Games.h
std::string rpsOutcome[3][3];
std::string hiLoOutcome[3];

and then initialized them in the cpp file:

//Games.cpp
string rpsOutcome[3][3] {
//row 1
{ "Both of you picked rock, you tied. Try again",
"You picked rock, the computer picked paper, you lose",

[code]....

From what I've read, Im pretty sure thats how your supposed to initialize multidimensional arrays (using the nested braces), but when I build the project, I get the following error:

123456789101112
1
1> RockPaperScissors.cpp
1> Games.cpp
1>c:userswuubbgoogle drivevisual studio projectsgamesgamesgames.cpp(75): error C2374: 'games::rpsOutcome' : redefinition; multiple initialization

[Code] .....

View 4 Replies View Related

C++ :: Crash When Initializing Indices Array

Nov 26, 2012

Why I get a crash when initializing the indices array below (bottom)-

Code:
struct TerrainGroupData {
TerrainGroupData(){};
TerrainGroupData(Ogre::Vector3 ** iVertices, unsigned long ** iIndices, size_t* iFaceNum, size_t* iVertNum) {
vertices = iVertices;
indices = iIndices;

[Code] ...

View 9 Replies View Related

Visual C++ :: Initializing Array In Constructor?

Mar 8, 2015

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.....

here is my code

Code:

// header file
#include <iostream>
#include <array>
class tictac {
public:
tictac(int);

[code]....

View 8 Replies View Related

C++ :: Initializing Array Of Char Pointers Directly

Nov 24, 2014

I'm learning OpenGL using the C API and some of the functions' argument types have proven a bit challenging to me.

One example is the function Code: glShaderSource(GLuint shader, GLsizei count, GLchar const** string, GLint const* length); It resides in foo() which receives a vector "data" from elsewhere Code: void foo(std::vector<std::pair<GLenum, GLchar const*>> const& data); To pass the pair's second element to glShaderSource's third argument, I do the following:

Code:

GLchar const* const source[] = {data[0].second};
glShaderSource(..., ..., source, ...);

Now I have two questions regarding this:

1. Can I initialize a char const** via initialization list, the way I do a char const*?

Code:

// this works
std::vector<std::pair<GLenum, GLchar const*>> const shader_sources = {
{GL_VERTEX_SHADER, "sourcecode"},
{GL_FRAGMENT_SHADER, "sourcecode"}
};
// but is this possible?

std::vector<std::pair<GLenum, GLchar const**>> = { ??? };

2. Is there an alternative to creating a temporary GLchar**, even though that's specifically what the function wants?

View 2 Replies View Related

C :: Declaring And Initializing 3000 Number Array?

Oct 23, 2013

Here are the steps to this program:

1. Declare an array that will hold 3000 numbers
2. Initialize this array by assigning a random number to each element in the array
3. Traverse the array, modifying the current contents of each element in the array so that each value now lies between -3000 and 3000 inclusive
4. Traverse the array to compute the average value of all elements

I have never worked with arrays before and am lost!

View 2 Replies View Related

C++ :: Initializing N-dimensional Array With A List Of Components

Nov 7, 2014

InitializeNArray<4, bool, NARRAY>()(narray, {1,2,3,2}, true);

is supposed to initialize a 4-dimensional std::array a with a[1][2][3][2] = true.

The commented-out line, which is the desired recursion, does not compile for some reason, and the problem third parameter cannot be deduced. So I placed some temporary lines to work in the special case only. Howw to make that recursion work?

#include <iostream>
#include <array>
#include <vector>
#include <memory>
template <typename, int...> struct NArray;

[Code] ....

Incidentally, this is a sub-problem within the task of initializing more generally with

template <typename T, int... N, typename... ARGS>
typename NArray<T, N...>::type NDimensionalArray (const T& value, ARGS&&... args)

Once this subproblem is fixed, then the bigger task is fixed too.

View 7 Replies View Related

C++ :: Initializing Array Of String Type In A Class

Apr 28, 2013

I want to use this array as part of my class. I have tried several different angles trying to get it to work but with out success. I have been checking to see if it works by simply using "cout << dayName[3];" It is printing nothing at all. What is the proper way to initialize this array of strings?

First I tried this:
const string dayName[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

then I tried this:
const string dayName[7];
dayName[0] = "Sunday";
dayName[1] = "Monday";
dayName[2] ="Tuesday";
dayName[3] ="Wednesday";
dayName[4] ="Thursday";
dayName[5] ="Friday";
dayName[6] ="Saturday";

My implemetation file code and my header file code is here (header first):

//dayType.h, the specification file for the class dayType
#include <iostream>
#include <string>
using namespace std;
class dayType{

[Code] .....

View 4 Replies View Related

C++ :: Global Char Type Array Initializing

Mar 20, 2015

I am not getting any error below

--------
#include <iostream>
using namespace std;
void main()
{
char sqr[3];
sqr[0] = '1';
sqr[1] = '1';
cout << sqr[0] << sqr[1];
while (1);
}------------

but if I move my char type array before main , I get error l

----------------------------
#include <iostream>
using namespace std;
char sqr[3];
sqr[0] = '1';
sqr[1] = '1';
void main()
{
cout << sqr[0] << sqr[1];
while (1);
}
-------------------

View 8 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 :: 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 :: 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 :: 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







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