+ 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; }
2 Respuestas
+ 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.
+ 3
Thanks Josh for your Help