please help me

Write your question here.
i want to Update the program to add a menu for choosing the operation (Search Data, Add a Record, Update Data, Quit)


.......
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct Salary
{
float basic_salary;
float deductions;
float bonuses;
float tax;
float net_salary;
};
struct Employee
{
string Name;
int ID;
Salary Sal1;
};

int main()
{
int i;
Employee Emp[3];
for( i=0; i<3; i++)
{
cout<<"Enter the Name of Employee "<<i+1<<": \n";
cin>> Emp[i].Name;
Emp[i].ID=i+1;
cout<<"Enter the basic_salary of Employee "<<i+1<<": \n";
cin>>Emp[i].Sal1.basic_salary;
cout<<"Enter the deductions of Employee "<<i+1<<": \n";
cin>> Emp[i].Sal1.deductions;
cout<<"Enter the bonuses of Employee "<<i+1<<": \n";
cin>> Emp[i].Sal1.bonuses;
if( Emp[i].Sal1.basic_salary<=499)
Emp[i].Sal1.tax=0.0;
else if( Emp[i].Sal1.basic_salary>=500 && Emp[i].Sal1.basic_salary<=800 )
Emp[i].Sal1.tax=0.07*Emp[i].Sal1.basic_salary;
else if( Emp[i].Sal1.basic_salary>=801 && Emp[i].Sal1.basic_salary<=1200)
Emp[i].Sal1.tax=0.10*Emp[i].Sal1.basic_salary;
else
Emp[i].Sal1.tax=0.15*Emp[i].Sal1.basic_salary;
Emp[i].Sal1.net_salary = Emp[i].Sal1.basic_salary + Emp[i].Sal1.bonuses - Emp[i].Sal1.deductions - Emp[i].Sal1.tax;
}

cout<<"Salaries Report"<<endl;

cout<<" ID "<<setw(25)<<" Name "<<setw(25)<<" Basic Salary "<<setw(25)<< " Bonuses " << setw(25)<<" Deductions " <<setw(25) <<" Tax " <<setw(25)<<" Net Salary "<<endl;
cout<< "========================================================================================================================"<<endl;

for( i=0; i<3;i++)
{
cout<<Emp[i].ID<<" "<<setw(25)<<Emp[i].Name<<setw(25)<<Emp[i].Sal1.basic_salary<<setw(25)<< Emp[i].Sal1.bonuses<< setw(25)<<Emp[i].Sal1.deductions <<setw(25) <<Emp[i].Sal1.tax <<setw(25)<<Emp[i].Sal1.net_salary<<endl;
}

cout<<" =========================================================="<<endl;
cout<<" =========================================================="<<endl;
bool found = false;
int Emp_searchid;
cout<<"Enter the Employee ID for searching "<<endl;
cin>> Emp_searchid;
for( i=0; i<3;i++)
if (Emp[i].ID==Emp_searchid)
{found =true; break;}
if (found)
cout<<Emp[i].ID<<" "<<setw(25)<<Emp[i].Name<<setw(25)<<Emp[i].Sal1.basic_salary<<setw(25)<< Emp[i].Sal1.bonuses<< setw(25)<<Emp[i].Sal1.deductions <<setw(25) <<Emp[i].Sal1.tax <<setw(25)<<Emp[i].Sal1.net_salary<<endl;
else
cout<<" The Employee is not found " ;

return 0;
}
Hello abuh,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Do you know about functions, do/while and while loops? Since the program does not show muck it helps to know what you can work with.

