doesnt work

"You have to create a class, named Student, representing the student's details, as mentioned above, and store the data of a student. Create setter and getter functions for each element; that is, the class should at least have following functions:

get_age, set_age
get_first_name, set_first_name
get_last_name, set_last_name
get_standard, set_standard
Also, you have to create another method to_string() which returns the string consisting of the above elements, separated by a comma(,). You can refer to stringstream for this."

i have to complete this exercise in hackerank and i dont know why it doesnt work.

#include <iostream>
#include <sstream>
#include<string>
using namespace std;
class Student{
private:
int val,val1;
string fn,sn;

public:
void set_age(int age){ int val=age;};
void set_standard(int standard){int val1=standard;};
void set_first_name(string f)
{string fn=f;
while (fn.length()>50 ){
cin>>fn;
}
};
void set_last_name(string s)
{string sn=s;
while (sn.length()>50 ){
cin>>sn;
}};
void to_string(){
cout<<age<<","<<first_name<<","<<last_name<<","<<standard<<endl};
}

int get_age(){return val;};
int get_standard(){return val1;};
int get_first_name(){return fn;};
int get_last_name(){return sn;};
};
int main() {
int age, standard;
string first_name, last_name;

cin >> age >> first_name >> last_name >> standard;

Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name);
st.set_last_name(last_name);

cout << st.get_age() << "\n";
cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
cout << st.get_standard() << "\n";
cout << "\n";
cout << st.to_string();

return 0;
}
Last edited on
what does not work? does it compile? if it compiles, what does it do when you run it?
Also, you have to create another method to_string() which returns the string consisting of the above elements, separated by a comma(,)

Why is your to_string function void instead of returning a string ?

Why I ran your code on the shell I got tons of errors / warnings

26:1: error: expected ';' after class definition In member function 'void Student::set_age(int)': 
11:28: warning: unused variable 'val' [-Wunused-variable] In member function 'void Student::set_standard(int)': 
12:37: warning: unused variable 'val1' [-Wunused-variable] In member function 'void Student::to_string()': 
25:7: error: 'age' was not declared in this scope 
25:17: error: 'first_name' was not declared in this scope 
25:34: error: 'last_name' was not declared in this scope 
25:50: error: 'standard' was not declared in this scope In function 'int get_age()': 
28:22: error: 'val' was not declared in this scope At global scope: 
28:27: warning: extra ';' [-Wpedantic] In function 'int get_standard()': 
29:27: error: 'val1' was not declared in this scope At global scope: 
29:33: warning: extra ';' [-Wpedantic] In function 'int get_first_name()': 
30:29: error: 'fn' was not declared in this scope At global scope: 
30:33: warning: extra ';' [-Wpedantic] In function 'int get_last_name()': 
31:28: error: 'sn' was not declared in this scope At global scope: 
31:32: warning: extra ';' [-Wpedantic] 
32:1: error: expected declaration before '}' token In function 'int get_age()': 
28:26: warning: control reaches end of non-void function [-Wreturn-type] In function 'int get_standard()': 
29:32: warning: control reaches end of non-void function [-Wreturn-type] In function 'int get_first_name()': 
30:32: warning: control reaches end of non-void function [-Wreturn-type] In function 'int get_last_name()': 
31:31: warning: control reaches end of non-void function [-Wreturn-type] 

http://cpp.sh/7txhu
Topic archived. No new replies allowed.