I'm using the 'struture' function to key in two type of data. First is the the position of employee, and another 1 is the salary of employee.

May sumbody tel me how to get and calculate the total amount of salary from my input ?
I need an example code if possible.. Thx

Recommended Answers

All 4 Replies

post the code you have so far.

Here you are:

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <string>

using namespace std;
float total_salary,total_sal;


int main()
{
    struct employee
    {
           char rank[3];
           int salary;

    };

           employee employ[3];
           for(int i=0;i<3;i++){
           cout<<"enter rank pls.type # when finish";
           int j=0;char letter;

           while (true)
           {
                 cin>>letter;
                 if (letter=='#')
                 break;
                 else 
                 employ[i].rank[j]=letter;
                 ++j;
           }

           employ[i].rank[j]='\0';
           cout<<"Enter amount of salary: ";
           cin>>employ[i].salary;
           }

           for(int i=0;i<3;i++)
           {
           cout<<"rank is "<<employ[i].rank<<endl;
           cout<<"salary is "<<employ[i].salary<<endl;
           }

            system("PAUSE");


           for (int i=0;i<3;i++)
           {

               total_sal=total_salary+employ[i].salary;

               cout<<"total is "<<total_salary<<endl;}




           getch();
           return 0;

           }

Just replace

for (int i=0;i<3;i++)
{

total_sal=total_salary+employ[i].salary;

cout<<"total is "<<total_salary<<endl;}

with

total_salary = 0;
for (int i=0;i<3;i++)
{

total_sal=total_salary+employ[i].salary;
} // End of loop to calculate total salary
cout<<"total is "<<total_salary<<endl;

total_salary = 0;

I think some corrections here... xD

       for (int i=0;i<3;i++)
       {

           total_salary=total_salary+employ[i].salary;
       } // End of loop to calculate total salary

       cout<<"total is "<<total_salary<<endl;

BTW, i really appreciate ur help. A great thanks to both of u ^^

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.