Structures inside private - c++

I do not know how to work with structures inside a class. I think I have the first part right but nothing works in the main. When I try running my program it says "function does not take 0 arguments" Should I write everything in the main like this:
P.Read(BOX m);
Here's my code so far as below:
#include <iostream>
#include <string>
using namespace std;
template <class T, int n>
class SIX
{
private:
struct BOX
{
T a[n];
string name;
};
public:
void Read(BOX m)
{
cout<<"Enter your name: ";
cin>>m.name;
cout<<m.name<<" please enter "<<n<<" data: ";
for(int i=0;i<n;++i)
cin>>m.a[i];
}
void SortArray(BOX m)
{
sort(m.a, m.a+n);
}
void Display(BOX m)
{
cout<<m.name<<" this is the sorted list of data in your array a: ";
for(int i=0;i<n;++i)
cout<<m.a[i]<<'\t';
cout<<endl;
}
};
int main()
{
SIX <int, 6> P;
SIX <string, 5> Q;
P.Read();
P.SortArray();
P.Display();
cout<<endl;
Q.Read();
Q.SortArray();
Q.Display();
cout<<endl;
system("pause");
return 0;
}

No, define Read as
void Read()
{
cout<<"Enter your name: ";
cin>>BOX.name;
cout<<BOX.name<<" please enter "<<n<<" data: ";
for(int i=0;i<n;++i)
cin>>BOX.a[i];
}
Read is a member function, and it has access to Box, so that's all you have to do.
Same for the other functions, you don't need to specify BOX m as the parameter, replace it by () and replace m by BOX inside the code.
As #Barmar pointed out, your struct BOX just defines a type, you should define a member variable named BOX. Simplest way to do it is to just move BOX to the end of struct declaration,
struct
{
T a[n];
string name;
} BOX;

Related

My c++ Code is not working for a Graph

