I have coded a program that gets the information of employees of a company, when I run it in Dec C , it does run without any errors, but when I run it in Visual Studio, it returns the following error :
Error C4703 potentially uninitialized local pointer variable 'data' used
and VS asks me to rewrite the pointer like this :
employ *data{};
instaed of : employ *data;
Can anyone please explain why does it happen? and What this {}
means here? Is it a thing of C or VS?
#include <iostream>
using namespace std;
struct employ {
long int emp_num;
string fn;
string ln;
int work_days;
long int payday;
};
int main()
{
int n=0;
employ *data;
int act;
do {
cout << "___________________________________________________________________________________________________________";
cout << "\n\n\t\tWelcome to the EMPLOTASK!";
cout << "\n\t\tFor doing any of the commands, enter number of that command.";
cout << "\n\t\t_______________________________________________________________";
cout << "\n\n\t\t\ ADD & EDIT : ";
cout << "\n\t\t[1] Add new employees.";
cout << "\n\t\t[2] Edit an existing employee.";
cout << "\n\t\t[3] Delete an existing employee.";
cout << "\n\t\t[4] Print list of all employees.";
cout << "\n\t\t_______________________________________________________________";
cout << "\n\n\t\t\ ACTIONS : ";
cout << "\n\t\t[5] Sort based on their salary.";
cout << "\n\t\t[6] Search for an emplyee.";
cout << "\n\t\t[7] Calculate the average salary.";
cout << "\n\t\t[8] Show maximum and minimum sallary.";
cout << "\n\n\t\tWhat do you want to do?";
cin >> act;
if (act > 8)
cout << "Invalid request!" << endl;
switch (act)
{
case 1: {
cout << "\n\t\t_______________________________________________________________";
cout << "\n\n\t\t\'ADD NEW EMPLOYEES\'";
cout << "\n\t\tEnter number of the employees : ";
cin >> n;
data = new employ[n];
for (int i = 0; i < n; i )
{
cout << "\n\t\t========== Employee number " << i 1 << " ==========";
cout << "\n\t\tFirst name : ";
cin >> data[i].fn;
cout << "\t\tLast name : ";
cin >> data[i].ln;
cout << "\t\tEmployee's number : ";
cin >> data[i].emp_num;
cout << "\t\tDays of work : ";
cin >> data[i].work_days;
cout << "\t\tDaily rate : ";
cin >> data[i].payday;
}
cout << "\n\t\t=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=";
cout << "\n\t\tOperatuon's done successfully! =)";
cout << "\n\t\t=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
break;
}
case 2: {
cout << "\n\t\t_______________________________________________________________";
cout << "\n\n\t\t\'EDIT AN EMPLOYEE\'";
cout << "\n\t\tEnter the employee number : ";
int em_num;
cin >> em_num;
int yes = 0;
if (n == 0)
{
cout << "There's no employee!";
}
for (int i = 0; i < n; i )
{
if (em_num == data[i].emp_num)
{
for (int j = 0; j < n; j )
{
cout << "\n\t\t========== Edit Employee number " << i 1 << " ==========";
cout << "\n\t\tFirst name : ";
cin >> data[i].fn;
cout << "\t\tLast name : ";
cin >> data[i].ln;
cout << "\t\tEmployee's number : ";
cin >> data[i].emp_num;
cout << "\t\tDays of work : ";
cin >> data[i].work_days;
cout << "\t\tDaily rate : ";
cin >> data[i].payday;
yes ;
}
}
}
if (yes == 0)
{
cout << "The entered employee number is invalid.";
}
break;
}
default:
break;
}
} while (act != 0);
return 0;
}
CodePudding user response:
Regarding {}
: uniform initialization(C 11). It enables you to initialize everything in the same way. It also provides better safety guarantees when it comes to narrowing conversions.
The following code shows some examples.
#include <iostream>
#include <string>
#include <vector>
struct Point
{
Point(float x, float y) : m_x{ x }, m_y{ y } {}
float m_x;
float m_y;
};
int main()
{
int a{ 0 }; // same as a{}
int b{ 1 };
// int c{ 2.0 }; // at least warning, often error
double d{ 3.0 };
char* pc{nullptr}; // same as pc{}
std::string s{ "C " };
std::vector<int> vi{1, 2, 3, 4, 5};
Point p1{ 3.0f, 4.0f };
}