this project - functions and arrays - getting few errors

This is a discussion on this project - functions and arrays - getting few errors within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <iomanip> using namespace std; void getinput (int &grade, int &hrsworked, double finalbudget, int &employees); void calcpay ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    6

    this project - functions and arrays - getting few errors

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void getinput (int &grade, int &hrsworked, double finalbudget, int &employees);
    void calcpay (double rate, int hrsworked, double budget, double totalbudget, double regwork);
    void getdisp (int grade, int hrsworked, double totalbudget);
    
    double grade[3] = {25.50, 35.75, 50.00};
    	
    
    int main ()
    {
    
    	double totalbudget, rate, budget;
    
    	int grade, hrsworked;
    	
    	getinput (rate, hrsworked, finalbudget, employees);
    	
    	calcpay(rate, hrsworked, budget, totalbugdet);
    
    	getdisp (grade, hrsworked, totalbudget);
    	
    	return 0;
    
    }
    
    void getinput (double &grade, int&hrsworked, double finalbudget, int employees)
    {
    	{
    		for (employees = 0; employees < 4; employees++)
    		//cout <<grade[count-1]
    		cout << "Enter Salary grade from 1, 2 or 3 ";
    		cin >> grade;
    	}
    	
    	do 
    	{
    		cout << "Please Enter hours worked (> 0) ";
    		cin >> hrsworked;
    	}while (hrsworked <=0 );
    	{
    	cout << "Enter total Budget ";
    	cin >> finalbudget;
    	}
    }	
    void calcpay (double rate, int hrsworked, double budget, double totalbudget, double regwork)
    {
    
    	double overtime = 0;
    	if (hrsworked > regwork)
    	{
    		overtime = (hrsworked - regwork) * rate * 1.5;
    		budget = regwork * rate + overtime;
    	}
    	else
    	{
    		budget = hrsworked * rate;
    	}
    
    	totalbudget = (grade * budget) - finalbudget;
    }
    
    void getdisp (int grade, int hrsworked, double totalbudget)
    {
    	int budget;
    	cout <<fixed<< showpoint<<setprecision(2);
    	cout << "Employee #"<<employees<<grade<<hrsworked<<endl;
    	cout << "Your total Budget was " <<budget<<endl;
    
    	int under, over;
    
    	if (finalbudget > totalbudget)
    	{
    		under = finalbudget - totalbudget;
    		cout << "You are under the budget " <<under<<endl;
    	}
    	else if (finalbudget < totalbudget)
    	{	
    		over = finalbudget - totalbudget;
    		cout << "You are over the budget " <<over<<endl;
    	}
    	else 
    		cout << "You are right on the target "<<endl;
    }

    Please help me
    I am having a couple of problems when I run this...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you read the errors you got?

    finalbudget, employees, and totalbudget are not declared as variables; hence they do not exist; hence an attempt to pass them to a function will not work. You need to declare them in main; and if you expect to set their value in a function, you need to pass them as pointers.

    In calcpay, you attempt to multiply an array by a number, which is not valid. (And I'm not sure what you're trying to calculate there, so I don't know what it's supposed to be.)

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    how would i do that
    could you please me....

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How did I find the errors? I read the output of my compiler.

    How would you declare variables? You declared all the other variables you use; declare finalbudget, employees, and totalbudget the same way. You're passing everything else to getInput as a pointer; pass finalbudget the same way.

    I don't know what your logic is supposed to look like, so I can't say what you're supposed to be doing in calcpay.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    what were you sayin about the array thing

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    grade is defined as an array, but in your calcpay function you attempt to multiply it by budget.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And of course, everywhere in this thread I said "pass by pointers" of course I meant "pass by reference". C++ board. Right. Sorry.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    No, I have that grade array as a const...
    and trying to define that in dispinput...

    so in the grade if u enter 1...
    the grade value would be 25.50

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by arps789 View Post
    No, I have that grade array as a const...
    and trying to define that in dispinput...

    so in the grade if u enter 1...
    the grade value would be 25.50
    Look at this line in calcpay:
    Code:
    	totalbudget = (grade * budget) - finalbudget;
    grade is an array. It is, as you mentioned, a const array, but it is an array all the same. You cannot multiply an array by a number (or by anything, for that matter).

    If you want a specific value from that array, you need to index it. The first element is grade[0], the second grade[1], and the third grade[2].

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    so you are saying on the top I should just put it like this


    double grade[0] = 25.50;
    double grade[1] = 35.75;
    double grade[2] = 50.00;

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I am saying nothing of the sort. You need to have arrays beaten into your head, so go here and read up and take the quiz. We'll still be here when you're done. (This will also help in your other thread.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 03:24 AM
  2. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  3. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 10:41 PM
  4. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21