I am new to C++ and I had problems with running this small example of inputing number of rectangle that we want to calculate the area each every one of them.
Here is my code:
#include<iostream>
#include<conio.h>
using namespace std;
class Rectangle{
private:
float length;
float width;
public:
void input()
{
cout<<"length:"<<endl; cin>>length;
cout<<"width:"<<endl; cin>>width;
}
void output()
{
cout<<length<<"\t"<<width<<"\t"<<area();
}
void inputAll(Rectangle a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<"Rectangle"<<(i+1)<<":"<<endl;
a[i].input();
}
}
void outputAll(Rectangle a[],int n)
{
for(int i=0;i<n;i++)
{
a[i].output();
}
}
float area()
{
return length*width;
}
};
int main()
{
Rectangle a[30];
int n;
cout<<"input n:"; cin>>n;
cout<<"Input all Rectangle data:"<<endl;
a.inputAll(a,n);
cout<<"Output all Rectangle data:"<<endl;
a.outputAll(a,n);
return 0;
}
Actually here are the errors in function inputAll and outputAll
main.cpp: In function ‘int main()’:
main.cpp:61:5: error: request for member ‘inputAll’ in ‘a’, which is of non-class type ‘Rectangle [30]’
a.inputAll(a,n);
^~~~~~~~
main.cpp:63:5: error: request for member ‘outputAll’ in ‘a’, which is of non-class type ‘Rectangle [30]’
a.outputAll(a,n);
^~~~~~~~~
I really dont know what caused the errors. Any help for this would be appreciated!
Member functions must be called using the dot. You'll have to loop over your objects:
int main()
{
Rectangle a[30];
int n;
cout<<"input n:"; cin>>n;
cout<<"Input all Rectangle data:"<<endl;
for (auto& r : a) {
r.inputAll(a,n);
}
cout<<"Output all Rectangle data:"<<endl;
for (auto& r : a) {
r.outputAll(n);
}
return 0;
}
Obviously, it doesn't make sense since you're already looping in those functions. You probably meant inputAll and outputAll to be free functions:
class Rectangle{
private:
float length;
float width;
public:
void input()
{
cout<<"length:"<<endl; cin>>length;
cout<<"width:"<<endl; cin>>width;
}
void output()
{
cout<<length<<"\t"<<width<<"\t"<<area();
}
float area()
{
return length*width;
}
};
void inputAll(Rectangle a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<"Rectangle"<<(i+1)<<":"<<endl;
a[i].input();
}
}
void outputAll(Rectangle a[],int n)
{
for(int i=0;i<n;i++)
{
a[i].output();
}
}
With this simple change, your previous main function will work.
inputAll() is a member function of the Rectangle class. Please access the inputAll() through a object of the Rectangle class.
Related
My program is supposed to take the names and 5 marks each for 10 students and output their corresponding average marks and grade. My functions avgmarks and avg_grade are supposed to calculate and return these, as arrays, to the main function. Using the statements return avg[10] and return gr[10] does not work. I know pointer variables can be used here, but i don't know how to implement them to make my program work correctly.
#include <iostream>
#include <string>
using namespace std;
void readnames(string p[]);
int readmarks(int p[10][5]);
int avgmarks(int p[10][5]);
char avg_grade(int p[10]);
void displayresults(string n[], int m[], char g[]);
int main()
{
string names[10];
readnames(names);
int marks [10][5];
readmarks(marks);
int avg_marks[10];
avg_marks[10]=avgmarks(marks);
char grade[10];
grade[10]=avg_grade(avg_marks);
displayresults(names, avg_marks, grade);
}
void readnames(string p[])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s name:\n";
cin>>p[i];
}
}
int readmarks(int p[10][5])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s marks:\n";
for (int j=0;j<5;j++)
{
cin>>p[i][j];
}
}
}
int avgmarks(int p[10][5])
{
int avg[10];
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
return avg[10];
}
char avg_grade(int p[10])
{
char gr[10];
for (int i=0;i<10;i++)
{
if (p[i]>=90)
{
gr[i]='A';
}
if ((p[i]>=80)&&(p[i]<90))
{
gr[i]='B';
}
if ((p[i]>=70)&&(p[i]<80))
{
gr[i]='C';
}
if ((p[i]>=60)&&(p[i]<70))
{
gr[i]='D';
}
if ((p[i]>=50)&&(p[i]<60))
{
gr[i]='E';
}
if ((p[i]>=40)&&(p[i]<50))
{
gr[i]='F';
}
}
return gr[10];
}
void displayresults(string n[], int m[], char g[])
{
float ct;
float sum=0;
cout<<endl;
for (int i=0;i<10;i++)
{
cout<<"Name: "<<n[i]<<" Average Marks: "<<m[i]<<" Grade: "<<g[i];
cout<<endl;
}
for (int i=0;i<10;i++)
{
sum=sum+m[i];
}
ct=sum/10;
cout<<"The class average is "<<ct<<endl;
}
Arrays cannot be returned from functions. It's one good reason to use vectors instead of arrays (there are many more).
But if you want to carry on with arrays you should use pointers to simulate returning an array from your function.
In this example avg is a pointer to an array that will recieve the returned value
void avgmarks(int p[10][5], int* avg)
{
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
}
You use it like this
int avg_marks[10];
avgmarks(marks, avg_marks);
PS. You seem to have the common (but to me bizarre) newbie misunderstanding that if you have an array of size 10 (say) then array[10] can be used to refer to the whole array. Please don't think that, if you have an array of size 10, then array[10] is just an error, because the valid indexes for an array of size 10 are 0 to 9.
This example changed every c-style array to std::array. Keep in mind, that it is the same code as yours with the arrays, so every programming error is still in there. And also arrays are returned by value, means that they are copied. Try to understand how to use std::array and you will get a better programmer.
#include <iostream>
#include <string>
#include <array>
using namespace std;
void readnames(std::array<string, 10>& p);
void readmarks(std::array<std::array<int,5>, 10>& p);
std::array<int,10> avgmarks(std::array<std::array<int,5>, 10>& p);
std::array<char,10> avg_grade(std::array<int,10>& p);
void displayresults(std::array<string, 10> n, std::array<int,10> m, std::array<char,10> g);
int main()
{
std::array<string, 10> names;
readnames(names);
std::array<std::array<int,5>, 10> marks;
readmarks(marks);
std::array<int,10> avg_marks;
avg_marks = avgmarks(marks);
std::array<char,10> grade;
grade = avg_grade(avg_marks);
displayresults(names, avg_marks, grade);
}
void readnames(std::array<string, 10>& p)
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s name:\n";
cin>>p[i];
}
}
void readmarks(std::array<std::array<int,5>, 10>& p)
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s marks:\n";
for (int j=0;j<5;j++)
{
cin>>p[i][j];
}
}
}
std::array<int,10> avgmarks(std::array<std::array<int,5>, 10>& p)
{
std::array<int,10> avg;
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
return avg;
}
std::array<char,10> avg_grade(std::array<int,10>& p)
{
std::array<char,10> gr;
for (int i=0;i<10;i++)
{
if (p[i]>=90)
{
gr[i]='A';
}
if ((p[i]>=80)&&(p[i]<90))
{
gr[i]='B';
}
if ((p[i]>=70)&&(p[i]<80))
{
gr[i]='C';
}
if ((p[i]>=60)&&(p[i]<70))
{
gr[i]='D';
}
if ((p[i]>=50)&&(p[i]<60))
{
gr[i]='E';
}
if ((p[i]>=40)&&(p[i]<50))
{
gr[i]='F';
}
}
return gr;
}
void displayresults(std::array<string, 10> n, std::array<int,10> m, std::array<char,10> g)
{
float ct;
float sum=0;
cout<<endl;
for (int i=0;i<10;i++)
{
cout<<"Name: "<<n[i]<<" Average Marks: "<<m[i]<<" Grade: "<<g[i];
cout<<endl;
}
for (int i=0;i<10;i++)
{
sum=sum+m[i];
}
ct=sum/10;
cout<<"The class average is "<<ct<<endl;
}
I think this will work:
#include <iostream>
#include <string>
using namespace std;
void readnames(string p[]);
void readmarks(int p[10][5]);
int* avgmarks(int p[10][5]);
char* avg_grade(int p[10]);
void displayresults(string n[], int m[], char g[]);
int main()
{
string names[10];
readnames(names);
int marks [10][5];
readmarks(marks);
int *avg_marks;
avg_marks=avgmarks(marks);
char *grade;
grade=avg_grade(avg_marks);
displayresults(names, avg_marks, grade);
}
void readnames(string p[])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s name:\n";
cin>>p[i];
}
}
void readmarks(int p[10][5])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s marks:\n";
for (int j=0;j<5;j++)
{
cin>>p[i][j];
}
}
}
int* avgmarks(int p[10][5])
{
static int avg[10];
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
return avg;
}
char* avg_grade(int *p)
{
static char gr[10];
for (int i=0;i<10;i++)
{
if (p[i]>=90)
{
gr[i]='A';
}
if ((p[i]>=80)&&(p[i]<90))
{
gr[i]='B';
}
if ((p[i]>=70)&&(p[i]<80))
{
gr[i]='C';
}
if ((p[i]>=60)&&(p[i]<70))
{
gr[i]='D';
}
if ((p[i]>=50)&&(p[i]<60))
{
gr[i]='E';
}
if ((p[i]>=40)&&(p[i]<50))
{
gr[i]='F';
}
}
return gr;
}
void displayresults(string n[], int m[], char g[])
{
float ct;
float sum=0;
cout<<endl;
for (int i=0;i<10;i++)
{
cout<<"Name: "<<n[i]<<" Average Marks: "<<m[i]<<" Grade: "<<g[i];
cout<<endl;
}
for (int i=0;i<10;i++)
{
sum=sum+m[i];
}
ct=sum/10;
cout<<"The class average is "<<ct<<endl;
}
Basically, to return an array from a function in C/C++, you declare it as "static", and you make the function return the pointer.
EDIT: As many others have noted, this won't work in multi-threaded programs.
Run the code to see where the problem lies.
[Error] 'd' does not name a type
I tried declaring the variable directly in public but it kept popping up with more errors.
There seems to be apparently no fix, since googling didn't help much in the process.
It'd be super helpful if someone could help me figure out where the problem is.
#include<iostream>
using namespace std;
class array1{
protected:
static int a[50];
public:
int n1;
void getNum1(void)
{
cout<<"how many numbers?"<<endl;
cin>>n1;
for(int i=0;i<n1;i++)
{
cout<<"enter le number"<<endl;
cin>>a[i];
}
}
int* retNum1(void)
{
return a;
}
};
class array2{
protected:
static int b[50];
public:
int n2;
void getNum2(void)
{
cout<<"how many numbers?"<<endl;
cin>>n2;
for(int i=0;i<n2;i++)
{
cout<<"enter le number"<<endl;
cin>>b[i];
}
}
int* retNum2(void)
{
return b;
}
};
class conarray:public array1, public array2{
private:
int c[100],d;
//int d;
public:
d=0;
void disp()
{
for(int i=0;i<5;i++)
{
cin>>c[i];
}
for(int i=0;i<5;i++)
{
cout<<c[i]<<endl;
}
cout<<d;
}
//int d = 0;
/*void merge(void)
{
int *nn1 = retNum1();
int *nn2 = retNum2();
for(int i=0;i<n1;i++)
{
c[d]=nn1[i];
d++;
}
for(int i=0;i<n2;i++)
{
c[d]=nn2[i];
d++;
}
}
void display(void)
{
cout<<"NUMBERS IN ARRAY:"<<endl;
for(int i=0;i<d;i++)
{
cout<<c[i]<<endl;
}
}*/
};
int main()
{
conarray a;
//a.getNum1();
//a.getNum2();
//a.merge();
//a.display();
a.disp();
return 0;
}
Here is my code for quick sort. I am a beginner kindly please help.
#include<iostream>
using namespace std;
class quick
{
private:
int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(a[],left,right);
obj.putdata();
return (0);
}
It is giving me error in int main() function:
a is not declared in this scope.
expected primary expression before ']'.
As the answer is given by #Shubham Khatri. Here is the corrected code.
#include<iostream>
using namespace std;
class quick
{
public: int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<n;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}
while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
As it mentions you have not declared a as a variable inside the int main(). Rather it is an object of quick. In a function you don't pass array like a[] rather as a only .since a, left, right are a private variable of a class you can't access it from the object directly. Declare it as public and use it as obj.a, obj.left, obj.right inside sort function.
Complete code:
#include<iostream>
using namespace std;
class quick
{
public:
int n,left,right,i,j;
float a[55];
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
here is my code. wriiten for to to find armstrong, factorial etc. now i want readNo method private what should i do..?
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Num_Demo
{
public:
int num;
void readNo(int no)
{
num=no;
}
int Factorial (int a)
{
if(a!=0)
{
int f=1;
for (int i=1;i<=a;i++)
{
f=f*i;
}
return f;
}
else
{
return 0;
}
}
int Reverse(int b)
{
int rev=0,rem;
while(b!=0)
{
rem=b%10;
rev=(rev*10)+rem;
b=b/10;
}
return rev;
}
void Palindrome (int c)
{
int num;
int rev=0,rem;
num=c;
while(c!=0)
{
rem=c%10;
rev=(rev*10)+rem;
c=c/10;
}
if(num==rev)
{
cout<<" Number Is Palindrome";
}
else
cout<<" Number is Not Plaindrome";
}
void Armstrong (int d)
{
int sum=0,n1,copy;
copy=d;
while(d!=0)
{
n1=d%10;
sum=sum+n1*n1*n1;
d=d/10;
}
if(sum==copy)
{
cout<<" Number Is Armstrong";
}
else
cout<<" Number is Not Armstrong";
}
};
int main()
{ clrscr();
Num_Demo nd1,nd2,nd3,nd4;
int n1,n2,n3,n4;
cout<<"\n\nEnter The Number To Find Factorial\t";
cin>>n1;
nd1.readNo(n1);
cout<<" The Factorial Is\t"<<nd1.Factorial(n1);
cout<<"\n\nEnter The Number To Find Reverse Number\t";
cin>>n2;
nd2.readNo(n2);
cout<<" The Reverse Is\t"<<nd2.Reverse(n2);
cout<<"\n\nEnter The Number To Find Palindrome\t";
cin>>n3;
nd3.readNo(n3);
nd3.Palindrome(n3);
cout<<"\n\nEnter The Number To Find Armstrong\t";
cin>>n4;
nd4.readNo(n4);
nd3.Armstrong(n4);
getch();
return 0;
}
now i want to make readNo method private. what should i do..? whent i place readNo outside public error "readNo not accessible" pop up. please help me.
You don't use a private function in main function. It's unacceptably.
You put function under private: tag in class definition, but then you can't call it outside from your class, soo then you need public function which will call private function in class.
I was working on this code for a project on school and when I wanted to try debugging my code just got segmentation fault before even running the first line in main() so i was wondering if i miss something on my code or is the compiler's fault.
#include <iostream>
using namespace std;
class poly
{
public: int a[1000000];
private:
int forx(int x);
public:
poly(){cout<<"add";}
~poly(){cout<<"kill";}
void add();
void sum(int *x,int *y);
void dif(int *x,int *y);
void mult(int *x,int *y);
void renew();
};
void poly::add()
{
int i,n;
cin>>n;
a[0]=n;
for (i=1; i<=n; i++)
{
cin>>a[i];
}
}
int poly::forx(int x)
{
int s,i,p;
p=1;
for (i=1; i<=a[0]; i++)
{
s+=p*a[i];
p*=x;
}
return s;
}
void poly::sum(int *x,int *y)
{
int i,m=x[0]>y[0]?x[0]:y[0];
a[0]=m;
for (i=1; i<=a[0]; i++)
{
a[i]=x[i]+y[i];
}
}
void poly::dif(int *x,int *y)
{
int i,m=x[0]>y[0]?x[0]:y[0];
a[0]=m;
for (i=1; i<=a[0]; i++)
{
a[i]=x[i]-y[i];
}
for (i=a[0]; i>0; i--)
{
if (a[i]!=0) break;
a[0]--;
}
}
void poly::mult(int *x,int *y)
{
int i,j,k;
for (i=1; i<=(x[0]+y[0]-2); i++)
{
j=0;
k=y[0]-1;
while (j+k!=i)
{
if (j+k>i) k--;
if (j+k<i) j++;
}
while (j<x[0] && k>=0)
{
a[i]+=x[j]*y[k];
k--;
j++;
}
}
}
void poly::renew () {
int i;
for (i=1; i<=a[0]; i++)
{
cout<<a[i];
}
}
int main()
{
cout<<"starting";
poly w;
w.add();
poly h;
h.add();
poly o;
o.sum(w.a,h.a);
o.renew();
o.dif(w.a,h.a);
o.renew();
o.mult(w.a,h.a);
o.renew();
}
Becase of int a[1000000];, size of poly class is very large. Making a (actually you are making 3) local variable(s) of this class (on stack) would give you segmentation fault.
You can try making them static or move them to global scope or alloc them dynamically.
...
static poly w;
w.add();
static poly h;
h.add();
static poly o;
...
Another solution is to replace arrays with std::vector
change public: int a[1000000]; to
...
public: std::vector<int> a;
...
poly() : a(1000000) {cout<<"add";}
...
Now you can create local objects of this class.
Another related question Segmentation fault on large array sizes