Pointers, Struct, C++,Writing a new structure in a function - c++

Could someone explain me the following mistakes and tell me how to fix them (written as comments)?
The structure I have:
const int max = 1000;
const int MAX = 30;
struct student_t
{
int k;
char* name[MAX];
char** bez[MAX];
};
With this function I must write a member of this structure(members<=1000) and bez. (<=30) for every member:
int write (student_t *field[max], int i)
{
student_t *pointerfield= new student_t;
printf("Number:....");
std::cin>>pointerfield->number;
if (pointerfield->number>1 && pointerfield->k<999999)
{
if (binarsearch(&field[max],i,(pointerfield->number))!=-1)
{
return i;
}
else
{
printf("\nName:.....");
readstr();
pointerfield->name=readstr();
// insupportable types of allocation of int to char* [30]
std::cout<<std::endl;
int counter=0;
std::cout<<"Do you want to add bez.? j,n"<<std::endl;
char a;
std::cin>>a;
if(a=='j' && counter<MAX)
{
std::cout<<"Bez.:";
readstr();
pointerfield->bez[MAX]=readstr();
// char * can not be converted into assignment to char **
counter++;
}
// ...

Related

how do i initialize a public variable in a public class method

I have a public class in which I create an array, this array takes its size from the constructor and needs to be used in other functions (including int main). Therefore the variable must be public. my code looks something along these lines:
class myclass {
public:
int parameter1;
int parameter2;
myclass(int p, int p2) {
parameter1 = p;
parameter2 = p2;
}
void makeArray() {
int array[parameter1][parameter2]; //I want this array to be public as the next method needs access to it
}
void otherFunction() {
array[1][2] = 5; //just an example of what i need to do
}
}
Look up how to use pointers and dynamic memory..
To do what you want would be something like:
class myclass {
public:
int parameter1;
int parameter2;
int **a;
myclass(int p, int p2) {
parameter1 = p;
parameter2 = p2;
a = nullptr;
}
~myclass() {
// TODO: delete "a"
}
void makeArray() {
// TODO: delete "a" if it has already been allocated
a = new *int[parameter1];
for (int i = 0; i < parameter1; ++i) {
a[i] = new int[parameter2];
}
}
void otherFunction() {
// TODO: check that "a" has already been allocated
a[1][2] = 5; //just an example of what i need to do
}
}
You could also allocate the array in the constructor since you have the necessary information being passed in already.
This is more optimized way to do the same thing:
class myclass {
public:
int parameter1;
int parameter2;
int *array;
myclass(int p1, int p2) {
parameter1 = p1;
parameter2 = p2;
}
void makeArray() {
array = new int[parameter1*parameter2];
}
void otherFunction() {
// ary[i][j] is then rewritten as ary[i*sizeY+j]
array[1*parameter2+2] = 5;
}
};
int main()
{
int sizeX = 5;
int sizeY = 5;
myclass m1(sizeX,sizeY);
m1.makeArray();
m1.otherFunction();
cout << m1.array[1*sizeY+2] << endl;
return 0;
}

How to clear struct or create new one each time it called?

I have the following struct:
const int max_stu= 100;
const int max_name =100;
const int max_add=100;
struct stu{
int id;
char name[max_name];
char addr[max_add];
};
stu student[max_stu];
One of my friends already give me this code
struct stu{
int id;
char name[max_name];
char addr[max_add];
void remove(stu temp){
temp.id= 0;
temp.name= "";
temp.addr= "";
}
for (int i=0;i < max_stu; i++){
stu.remove(student[i]);
}
but it have problem in this part
temp.name= "";
because of the type of char and
max_name=100
and on this part
stu.remove(student[i]);
because its not a class. How can I fix this?

How can I access a member of a class from a function via pointer?

I wish to
create an array of class/struct items (c1)
then create an array of pointer to the original array (*cp1), which can be sorted
then access members of the class from within a function.
However I'm getting stuck at the initial function call.
Here's my basic code:
struct Car
{
int speed;
};
Car c1[5];
Car *cp1[5];
int main() {
for (int i=0;i<5;i++) {
c1[i].speed = i;
cp1[i] = &c1[i];
}
garage(cp1, 5);
}
void garage(Car **ar, int n) {
int p = (*ar[n / 2])->speed;
}
First of all, your garage function is not known to the compiler at the place where you call it, since it is defined below main. To fix it, either place the function definition above main, or introduce it with a prototype.
Second, at the line int p = (*ar[n / 2])->speed;, *ar[n/2] is not a pointer, so you should use . instead of ->, as in int p = (*ar[n / 2]).speed;
Funcion garage must be declared before you can refer it.
void garage(Car **ar, int n);
int main()
{
//...
}
void garage(Car **ar, int n) {
//...
}
Function main in C++ shall have return type int
int main()
{
//...
}
And within the function the correct expression will look
void garage(Car **ar, int n) {
int p = (*ar )[n / 2]).speed;
}
Or
void garage(Car **ar, int n) {
int p = ar[n / 2]->speed;
}
Or
void garage(Car **ar, int n) {
int p = ( *ar[n / 2] ).speed;
}
struct Car
{
int speed;
};
Car c1[5];
Car *cp1[5];
void garage(Car **ar, int n); // forward declare garage
int main()
{
for (int i=0;i<5;i++) {
c1[i].speed = i;
cp1[i] = &c1[i];
}
garage(cp1, 5);
}
void garage(Car **ar, int n) {
int p = ar[n / 2]->speed; // -> dereferences the pointer, you don't need to
}

How to declare the Char array of char variables already declared in class?

#include <stdio.h>
#include <iostream>
using namespace std;
//char* b[6] = new char[6];
char a[6] = {'b','c','d','e','f','g'};
char c[6] = {'a','b','d','d','f','g'};
int main()
{
char d[][6]={*a,*c};
for (int x = 0 ; x < 1; x++)
{
for(int y = 0; y<6; y++)
{
char test = d[x][y];
cout << test <<"\n";
}
}
return 0;
}
This code is C++ code. I am trying to create a class where it stores the char array. Then there is another char array of array storing already declared char variables. It compiles fine but it doesn't work out to as it should. It doesn't get me the right value that it should when the program tries to print the value
May be you meant array of pointers:
char *d[]={a,c};
typedef std::vector<char> VectorChar;
typedef std::vector< VectorChar* > VectorVectorChar;
struct V
{
V() : _v{ '0', '1', '2' } {}
VectorChar _v;
};
int main(void)
{
V arrV[5];
VectorVectorChar vvc;
for ( auto& v : arrV )
vvc.push_back( &v._v );
// print them
for ( auto pV : vvc )
{
for ( auto c : *pV )
cout << c << ' ';
cout << '\n;
}
return 0;
}
what i understood from the question that, you want to create class to store char array, which already initialized.
#include <stdio.h>
#include <iostream>
char a[6] = {'b','c','d','e','f','g'}; // Initialized character array. MAX 6
// This class will hold char array
class Test {
public:
void setName(char *name);
const char* getName();
private:
char m_name[6]; // MAX 6 ( since you already initialized(MAX 6), So no need dynamic memory allocation. )
};
void Test::setName(char *name) {
strcpy(m_name, name); // Copy, already initialized array
}
const char* Test::getName() {
return m_name;
}
int main(int argc, char** argv) {
{
Test foobar;
foobar.setName( a ); // set the pointer which point to starting of the initialized array.
cout << foobar.getName();
return 0;
}
char a[6] = {'b','c','d','e','f','\0'};
char c[6] = {'a','b','d','d','f','\0'};
char* d[]= {a,c};
for (int x = 0 ; x < 2; x++)
{
for(int y = 0; y < 6; y++)
{
char test = d[x][y];
cout << test << "\n";
}
}
return 0;

I can't display variables of different types included in array

I have to do a program for college.
I have 3 classes already declared in the statement of the problem.
First class:
class piesa_a{
protected:
int id;
char *tip;
int pret;
};
Second class:
class piesa_b:public piesa_a
{
private:
float lungime;
bool bw;
};
Third class:
class piesa_c:public piesa_a
{
private:
int nr;
piesa_b *buf;
};
In main I need to create an array in which to store items such piesa_a, piesa_b, piesa_c. Then I have to sort items by price.
I have this code so far: http://pastebin.com/nx2FGSfe
The program is incomplete because it does not displays each item in the array.
I got stuck here. But if you display the array's elements when they are outside of it, it works.
SHORT: I have an error on line 143 and I want to solve it.
main.cpp:143:18: error: request for member ‘afisare’ in ‘*(v + ((unsigned int)(((unsigned int)i) * 4u)))’, which is of non-class type ‘piesa_a*’
The code is here:
#include <cstdlib>
#include<iostream>
#include<string.h>
using namespace std;
class piesa_a{
protected:
int id;
char *tip;
int pret;
public:
piesa_a()
{
id = 0;
tip = new char[1];
pret = 0;
}
piesa_a(int aidi, char *typ, int pretz)
{
id = aidi;
tip = new char[strlen(typ)+1];
strcpy(tip,typ);
pret = pretz;
}
piesa_a&operator =(piesa_a alta)
{
id = alta.id;
tip = new char[strlen(alta.tip)+1];
strcpy(tip,alta.tip);
pret = alta.pret;
return *this;
}
virtual void afisare()
{
cout<<"\n Piesa A: "<<id<<" "<<tip<<" "<<pret;
}
};
class piesa_b:public piesa_a
{
private:
float lungime;
bool bw;
public:
piesa_b():piesa_a(){lungime = 0;bw = 0;}
piesa_b(float lg,bool bl, int aid, char *tipi, int pretzz):piesa_a(aid,tipi,pretzz)
{
lungime = lg;
bw = bl;
}
piesa_b&operator =(piesa_b &c)
{
id = c.id;
tip = new char[strlen(c.tip)+1];
strcpy(tip,c.tip);
pret = c.pret;
lungime = c.lungime;
bw = c.bw;
return *this;
}
void afisare()
{
piesa_a::afisare();
cout<<"impreuna cu piesa B: "<<lungime<<" "<<bw<<"\n";
}
};
class piesa_c:public piesa_a
{
private:
int nr;
piesa_b *buf;
public:
piesa_c():piesa_a(){nr=0; buf = new piesa_b[nr];}
piesa_c(int n, piesa_b *bu,int aid, char *tipi, int pretzz):piesa_a(aid,tipi,pretzz)
{
nr = n;
buf = new piesa_b[nr];
for(int i=0;i<nr;i++)
buf[i]= bu[i];
}
piesa_c&operator =(piesa_c &alta)
{
id = alta.id;
tip = new char[strlen(alta.tip)+1];
strcpy(tip,alta.tip);
pret = alta.pret;
nr = alta.nr;
for(int i=0;i<alta.nr;i++)
buf[i] = alta.buf[i];
}
void afisare()
{
for(int i=0;i<nr;i++)
buf[i].afisare();
}
};
int main(int argc, char** argv) {
piesa_b *H;
H = new piesa_b[2];
piesa_a A(4,"TIPA",120);
piesa_b B(100,1,3,"TIPA",120);
H[0]=B;
H[1]=B;
piesa_c C(2, H,14,"TIPC",20);
piesa_a** v = new piesa_a*[3];
v[0] = &A;
v[1] = &B;
v[2] = &C;
for(int i=0;i<3;i++)
v[i].afisare();
return 0;
}
What's wrong?
In C++ (and current C), casts are almost always a sign that the programmer didn't know how to use the language as it is supposed to be used. If you need an array of 3 types of data, the cleanest solution is an array of objects of a class that is base to the 3. And if you want to display each item differently, you'll want to overload the << operator, so you just iterate over the array and go << on each item. Sorted by price means that the class includes a price field, and you use the sort from the standard template library, passing a comparison operation that just compares prices.