+ 3

What's wrong in this code Its not taking my id

#include<iostream> //#define SIZE 3 using namespace std; class code{ char name; int id; public: void getdata(); void showdata(); }; void code::getdata(){ cout<<"Enter Name : "; cin>>name; cout<<"Enter ID : "; cin>>id; } void code :: showdata(){ cout<<"\nName : "<<name<<endl; cout<<"ID : "<<id<<endl; } int main() { char x; int y; code obj; obj.getdata(); obj.showdata(); return 0; }

1st Jan 2021, 3:48 AM
MUKUL Choudhary
MUKUL Choudhary - avatar
2 Respostas
+ 8
name should be an array instead of a single char. This works: #include<iostream> //#define SIZE 3 using namespace std; class code{ char name[50]; int id; public: void getdata(); void showdata(); }; void code::getdata(){ cout<<"Enter Name : "; cin>>name; cout<<"Enter ID : "; cin>>id; } void code :: showdata(){ cout<<"\nName : "<<name<<endl; cout<<"ID : "<<id<<endl; } int main() { code obj; obj.getdata(); obj.showdata(); return 0; } The improvements I made are: 1. Most importantly converted name from a char to a char array. 2. Removed the unused x and y variables from main. 3. Improved indentation and removed some unneeded blank lines.
1st Jan 2021, 3:53 AM
Josh Greig
Josh Greig - avatar
+ 3
Thanks Josh for your Help
1st Jan 2021, 5:44 AM
MUKUL Choudhary
MUKUL Choudhary - avatar