A few blank lines will help to make your code much easier to read. As an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
for (i = 0; i < 3; i++)
{
    cout << "Enter the Name of Employee " << i + 1 << ": \n";
    cin >> Emp[i].Name;

    Emp[i].ID = i + 1;

    cout << "Enter the basic_salary of Employee " << i + 1 << ": \n";
    cin >> Emp[i].Sal1.basic_salary;

    cout << "Enter the deductions of Employee " << i + 1 << ": \n";
    cin >> Emp[i].Sal1.deductions;

    cout << "Enter the bonuses of Employee " << i + 1 << ": \n";
    cin >> Emp[i].Sal1.bonuses;

    if (Emp[i].Sal1.basic_salary <= 499)
        Emp[i].Sal1.tax = 0.0;
    else if (Emp[i].Sal1.basic_salary >= 500 && Emp[i].Sal1.basic_salary <= 800)
        Emp[i].Sal1.tax = 0.07*Emp[i].Sal1.basic_salary;
    else if (Emp[i].Sal1.basic_salary >= 801 && Emp[i].Sal1.basic_salary <= 1200)
        Emp[i].Sal1.tax = 0.10*Emp[i].Sal1.basic_salary;
    else
        Emp[i].Sal1.tax = 0.15*Emp[i].Sal1.basic_salary;

    Emp[i].Sal1.net_salary = Emp[i].Sal1.basic_salary + Emp[i].Sal1.bonuses - Emp[i].Sal1.deductions - Emp[i].Sal1.tax;
}


Also unless there is a good reason for using "float" "double" is the preferred floating point type to use. A more accurate way to work with money is to use "int"s.

Andy
Well as a starter, consider (without input checking and not run-time tested):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

constexpr size_t MAXEMP {3};

struct Salary
{
	double basic_salary {};
	double deductions {};
	double bonuses {};
	double tax {};
	double net_salary {};
};

struct Employee
{
	string Name;
	size_t ID {};
	Salary Sal1;
};

using Employees = Employee[MAXEMP];

void search(const Employees Emp)
{
	size_t Emp_searchid {};

	cout << "Enter the Employee ID for searching: ";
	cin >> Emp_searchid;

	bool found {};
	size_t pos {};

	for (pos = 0; pos < MAXEMP; ++pos)
		if (Emp[pos].ID == Emp_searchid) {
			found = true;
			break;
		}

	if (found)
		cout << Emp[pos].ID << " " << setw(25) << Emp[pos].Name << setw(25) << Emp[pos].Sal1.basic_salary << setw(25) << Emp[pos].Sal1.bonuses << setw(25) << Emp[pos].Sal1.deductions << setw(25) << Emp[pos].Sal1.tax << setw(25) << Emp[pos].Sal1.net_salary << '\n';
	else
		cout << "The Employee is not found\n";
}

void add(Employees Emp)
{
	for (size_t i = 0; i < MAXEMP; ++i) {
		cout << "Enter the Name of Employee " << i + 1 << ": ";
		getline(cin >> ws, Emp[i].Name);
		Emp[i].ID = i + 1;

		cout << "Enter the basic_salary of Employee " << i + 1 << ": ";
		cin >> Emp[i].Sal1.basic_salary;

		cout << "Enter the deductions of Employee " << i + 1 << ": ";
		cin >> Emp[i].Sal1.deductions;

		cout << "Enter the bonuses of Employee " << i + 1 << ": ";
		cin >> Emp[i].Sal1.bonuses;

		if (Emp[i].Sal1.basic_salary <= 499)
			Emp[i].Sal1.tax = 0.0;
		else if (Emp[i].Sal1.basic_salary >= 500 && Emp[i].Sal1.basic_salary <= 800)
			Emp[i].Sal1.tax = 0.07 * Emp[i].Sal1.basic_salary;
		else if (Emp[i].Sal1.basic_salary >= 801 && Emp[i].Sal1.basic_salary <= 1200)
			Emp[i].Sal1.tax = 0.10 * Emp[i].Sal1.basic_salary;
		else
			Emp[i].Sal1.tax = 0.15 * Emp[i].Sal1.basic_salary;

		Emp[i].Sal1.net_salary = Emp[i].Sal1.basic_salary + Emp[i].Sal1.bonuses - Emp[i].Sal1.deductions - Emp[i].Sal1.tax;
	}
}

