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

Edited 6 Years Ago by Chalson: n/a

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;

           }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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;

Edited 6 Years Ago by sundip: n/a

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 ^^

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.