Struct in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

12th Jul 2021, 7:39 AM
Viraj
Viraj - avatar
21 Answers
+ 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.
12th Jul 2021, 12:39 PM
Subashini G
Subashini G - avatar
14th Jul 2021, 1:54 AM
Abhishek Kumar
Abhishek Kumar - avatar
+ 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.
12th Jul 2021, 9:48 AM
Subashini G
Subashini G - avatar
+ 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.
12th Jul 2021, 9:56 AM
Subashini G
Subashini G - avatar
+ 1
Subashini G Thank u so much ❤️
12th Jul 2021, 10:01 AM
Viraj
Viraj - avatar
+ 1
Guru patel But if you want to store information for several employee, you have to create a array of object.
12th Jul 2021, 12:41 PM
Subashini G
Subashini G - avatar
+ 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
12th Jul 2021, 3:22 PM
Viraj
Viraj - avatar
+ 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 😁😁
13th Jul 2021, 8:58 AM
Viraj
Viraj - avatar
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
12th Jul 2021, 7:39 AM
Viraj
Viraj - avatar
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 !!!
12th Jul 2021, 12:12 PM
Viraj
Viraj - avatar
0
Can you post your input and output here?
12th Jul 2021, 12:18 PM
Subashini G
Subashini G - avatar
0
Ok but my program is still not complete
12th Jul 2021, 12:20 PM
Viraj
Viraj - avatar
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;
12th Jul 2021, 12:21 PM
Viraj
Viraj - avatar
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
12th Jul 2021, 12:22 PM
Viraj
Viraj - avatar
0
Subashini G oh but you just tell me about that output , I am working on other parts
12th Jul 2021, 12:49 PM
Viraj
Viraj - avatar
0
visph it is not showing the data of employees also it was supposed to use only one header 😅😅
12th Jul 2021, 3:16 PM
Viraj
Viraj - avatar
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
12th Jul 2021, 3:20 PM
visph
visph - avatar
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
12th Jul 2021, 3:28 PM
visph
visph - avatar
0
Ok bro 🤟🏻
12th Jul 2021, 3:29 PM
Viraj
Viraj - avatar