+ 5
Struct in C++
I have to write a C++ program using struct and function whose argument is struct. The program should ask user how many employees data they need to store and it should also give user data of required employee when needed.
21 odpowiedzi
+ 4
Guru patel
#include <iostream>
using namespace std;
struct employee
{
int ID_number;
char name[50];
char address[50];
char department[20];
int salary;
};
int main ()
{
employee p;
cout<<" What is your address ?"<<endl;
cin.getline(p.address,50);
cout<<" What is your department ?"<<endl;
cin.getline(p.department,20);
cout<< " What is your full name ?"<<endl;
cin.getline(p.name,50);
cout<<" What is your ID number ?"<<endl;
cin>>p.ID_number;
cout<< " What is your salary ?"<<endl;
cin>>p.salary;
cout<<"\nDisplaying Information :"<<endl;
cout<<"ID Number is : "<<p.ID_number<<endl;
cout<<"name is : "<<p.name<<endl;
cout<<"adress is : "<< p.address<<endl;
cout<<"department is : "<<p.department<<endl;
cout<<"salary is : "<<p.salary<<endl;
return 0;
}
This is your code(1st posted). And i have made change what I said. And this works well. (I CHECKED ALREADY) for single employee.
+ 1
Guru patel
In cin>>p.salary it reads digits, it does not read a newline. You might type a newline but that doesn't mean that it gets read. In fact the newline is left over until the next time you read, which is your getline call. So your first getline call reading the newline that has been left over.
SOLUTION:
Just put your all getline statements first, before getting id and salary.
+ 1
Since your 1st getline takes new line as input, your 2nd getline takes the address as input and 3rd getline takes the department as input. So, the code displays the corresponding output.
+ 1
Subashini G Thank u so much ❤️
+ 1
Guru patel But if you want to store information for several employee, you have to create a array of object.
+ 1
I tried it in VSC but it's only taking data not displaying it
edit :
oh sorry
It is displaying but just after the input, it is displaying
+ 1
Today our teacher taught us concept of classes based on which I have done this program 😁😁https://code.sololearn.com/cXMC5x4tFHeV/?ref=app
do run in some editor 😁😁
0
#include <iostream>
using namespace std;
struct employee
{
int ID_number;
char name[50];
char address[50];
char department[20];
int salary;
};
int main ()
{
employee p;
cout<<" What is your ID number ?"<<endl;
cin>>p.ID_number;
cout<< " What is your salary ?"<<endl;
cin>>p.salary;
cout<<" What is your adress ?"<<endl;
cin.getline(p.address,50);
cout<<" What is your department ?"<<endl;
cin.getline(p.department,20);
cout<< " What is your full name ?"<<endl;
cin.getline(p.name,50);
cout<<p.name;
cout<<"Displaying Information :"<<endl;
cout<<"ID Number is : "<<p.ID_number<<endl;
cout<<"name is : "<<p.name<<endl;
cout<<"adress is : "<< p.address<<endl;
cout<<"department is : "<<p.department<<endl;
cout<<"salary is : "<<p.salary<<endl;
return 0;
}
This is the code
0
Subashini G sorry but it's not working I checked just now!!
what I did :
I have made some changes like I have made a function and inside function body, I have made changes as stated by you , but it is not working now , it is showing two questions one after another plz help !!!
0
Can you post your input and output here?
0
Ok but my program is still not complete
0
Code :
#include <iostream>
using namespace std;
void callme(struct employee);
struct employee
{
int ID_number;
char name[50];
char address[50];
char department[20];
int salary;
};
int main ()
{
int a;
cout<<" Data of how many employees you want to enter ? : "<<endl;
cin>>a;
employee p;
callme(p);
return 0;
}
void callme(struct employee p)
{
cout<<" What is your adress ?";
cin.getline(p.address,50);
cout<< " What is your full name ?"<<endl;
cin.getline(p.name,50);
cout<<" What is your department ?"<<endl;
cin.getline(p.department,20);
cout<< " What is your salary ?";
cin>>p.salary;
cout<<p.name;
cout<<" What is your ID number ?"<<endl;
cin>>p.ID_number;
cout<<"Displaying Information :"<<endl;
cout<<"ID Number is : "<<p.ID_number<<endl;
cout<<"name is : "<<p.name<<endl;
cout<<"adress is : "<< p.address<<endl;
cout<<"department is : "<<p.department<<endl;
cout<<"salary is : "<<p.salary<<endl;
0
Output :
Data of how many employees you want to enter ? :
45
What is your adress ? What is your full name ?
this two questions are printing one after other
0
Subashini G oh but you just tell me about that output , I am working on other parts
0
visph it is not showing the data of employees
also it was supposed to use only one header 😅😅
0
Guru patel it is...
first enter all employees data, then display employee list (all employees data)...
tried successfully with input:
2
42
john doe
new york city
ceo
55200
11
foo bar
somewhere
factory
21500
0
I don't know VSC, but try here: it's working fine ^^
I can't see reasons for same code not running too in VSC... but bad copying it ;P
0
Ok bro 🤟🏻