I am writing a code for a graph in c++ but there is some problem. It is not working properly. Please help me what is the problem with it? Its code for a graph which can take inputs for graph from user and each edge in graph has specific weight.
Here is the code:
#include <iostream>
#include <vector>
using namespace std;
struct edge {
char src;
char dest;
int weight;
};
class Graph {
public:
vector<edge> edges;
int size,j=0;
//Constructor
Graph(int c) {
size=c;
}
void graphDesign(char s,char d,int w) {
edges[j].src=s;
edges[j].dest=d;
edges[j].weight=w;
j++;
}
void printGraph() {
for(int i=0; i<size; i++) {
cout<<edges[i].src<<"->"<<edges[i].dest<<" :
<<edges[i].weight<<endl;
}
}
};
int main() {
int e,i,w;
char s,d;
cout<<"Enter number of edges of graphs: ";
cin>>e;
Graph graph(e);
for(i=0; i<e; i++) {
cout<<"Enter source: ";
cin>>s;
cout<<"Enter destination: ";
cin>>d;
cout<<"Enter weight of the edge: ";
cin>>w;
graph.graphDesign(s,d,w);
}
graph.printGraph();
return 0;
}
One issue is here:
void graphDesign(char s,char d,int w) {
edges[j].src=s;
edges[j].dest=d;
edges[j].weight=w;
j++;
}
Since edges is an empty vector, accessing edges[j] is an illegal access.
You need to size the edges vector appropriately before you use it.
class Graph {
public:
vector<edge> edges;
//Constructor
Graph(int c) : edges(c) {}
This creates a vector with c entries.
Also, do not use extraneous, unnecessary member variables such as size here. The vector class has a size() member function to tell you how many items are in the container.
Using extraneous variables like size runs the risk of bugs occurring due to having to update this variable anytime the vector changes size. Instead of trying to do this housekeeping yourself, use the size() function provided to you by std::vector.

Linked List - Adding/Displaying using a vector

I have a homework to do but I don't know what's the problem with the displaying function.. I have a vector use to make more linked list (it's a part of the homework).. that's the code:
#include <iostream>
using namespace std;
#define MAX_LIST 100
struct nod {
int info;
nod* urm;
};
nod* liste[MAX_LIST];
void citesteListaSimpla(nod* liste[MAX_LIST],int nrListe);
void afisareListaSimpla(nod* liste[MAX_LIST],int nrListe);
int main()
{
unsigned int nrListe;
cout << "List numbers: ";
cin>>nrListe;
cout<<endl;
citesteListaSimpla(liste,nrListe);
afisareListaSimpla(liste,nrListe);
}
void citesteListaSimpla(nod* liste[MAX_LIST],int nrListe)
{
for(int i=0; i<nrListe; i++)
{
unsigned int nrElemente;
cout<<"Numbers of the list "<< i+1 << ": ";
cin>>nrElemente;
int element;
liste[i]=NULL;
nod* liste[nrListe];
for(int j=1; j<=nrElemente; j++)
{
cout<<"Number "<<j<<": ";
cin>>element;
liste[nrListe]=new nod;
liste[nrListe]->info=element;
liste[nrListe]->urm=liste[i];
liste[i]=liste[nrListe];
}
}
}
void afisareListaSimpla(nod* liste[MAX_LIST],int nrListe)
{
for(int i=0; i<nrListe; i++)
{
nod* liste[nrListe];
liste[nrListe]=liste[i];
while(liste[nrListe]!=NULL)
{
cout<<liste[nrListe]->info<<", ";
liste[nrListe]=liste[nrListe]->urm;
}
}
}
And if I run it it looks like this:
How I make the program to show me the linked list ? ..
On line 35 and 52 you're re-declaring liste shadowing the function parameter. Having liste declared in three different scopes is a recipe for disaster.
nod* liste[nrListe];
Removing those two lines seems to work.
You should probably use a better editor (I use CLion). You would have figured this out, because CLion warned me about it when I pasted your code in the editor. ;)

structure name not declared and swap function swaps without pass by refrence

I have this code,which takes input of 3 students from division a and b each.
those 2 divions are sorted and merged in a 3rd array according to birth dates of students.
the swap function ,I have not passed anything by refrence still its swapping and sort output is correct !!!.
NOTE:the line below #include..
void swap(struct a,struct b)
it should be
void swap(struct student a,struct student b)
but without changing that the program is runnong and giving correct outputs !! how ??
#include<iostream>
#include<string.h>
using namespace std;
void swap(struct a,struct b);
void findweek(struct student ar[10],int l,int bd1,int bm1);
struct student
{
//m is for month and b is for birthdate prn=prn number,name=name of student
int m,bd;
char prn[10],name[10];
};
int main()
{
//2 divisions a and b declared and will be merged into c
struct student a[3], b[3], c[6];
//division a input
for(int i=0;i<3;i++)
{
cout<<" Enter name of student "<<endl;
cin>>a[i].name;
cout<<"Enter prn no. "<<endl;
cin>>a[i].prn;
cout<<"Enter birth day "<<endl;
cin>>a[i].bd;
cout<<"Enter birth month "<<endl;
cin>>a[i].m;
}
//sorting of a
for(int i=0;i<3;i++)
{
for(int j=i;j<3;j++)
{
if(a[i].m>a[j].m)
{
swap(a[i],a[j]);
}
else if(a[i].m==a[j].m)
{
if(a[i].bd>a[j].bd)
{
swap(a[i],a[j]);
}
}
}
}
//division b input
for(int i=0;i<3;i++)
{
cout<<" Enter name of student "<<endl;
cin>>b[i].name;
cout<<"Enter prn no. "<<endl;
cin>>b[i].prn;
cout<<"Enter birth day "<<endl;
cin>>b[i].bd;
cout<<"Enter birth month "<<endl;
cin>>b[i].m;
}
//sorting of b
for(int i=0;i<3;i++)
{
for(int j=i;j<3;j++)
{
if(b[i].m>b[j].m)
{
swap(b[i],b[j]);
}
else if(b[i].m==b[j].m)
{
if(b[i].bd>b[j].bd)
{
swap(b[i],b[j]);
}
}
}
}
cout<<"-----------------------"<<endl;
cout<<"Division A"<<endl;
int count=0;
for(int i=0;i<3;i++)
{ //c has merged array , a being filled first
c[i]=a[i];
count++;
cout <<c[i].name<<"\t"<<c[i].prn<<"\t"<<c[i].bd<<"|"<<c[i].m<<endl;
}
cout<<"Division B"<<endl;
for(int i=0;i<3;i++)
{
//resume filling the array from count
c[count]=b[i];
cout <<c[count].name<<"\t"<<c[count].prn<<"\t"<<c[count].bd<<"|"<<c[count].m<<endl;
count++;
}
int bd1,bm1;
cout<<"Enter date to find birthdays in that week "<<endl;
cin>>bd1;
cout<<"Enter corresponding month "<<endl;
cin>>bm1;
findweek(c,count,bd1,bm1);
return 0;
}
//to swap the structure student arrays for sorting
void swap(struct student a,struct student b)
{
struct student t;
t=a;
a=b;
b=t;
}
void findweek(struct student ar[10],int l,int bd1,int bm1)
{
int count=0;
for(int i=0;i<l;i++)
{
int month_end=30;
int next_month=bm1+1;
//if(bd1>=23)
int end_date=bd1+7-month_end;
// else
int endofweek=bd1+7;
//l is length of ar , ar=copy of merged array, bd1&bm1 are date and month to search for birthday in that week
if((ar[i].m==bm1&&ar[i].bd>=bd1&&ar[i].bd<=endofweek)||ar[i].m==bm1+1&&ar[i].bd<=end_date)
{
if(month_end-bd1>7)
cout <<ar[i].name<<"\t"<<ar[i].prn<<"\t"<<ar[i].bd<<"|"<<ar[i].m<<endl;
else
{
if((ar[i].m==bm1&&ar[i].bd>=bd1)||(ar[i].m==next_month&&ar[i].bd<=end_date))
{
cout <<ar[i].name<<"\t"<<ar[i].prn<<"\t"<<ar[i].bd<<"|"<<ar[i].m<<endl;
}
}
count++;
if(count>7)
break;
}
}
}
It compiles even if you remove this line:
void swap(struct a,struct b);
Also, it compiles if you remove the whole swap function.
How is it?
Quite simple.
You are defining a function that takes two arguments: an incomplete type struct a and an incomplete type struct b.
That function is simply discarded from the overload set while searching the one to be used at function call.
Your main is not using your swap function. Instead, it's using the one from std:: namespace.
It is probably introduced by iostream or string, it's implementation defined.
Try changing the name of the function or putting a throw in your implementation of swap. In the second case, your runtime won't be affected.
Minimal, (not-)working example to reproduce the issue:
void f(struct s);
struct S {};
int main() { f(S{}); }
void f(S) {}
As you can see, the error is that you are referring to an incomplete type struct s.
swap is kinda of a somehow misleading example that compiles for the reasons above.
Reproducing an issue with a minimal example is often helpful.

What can't I delete a value defined by the user from a vector?

I am trying to use the vector erase function to delete a string that is an element of a vector for a homework assignment. I have tried the line two ways:
vectnames.erase(vectnames[blowup]);
vectnames.erase(blowup);
Does anyone know why the erase function might not be working? As a bit of background, I have to allow the user to enter a planet name to a vector and also let them delete it by name. The example I found online used line #2, but it's not working...
Here is the rest of my code for reference.
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
using namespace std;
class planet
{
private:
string n;
double d, m;
public:
void Density (double d, double m)
{
double Den = m/((4.0/3.0)*M_PI*pow((d/2.0), 3.0));
cout<<"Density: "<<Den<<endl;
}
void SurfaceArea(double d)
{
double S = 4.0*M_PI*pow((d/2.0), 2.0);
cout<<"Surface Area: "<<S<<endl;
}
void Name (string n)
{
string N = n;
cout<<"Name: "<<N<<endl;
}
void Gravity (double G, double m, double d)
{
double F = G*m/pow((d/2.0), 2.0);
cout<<"Force of gravity: "<<F<<endl;
}
};
int main()
{
const double G=6.67384e-11;
int c=0;
string n, N, blowup;
double d=0.0, m=0.0, Den=0.0, S=0.0, F=0.0;
vector<string> vectnames;
vector<double> vectdiam;
vector<double> vectmass;
do
{
cout<<"1. Add a planet\n";
cout<<"2. Delete planet\n";
cout<<"3. Find planet (by name)\n";
cout<<"4. List all planets\n";
cout<<"5. Sort (alphabetical order)\n";
cout<<"6. Quit\n";
cout<<endl;
cout<<"Please select an option from the menu above."<<endl;
cin>>c;
cout<<endl;
if(c==1)
{
planet red;
cout<<"Enter the planet's name: ";
cin>>n;
cout<<"Enter the planet's diameter: ";
cin>>d;
cout<<"Enter the planet's mass: ";
cin>>m;
cout<<endl;
vectnames.push_back(n);
vectdiam.push_back(d);
vectmass.push_back(m);
red.Name(n);
red.Density(d, m);
red.SurfaceArea(d/2.0);
red.Gravity(G, m, d);
cout<<endl;
}
else if (c==2)
{
cout<<"Fire the Death Star's superlaser at: "<<endl;
cin>>blowup;
vectnames.erase(vectnames[blowup]); //This is the part that I'm having trouble with
for (int i=0; i<vectnames.size(); i++)
{
cout<<vectnames[i]<<endl;
}
}
else if (c==4)
{
for (int i=0; i<vectnames.size(); i++)
{
planet red;
cout<<"Planet name: "<<vectnames[i]<<endl;
cout<<"Planet diameter: "<<vectdiam[i]<<endl;
cout<<"Planet mass: "<<vectmass[i]<<endl;
red.Density(vectdiam[i], vectmass[i]);
red.SurfaceArea(vectdiam[i]/2.0);
red.Gravity(G, vectmass[i], vectdiam[i]);
cout<<"************************"<<endl;
cout<<endl;
}
}
} while (c!=6);
system("pause");
return 0;
}
I'm going to work with vector<planet> planets; because it's got a nice ring to it and it's purpose is about as close to obvious as I can get without using an idiotically long name like VectorOfPlanets.
In order to erase an item from a vector you have to know where it is. There are a bunch of ways to do this from brute force searching the vector in a loop until you find the index of the item you want and then calling planets.erase(planets.begin + index);
I thought there was a overload of std::find you could pass a comparator function, but it looks like I'm on crack. Glad I didn't make an ass of myself by suggesting it. Wait... Am I writing out loud? Crap. Hate it when I do that.
Read up on how to create an operator= method in the planet class and you can use std::find. With it you can:
vector<planet> planets;
vector<planet>::iterator it;
it = std::find(planets.begin(), planets.end());
if (it != planets.end())
{
planets.erase(it);
}

Newbie - matrix addition implementation in c++

Hello i'm trying to program the addition of 2 matrices into a new one (and it does when i run the program step by step) but for some reason VS 2010 gives me an access error after it does the addition.
Here is the code.
#include <iostream>
#include <cstdio>
#include <conio>
using namespace std;
class operatii
{
typedef double mat[5][5];
mat ms,m1,m2;
int x1,x2,y1,y2;
public:
void preg();
int cit_val();
void cit_mat(int&,int&,double[5][5]);
void suma();
void afisare(int&,int&,double[5][5]);
};
void operatii::preg()
{
cit_mat(x1,y1,m1);
cit_mat(x2,y2,m2);
suma();
afisare(x1,y1,ms);
}
int operatii::cit_val()
{
int n;
cin>>n;
return n;
}
void operatii::cit_mat(int& x,int& y,double m[5][5])
{
char r;
cout<<"Matrice patratica? Y/N ";
cin>>r;
if ((r=='y')||(r=='Y'))
{
cout<<"Numar linii si coloane: ";
x=cit_val();
y=x;
}
else
{
cout<<"Numar linii: ";
x=cit_val();
cout<<"Numar coloane: ";
y=cit_val();
}
for (int i=1;i<=x;i++)
for (int j=1;j<=y;j++)
cin>>m[i][j];
}
void operatii::suma()
{
if ((x1==x2)&&(y1==y2))
for (int i=1;i<=x1;i++)
for (int j=1;i<=y1;j++)
ms[i][j]=m1[i][j]+m2[i][j];
else cout<<"Eroare";
}
void operatii::afisare(int& x,int& y,double m[5][5])
{
cout<<endl;
for (int i=1;i<=x;i++)
{
for (int j=1;j<=y;j++)
cout<<m[i][j];
cout<<endl;
}
}
void main()
{
operatii matrice;
matrice.preg();
system("PAUSE");
}
Any kind of help would be apreciated.
Arrays are 0-based in c++.
Change your various variants of for (somevar=1; somevar<=something) to for (somevar=0; somevar<something)
You're writing past the end of your arrays, which overwrites stack return address, leading to a return to nonexecutable code, again leading to an access violation.
Also,
for (int j=1;i<=y1;j++)
I think you want to use j not i here. Such errors are much easier to see if you use longer and more distinct variable names than "i" and "j", such as e.g. "Line" and "Column"