cant display the correct value of the structured values with function ... is it got to do wif some buffer stuff ???

#include "stdafx.h" 
#include <iostream>
#include <iomanip>
using namespace std;

const int MAXCHARS = 15;
const int NUMEMPS = 1;
int i;

struct EmpRec
{
	long num;
	char name[MAXCHARS];
	double rate;
	double hours;
};

int main()
{

   int display();
   EmpRec payroll[NUMEMPS];
  

   for(i = 0; i < NUMEMPS; ++i)
   {
	 
     cout << "\nEnter employee number: ";
     cin  >> payroll[i].num;
	 
     cout << "Enter employee name: ";
     cin  >> payroll[i].name;
	 
     cout << "Enter employee rate: ";
     cin  >> payroll[i].rate;
	 
     cout << "Enter employee hours worked: ";
     cin  >> payroll[i].hours;
	 
   }

	   for( i = 0; i < NUMEMPS; ++i)
	   cout << setiosflags(ios::left) << setw(12) << payroll[i].name
			<< setiosflags(ios::right)
			<< setw(6) << payroll[i].num
			<< setiosflags(ios::showpoint | ios::fixed) << "     "
			<< setw(8) << setprecision(2)
			<< (payroll[i].rate * payroll[i].hours) << endl;

  display();

  return 0;
}
int display()
{

	EmpRec payroll[NUMEMPS];


   for( i = 0; i < NUMEMPS; ++i)
	   cout << setiosflags(ios::left) << setw(12) << payroll[i].name
			<< setiosflags(ios::right)
			<< setw(6) << payroll[i].num
			<< setiosflags(ios::showpoint | ios::fixed) << "     "
			<< setw(8) << setprecision(2)
			<< (payroll[i].rate * payroll[i].hours) << endl;

  cin.get();cin.ignore(); 
	return 0;
}

Recommended Answers

All 3 Replies

You need to pass EmpRec payroll[NUMEMPS]; to your display function and you should be fine. It didn't have any info stored in "display's EmpRec payroll[NUMEMPS]", so it just spit out whatever was stored when you initiated payroll again.

If you use a variable in a function and use the same variables in another function, they are two different things. If you want to use the same variable across functions, you need to either make sure it is a global variable or pass it as a parameter.
Change: display(); to display(payroll); int display(EmpRec payroll[]) to int display(EmpRec payroll[])

Also, the prototype for the display function should be outside of main( ), after the struct definition.

The display function doesn't return any useful value, so why not make it a void function?

thanks for the help ... still a newbie in C++ XD ... managed to solve it

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.