void report(const Employees Emp)
{
	constexpr size_t linesz {154};

	cout << "\nSalaries Report\n";

	cout << " ID " << setw(25) << " Name " << setw(25) << " Basic Salary " << setw(25) << " Bonuses " << setw(25) << " Deductions " << setw(25) << " Tax " << setw(25) << " Net Salary\n";
	cout << setw(linesz) << setfill('=') << "=\n" << setfill(' ');

	for (size_t i = 0; i < MAXEMP; ++i)
		cout << Emp[i].ID << " " << setw(25) << Emp[i].Name << setw(25) << Emp[i].Sal1.basic_salary << setw(25) << Emp[i].Sal1.bonuses << setw(25) << Emp[i].Sal1.deductions << setw(25) << Emp[i].Sal1.tax << setw(25) << Emp[i].Sal1.net_salary << endl;

	cout << setw(linesz) << setfill('=') << "=\n" << setfill(' ');
}

int main()
{
	Employee Emp[MAXEMP] {};

	size_t opt {};

	do {
		cout << "\n1. Enter data\n";
		cout << "2. Display data\n";
		cout << "3. Search data\n";
		cout << "0. Exit\n";

		cout << "\n Enter option: ";
		cin >> opt;

		switch (opt) {
			case 1: add(Emp); break;
			case 2: report(Emp); break;
			case 3: search(Emp); break;
			case 0: break;
			default: cout << "\nInvalid option\n"; break;
		}

	} while (opt);
}

Last edited on
seeplus there was a proplem.
I'm so sorry to hear there was a proplem.
Hello Abuh,

Please, avoid double posting <http://www.cplusplus.com/forum/beginner/276982/> the same question. Contrary to popular believe, it does not expedite the help.

Another tip I can give you is the same tip I give to the systems engineer, testing engineer and the program owner at my job, "Could you be more specific of what the problem is to perform the investigation of the defect?" In other words, what is the program doing that is wrong? What are the requirements? What is the code supposed to do? What is the code doing that is not supposed to do? How can you replicate the issue of the code? What I am getting at is the less that you are specific, the more time is wasted going back and forth on trying to figure out what is the issue.

Last edited on
seeplus there was a proplem.


Which is? Compile time or run-time?

If compile time, are you compiling as C++17? That code compiles OK with VS2019.

If run-time, then debug the program to find the problem.

Last edited on
seeplus i have a vs2013,the proplem is synatx error and error c4430 in line 6,24,117.
I am afraid your VS 2013 is far too old. Consider getting VS 2019, it's free for personal use.
@abuh, @thmm,
I use the clang 10 compiler from 2009 and I had no problem compiling his code using the -std=c++17 flag. Although I think it has had some updates in order to use that flag.

@seeplus,
A tip:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// To make it easier to read, consider:
for (size_t i = 0; i < MAXEMP; ++i)
{
     cout << Emp[i].ID << " " << setw(25)
          << Emp[i].Name << setw(25)
          << Emp[i].Sal1.basic_salary << setw(25)
          << Emp[i].Sal1.bonuses << setw(25)
          << Emp[i].Sal1.deductions << setw(25)
          << Emp[i].Sal1.tax << setw(25)
          << Emp[i].Sal1.net_salary << endl;
}
// instead of 

for (size_t i = 0; i < MAXEMP; ++i)
     cout << Emp[i].ID << " " << setw(25) << Emp[i].Name << setw(25) << Emp[i].Sal1.basic_salary << setw(25) << Emp[i].Sal1.bonuses << setw(25) << Emp[i].Sal1.deductions << setw(25) << Emp[i].Sal1.tax << setw(25) << Emp[i].Sal1.net_salary << endl;


Just a tip, because not many people I know like reading long lines of code that go over into the next line. Just easier to read and visually debug.

Have a good day!
max
Last edited on
agent max, your compiler is not from 2009 if it supports C++17.
Last edited on
Whoops, I meant to say my computer is from 2009, not my compiler. clang 10 was released in 2020, so it's pretty recent. Although you still have to set a dumb flag every time you want to compile something newer than C++11!!!
...I am not entirely sure what you mean...?
Topic archived. No new replies allowed.