L-value required in c++ - c++

I am getting an lvalue required error at ob1.name and ob2.name. Please
help me figure it out.
#include<iostream.h>
#include<conio.h>
class student
{
public:
int rollno;
char name[10];
};
void main()
{
student ob1;
student ob2;
ob1.rollno=101;
ob1.name="ajay"; // this is where i am getting the error
ob2.rollno=102;
ob2.name="pintu"; // this is where i am getting the error
clrscr();
cout<<"roll no and name of first student"<<ob1.rollno<<ob1.name;
cout<<"roll no and name of second student"<<ob2.rollno<<ob2.name;
getch();
}

You need to use strcpy
i.e.
strcpy(ob1.name, "ajay");
But a better solution would be to use std::string

Related

‘sortBook’ was not declared in this scope [duplicate]

This question already has answers here:
Sorting a vector of custom objects
(14 answers)
Closed 1 year ago.
I cannot solve the error - main.cpp:31:34: error: ‘sortBook’ was not declared in this scope in the line sortBook(arr,arr+n,comparator);
I apologize for asking a question that should have a simple solution, but it's driving me nuts. I've checked for all the common errors.
#include<iostream>
using namespace std;
class BOOK{
private:
char bookname[20];
float bookprice;
public:
void getBookDetails()
{
cout<<"Enter the Book Name:";
cin>>bookname;
cout<<"Enter the Book Price:";
cin>>bookprice;
}
void displayDetails()
{
cout<<"Book Name:"<<bookname<<endl;
cout<<"Book Price:"<<bookprice<<endl;
}
};
bool comparator(string a,string b)
{
return a<b;
}
int main()
{
int n=5;
string arr[]={"sandwich","apple","banana","zombie","pear"};
sortBook(arr,arr+n,comparator);
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
I think it's just that you have not declared sortBook function anywhere and trying to use it.
Your error shows up because you have not defined the method "sortBook".
There are 2 ways to fix this error:
Define your own "sortBook" method in your class.
Use C++ sort method that is defined in "Algorithm". Below is my code that uses this sort method. You have to "include algorithm " at the top of your file, and call the sort method.
#include<iostream>
#include<algorithm> ////// YOU SHOULD INCLUDE THIS STATEMENT
using namespace std;
class BOOK{
private:
char bookname[20];
float bookprice;
public:
void getBookDetails()
{
cout<<"Enter the Book Name:";
cin>>bookname;
cout<<"Enter the Book Price:";
cin>>bookprice;
}
void displayDetails()
{
cout<<"Book Name:"<<bookname<<endl;
cout<<"Book Price:"<<bookprice<<endl;
}
};
bool comparator(string a,string b)
{
return a<b;
}
int main()
{
int n=5;
string arr[]={"sandwich","apple","banana","zombie","pear"};
std::sort(arr,arr+n,comparator); // HERE, YOU CALL : sort()
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
Note: My code above compiles with no errors. It runs and produces the correct output. Please let me know if you have any other issues.

Composition error in c++

Newbie to C++ over here. I am trying to create 2 classes and interlink them using Composition, but I keep getting errors.
#include <iostream>
#include <string>
using namespace std;
class student
{
public: int roll_no;
string name;
string dob;
void student_display()
{
cout<<roll_no<< " "<<name<<" "<<dob<<endl;
}
student(int roll,string names,string dateofb)
{
roll_no=roll;
name=names;
dob=dateofb;
}
~student(){};
};
class course
{
public:string course_name;
int duration;
void course_display()
{
cout<<course_name<< " "<<duration<<" "<<endl;
s1.student_display()<<endl;
}
course (string c_name,int dur,student s2):course_name(c_name),duration(dur),s1(s2){}
~course(){};
private : student s1;
};
class college
{
public: string college_name;
string location;
course c1;
course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
~course(){};
};
int main() {
student s5(001,"Noel","28/04/1994");
s5.student_display();
course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
c1.course_display();
return 0;
}
The errors are as follows:
prog.cpp: In member function 'void course::course_display()':
prog.cpp:32:35: error: invalid operands of types 'void' and '<unresolved overloaded function type>' to binary 'operator<<'
s1.student_display()<<endl;
^
prog.cpp: At global scope:
prog.cpp:45:20: error: expected ')' before 'col_name'
course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
^
prog.cpp:47:13: error: declaration of '~course' as member of 'college'
~course(){};
^
prog.cpp: In function 'int main()':
prog.cpp:52:54: error: no match for call to '(student) (int, const char [5], const char [11])'
course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
Could someone please help me out? I have tried going through forums with the same error but cant figure it out
^
I have modified the code as follows:
#include <iostream>
#include <string>
using namespace std;
class student
{
public: int roll_no;
string name;
string dob;
void student_display()
{
cout<<roll_no<< " "<<name<<" "<<dob<<endl;
}
student(int roll,string names,string dateofb)
{
roll_no=roll;
name=names;
dob=dateofb;
}
~student(){};
};
class course
{
public:
string course_name;
int duration;
private:
student s1;
public:
void course_display()
{
cout<<course_name<< " "<<duration<<" "<<endl;
s1.student_display();
}
course (string c_name,int dur,student s2):course_name(c_name),duration(dur),s1(s2){}
~course(){};
//private: // shifted before course_display()
// student s1;
};
// Not used
//class college
//{
//public:
// string college_name;
// string location;
// course c1;
//
// course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
//
// //~course(){}; // destructor mismatch
//};
int main() {
student s5(001,"Noel","28/04/1994");
s5.student_display();
//course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
course c1("Engineering",4,s5);
c1.course_display();
return 0;
}
and the output is follows:
Line 30: Remove the semicolon.
Line 45: Looks like copy/paste error, should be the constructor of the college class, right? Then change the name from course to college. Same for the destructor on the following line.
Line 52: You create the object s5 2 lines above, I guess you want to pass this object to the constructor of the course object. If so then just remove the expression in the brackets after s5.

My code is username validation, user must be enter the word capital

#include<iostream>
#include<conio.h>
#include<string.h>
#include<cctype>
using namespace std;
string::size_type len;
class login{
private:
char name[10];
string password;
public:
login()
{
strcpy(name,"0");
password="0";
}
void user()
{
cout<<"enter the user name::";
cin>>name;
}
char user_varification()
{
user();
for(int i=1;i<=name.length();i++){
if(isupper(name[i]))
cout<<name[i];
}
}
};
int main()
{
//cout<<"user name should be is capitallater"<<endl<<"length must be minimum 6 characher and user name "<<endl;//
login s;
s.user_varification();
system ("pause");
}
Error message
29 C:\Users\Adeel Haider\Desktop\hellow.cpp
[Error] request for member 'length' in '((login*)this)->login::name', which is of non-class type 'char [10]'
The error is pretty self explanatory. name is of the type char[] and has no member function called length(). Change name to be of type std::string like password is.

default constructor for a class which contains character pointer and a character array

I have designed the class as below
But i am getting runtime error while executing it.
Please let me know the solutions for the below
Why does Line3 in main gives compilation errors (what do i need to do if i want to initialize only one member of the class)
Why do i get run time error for Line 4.
Code:
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
class A{
int age;
char *name;
char info[50];
public:
A(int p_age=0,char *p_name=NULL,char p_info[]=NULL)
{
cout<<"Constructor called"<<endl;
age=p_age;
int length1=strlen(p_name)+1;
int length2=strlen(p_info)+1;
strncpy(name,p_name,length1);
name[length1-1]='\0';
strncpy(info,p_info,length2);
info[length2-1]='\0';
}
void get()
{
cout<<"Enter the age";
cin>>age;
cout<<"Enter name & info";
gets(name);
gets(info);
}
void display()
{
cout<<age<<name<<info;
}
};
int main()
{
A obj(20,"Ename","Stud");
obj.display();
// A obj1("EEEE");//Line3
A obj2; //Line 4
obj2.get();
obj2.display();
}
A obj1("EEEE") is an error because the first parameter to your constructor is declared as an int p_age and the compiler cannot automatically cast "EEEE" into an int. Trying to initialize obj2 causes an error because you have provided no parameter in the call, so the default values are used, and when the line int length1=strlen(p_name)+1; is executed, strlen() crashes from accessing memory from p_name, which is NULL.
name is not allocated.
...
int length1=strlen(p_name)+1;
int length2=strlen(p_info)+1;
name = new char[length1]; // add this line
strncpy(name,p_name,length1);
name[length1-1]='\0';
strncpy(info,p_info,length2);
info[length2-1]='\0';
...
Your first argument in constructor is int and you are trying to pass const char*.
The default initialization for your p_name is NULL and strlen of null gives segmentation fault
In the declaration
A obj2
you do not provide the p_name and p_info arguments, so they are defaulted to NULL. Then in the constructor you call
strlen(p_name)
on p_name which is NULL. This is not allowed.
Let C++ help you.
class A{
int age;
std::string name;
std::string info;
public:
explicit A(int p_age = 0, const std::string& p_name = "", const std::string& p_info = "")
: age(p_age)
, name(p_name)
, info(p_info)
{
}
};

Adding a class to a map

I am trying to add a class object to a map, this is what I have:
#include<vector>
#include<map>
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
class Student{
int PID;
string name;
int academicYear;
public:
Student(int, string, int);
};
Student::Student (int P, string n, int a) {
PID = P;
name = n;
academicYear = a;
}
void createStudent(map<string, Student>);
int main(int argc, char** argv){
map <string, Student> studentList;
createStudent(studentList);
}
void createStudent(map<string, Student> studentList){
int PID;
string name;
int academicYear;
cout << "Add new student-\nName: ";
getline(cin, name);
cout << "PID: ";
cin >> PID;
cout << "Academic year: ";
cin >> academicYear;
Student newstud (PID, name, academicYear);
studentList[name] = newstud; //this line causes the error:
//no matching function for call to
//'Student::Student()'
}
I don't understand why the constructor function is being called there, I thought newstud would already have been constructed from the previous line. Can anyone explain what is happening when I try to add newstud to the map?
Problem 1st
std::map::operator[] will insert new element into container if its not present, using default constructor, which in your case is not present and will probably doesn't make sense even if you provide one.
So use std::map::insert for such scenario
Problem 2nd
Even if you successfully insert using studentList.insert(std::make_pair(name, newstud)); its not going to reflect changes in original map studentList in main ( ) unless you use a reference type, in function definition & declaration of createStudent
So use
void createStudent(map<string, Student>& );
In order to add entries using the function, you should make your parameter studentList pass by reference.
And instead of
studentList[name] = newstud;
use:
studentList.insert(std::make_pair(name, newstud));
Just a suggestion, though.