C# :: Adding To Array From Different Class
Jan 10, 2015I have an inventory array in a class called inventory. This class is in a different program. How do I access and add to this array for my main program?
View 1 RepliesI have an inventory array in a class called inventory. This class is in a different program. How do I access and add to this array for my main program?
View 1 RepliesI had written a "DLL" in VB.NET a year or two ago to read and set MP3 tags. I want to write this exact same library in C++ so I can convert it in my droid project, and to get a hands on introduction to C++. So far this attempt has been a total mind melt!
I am finally wrapping my head around .cpp and .h files so there is light at the end of this tunnel. Here is my problem now:
I wrote the VB project with properties for each tag in the MP3 file, then I could get and set them - easy stuff.
When I try this in C++ I get compile error: a property can only appear within the definition of a managed type. I can usually stumble through Google searches and figure out this type of stuff on my own, but this one is stumping me -- I think I am missing some fundamental stuff here.
My code:
// MP3Tags.h
#pragma once;
#include <string>
using namespace std;
using namespace System::IO;
[Code]....
I'd like to start out by adding an array to a C++ class. I'd like to be able to reference the array using a class object that I create, for example:
Class is Stone.
Stone Bob is an instance of "stone" that I name "Bob".
"Bob.array[1] = "granite";" tells the compiler that the second element in the array (with the first being the zeroth element) is a string containing "granite".
I'll eventually want to extend this to an n x m matrix within the "stone" class that can be referenced as: Bob.matrix[1][3]="lignite";
I tried to make this work using a text again and again last night to no avail. My code is below.
NOTE: Since I am dynamically allocating memory space, I'd like to avoid memory leaks when using this class with dynamically allocated arrays and matrices. Not sure how to do this. Also need some insight into "destructor", and why my simple version reduced to a comment below doesn't seem to please the compiler.
CODE FOLLOWS:
Code:
// AINOW.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using std:: string;
using std:: cout;
using std:: endl;
using std:: cin;
[code]....
I'm giving the replace option to my string:
void replace(string oldstring, string newstring) {
int stroldstringpos=b.find(oldstring);
b.replace(stroldstringpos,newstring.length(),newstring);
}
i have 1 error in these function that i'm confused. imagine the newstring size is more big than the oldstring, how can change the string, but only change the oldstring and add what left?
see these:
String test="hi hello world";
test.replace("hi","hello");
the result must be:
hello hello world
how can i change the replace function for it?
I am trying to add matrices a and b. I am getting an error in the "add" function, probably because I have m[i] in it, and m is not an array. What is the correct way of writing the "add" member function here?
Also, although the "read" and "write" member functions of the class are working just fine, do you think there is a better way of writing them?
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const int rows=3;
const int columns=3;
[code].....
how to add a list of information from a file to a vector of a class. Here is my code:
Champion_Info.h
#ifndef CHAMPION_INFO_H_INCLUDED
#define CHAMPION_INFO_H_INCLUDED
#include <vector>
#include <string>
[Code].....
How would I add my own function to the vector class?
View 3 Replies View RelatedI am trying to add data from a file that would go into a class that would later go into a vector of a class. I'm not really sure how to do it exactly. Here is the code:
Champion_Info.h
#ifndef CHAMPION_INFO_H_INCLUDED
#define CHAMPION_INFO_H_INCLUDED
#include <vector>
#include <string>
using namespace std;
class Champ_Info {
[Code] .....
I have a .cpp file which contains 5 smaller defined classes. In my missile class I have a default constructor and a constructor that I invoke
class Missile{
private:
bool isHuman;
[Code]...
My issue is when creating and adding the pointer object; it doesn't seem to create a new instance of the class-the Missile objects all share the same xPos value which is always the first xPos when the "fire" command is given. The "Missile *missile = new Missile(xPos, yPos, true);" line does not seem to create a new instance of the object with different variables but instead creates a new object with the same variables. Is it necessary for me to always make a separate .cpp and .h file for any class I want to create multiple instances of or can I keep the smaller classes in the same file and still create a new separate instance of the class?
I'm having trouble finishing this program. What I'm trying to do is create a class for Rational numbers. Get input from user for denominator and numerator. Store the numbers without a sign, and for the sign store it separately in char. I'm not supposed to use accessor functions. The part that I can't seem to know how to do is use the info that was stored in the addUp function. This function need to add two rational numbers. i need to make this function general enough that it can handle two fractions, or a fraction and a whole number, or two whole numbers. What I already have here is readin function which reads in the numerator and denominator. setnumerator and setdenominator to assign positive values. setsign should get the sign of the fraction. Finally addUp should addUp which I explained before. I have some ideas about writing the tests, but can't seem to know how to implement it to the program. The main program is still empty but all I'm supposed to do there is call the class functions.
Code:
#include <iostream>
using namespace std;
class Rational {
private:
int numerator, denominator;
char sign;
[Code] .....
How would one add each value from an array? I'm working from a string but I was wondering if there was a way to loop through the string and add each value. This is what I have so far:
#include <iostream>
#include <cmath>
#include <string>
int main() {
std::string numbers;
int sum;
[Code] ....
I am trying to convert a string Input by user into an unsigned byte array.The data would be in hex form like :- "AE 1F 2C". I am thinking of using strtok to split the string into be " ". and then cast to (unsigned char *) . But for that I''ll have to prefix every element with 0X Is there any convenient way and elegant way to do this?
View 3 Replies View RelatedI'm having trouble getting my array to add its values together. I have a similar program running fine with the exact same equation. I am not so naive as to believe that this means it should run with every program but I'm at a loss as to what to do.
#include <iostream>
#include <iomanip>
using namespace std;
[Code].....
When i run the program i want to add a big integer number into an int array. How can i do it.i don't want to use for loop.
View 4 Replies View RelatedI'm having some issues with adding a record to my array.
#include <iostream>
#include <fstream>
#include <string>
[Code].....
My program is suppose to be as a virtual phone book that allows you to add,search, display names and numbers.
At the beginning you are able to add up to 10 entries and then from there the program goes to a menu where you can add more entries, search etc.
My problem is that I am unable to add an entry into the existing list of names/phone numbers.
Example: At the beginning I add Joe,Albert,Barry. It sorts them into Albert, Barry, Joe (good so far!)
However, if I choose to add another entry (Carl) it becomes Barry,Carl,Joe.
The functions I am using to add entries are: GetEntries (for initial entries) and Addentries for more entries during the main program.
*******************************COPY OF CODE**********************************
#include <iostream>
#include <string>
using namespace std;
[Code].....
This code shows a loop inside a loop initializing a two-dimensional array named arr with elements [2] and [5]. Here's my code:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
[Code]....
Now, what I wanted to do is to have an output showing the sums of each elements. Example, the above code has an output of:
1 2 3 4 5
6 7 8 9 10
I wanted to have more codes which would add up the elements 1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10 and produce and output:
7 9 11 13 15
lets say we have a txt that contains this
| | | | |*| |
| | | |*| | |
and that symbolizes a 2x6 char array and we want to take only the symbols inside the || and place them in a 2x6 char array. how do we do that?
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));
[code]....
I would just like to share my code and wanted to do something about it. This code shows a loop inside a loop initializing a two-dimensional array named arr with elements [2] and [5]. Here's my code:
#include <iostream>
#include <conio.h>
using namespace std;
[Code]....
Now, what I wanted to do is to have an output showing the sums of each elements. Example, the above code has an output of:
1 2 3 4 5
6 7 8 9 10
I wanted to have more codes which would add up the elements 1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10 and produce and output:
7 9 11 13 15
The goal of this program is to take 4 neighboring elements in an array and add them together. The program asks user for the number of rows and columns to start out with and the program will then continue to print the board until 1 element remains.
I'm having problems getting my program to compile (line 19)
#include <iostream>
#include <cstring>
#include <cstdlib>
[Code].....
The book uses this example:
#define ALLOCSIZE 10000 /* size of available space */
static char allocbuf[ALLOCSIZE]; /* storage for alloc */
static char *allocp = allocbuf; /* next free position */
char *alloc(int n)
/* return pointer to n characters */
[Code] ....
The logic here I don't understand:
if (allocbuf + ALLOCSIZE - allocp >= n)
allocbuf is a char array allocated with 10000 positions. We add that to ALLOCSIZE macro which is 10000. Does that return a value of 20000? If so, it doesn't make sense to do this because all we had to do was this:
if (allocbuf - allocp >= n)
That takes length of allocbuf which is 10000 and subtracts allocp from it. So if allocp is 1000 we are left with 9000 available slots. And we can use that to check if we have enough to allocate n elements.
I want to write a program to record my neighborhoods's name and address by using an array of structs and file.
my array of structs is
Code:
#define SIZE 30
typedef struct{
char name[30];
char address[100];
}Detail;
Detail neighbor[SIZE];
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{
[Code]......
I am making a program to run Fibonacci series. I have created 2 array.
1)- 1st array holds only 0, 1
2)- 2nd array holds other values eg: 1, 2, 3, 5..........etc
I am using while loop to get the febonacci series and storing it in a 2nd array called int[] numbers.
After getting value using while loop, I am joing both the arrays using int[] final = arr.Concat(number).ToArray();
At last, I have used foreach loop to add the febonacci series into the listbox.
The problem I have is that, I cannot able to concat both the arrays. I tried to assign number array at the top of the while loop. so that number variable will be accessible outside the while loop. But I am getting a error.
See the code below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
[Code] .....
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
Please consider the following code :
#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.