I need help with this code to :

a. Creating an array of employees
b. Asking the user how many employees records does he want to enter.
c. Modifying the input_data function to take the data of all employees and fill them in the array
d. Modifying the output_data function to display the records of all employees.

#include <iostream>
#include <string>
using namespace std;
void output_data(struct database*data_ptr);
void input_data(struct database*data_ptr);
struct database {
int id_number;
int age;
float salary;
string name;
};
int main()
{
struct database employee;
struct database* sp;
sp = &employee;
input_data(sp);
output_data(sp);
return 0 ;
}
void output_data(struct database*data_ptr)
{
cout << "*********************************\n";
cout << "name = " << data_ptr->name << "\n";
cout << "id_num = " << data_ptr->id_number << "\n";
cout << "age = " << data_ptr->age << "\n";
cout << "salary = " << data_ptr->salary << "\n";
cout << "*********************************\n";
}
void input_data(struct database*data_ptr)
{
int i_tmp;
float f_tmp;
string s_tmp;
cout<<"enter name:";
cin >>s_tmp;data_ptr->name = s_tmp;
cout << "enter id_number :";
cin >> i_tmp;data_ptr->id_number = i_tmp;
cout << "enter age :";
cin >> i_tmp;data_ptr->age = i_tmp;
cout << "enter salary:";
cin >> f_tmp;data_ptr->salary = f_tmp;;
cout<<endl;
}

Dani AI

Generated

Quick diagnosis: the compiler errors in Post #5 come from forum formatting (BBCode/markup) being copied into the .cpp file. Tokens like [B] and [/B] ended up in the source and confuse the parser (hence "missing ';' before '['", "undeclared n", etc.). Removing those markup tags restores valid syntax.

Practical fixes and gotchas:

  • Remove any BBCode/HTML tags from the pasted source so only plain C++ remains.
  • Make sure int n; is declared before it is used and read from input.
  • Prefer std::vector<...> instead of manual new[]/delete[] to avoid leaks and lifetime issues. Include <vector>.
  • When reading a name that may contain spaces, use std::getline and consume the leftover newline after numeric input (e.g., std::cin.ignore(...)). Mixing operator>> and getline without ignoring will skip input.
  • If keeping input_data/output_data, keep them operating on one employee (pointer or reference) and call them inside a loop over the array/vector.
  • Ensure main returns int and ends with return 0; for portability.

A minimal modern approach (illustrates std::vector + safe line input):

#include <iostream>
#include <vector>
#include <string>
#include <limits>

struct Database { int id; int age; float salary; std::string name; };

int main() {
  int n;
  if (!(std::cin >> n)) return 0;
  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

  std::vector<Database> employees(n);
  for (int i = 0; i < n; ++i) {
    std::cout << "Name: ";
    std::getline(std::cin, employees[i].name);
    std::cout << "ID Age Salary: ";
    std::cin >> employees[i].id >> employees[i].age >> employees[i].salary;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  }

  // output loop here
  return 0;
}

was on the right track with dynamic allocation; the main blocker shown in 's error list is the stray formatting tags. After stripping BBCode and handling input buffering (getline vs >>), the program should compile and the array-of-employees logic will work as intended.

Recommended Answers

All 5 Replies

int main()
{
   database *employee ;
   cin >> n ;
   employee = new int[n] ;
   for ( int i = 0 ; i  < n ; i++)
       input_data(employee+i);
   for ( int i = 0 ; i  < n ; i++)
       output_data(employee+i);
}

EDIT:
Also Use code tags properly. Like [code=cpp] and not just [code].
Intend your code for better readability.

Prabakar can u integrate it in my code plz cuz i get alot of errors and i donno how to resolve it

oops sorry. Should have tested the code before posting it.
heres the code

#include <iostream>
#include <string>

using namespace std;

struct database {
  int id_number, age;
  float salary;
  string name;
};

void output_data(struct database*data_ptr)
{
  cout << "*********************************\n";
  cout << "name = " << data_ptr->name << "\n";
  cout << "id_num = " << data_ptr->id_number << "\n";
  cout << "age = " << data_ptr->age << "\n";
  cout << "salary = " << data_ptr->salary << "\n";
  cout << "*********************************\n";
}
void input_data(struct database*data_ptr)
{
  int i_tmp;
  float f_tmp;
  string s_tmp;
  cout<<"enter name:";
  cin >>s_tmp;data_ptr->name = s_tmp;
  cout << "enter id_number :";
  cin >> i_tmp;data_ptr->id_number = i_tmp;
  cout << "enter age :";
  cin >> i_tmp;data_ptr->age = i_tmp;
  cout << "enter salary:";
  cin >> f_tmp;data_ptr->salary = f_tmp;;
  cout<<endl;
}
int main()
{
   database *employee ;
   [B]int n;[/B]
   cin >> n ;
   employee = new [B]database[/B][n] ;
   for ( int i = 0 ; i  < n ; i++)
       input_data(employee+i);
   for ( int i = 0 ; i  < n ; i++)
       output_data(employee+i);
}

i got a list of errors which are

:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(39) : error C2143: syntax error : missing ';' before '['
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(39) : error C2143: syntax error : missing ';' before '['
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(41) : error C2059: syntax error : '['
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(42) : error C2143: syntax error : missing ')' before ';'
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(42) : error C2143: syntax error : missing ';' before ')'
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(42) : error C2143: syntax error : missing ';' before ')'
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(42) : error C2065: 'n' : undeclared identifier
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(42) : warning C4552: '<' : operator has no effect; expected operator with side-effect
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(42) : error C2143: syntax error : missing ';' before ')'
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(44) : error C2374: 'i' : redefinition; multiple initialization
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(42) : see declaration of 'i'
C:\Users\Omar\Desktop\New Folder (3)\Cpp1.cpp(46) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

For some reason the [/ b] tags did not work. Cant you at least remove it?
Did you really do that program?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.