functions are killing me

This is a discussion on functions are killing me within the C++ Programming forums, part of the General Programming Boards category; I have this program and I can get everything to work except for the calculation of hours worked. Can someone ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    4

    functions are killing me

    I have this program and I can get everything to work except for the calculation of hours worked. Can someone look at my program and tell me what I have done wrong in using voide functions and reference parameters?
    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    void initialize(int& a, int& b, char c);
    void getHoursRate(double& h, double& r);
    double payCheck(double h, double r, double wages);
    void printCheck(double& hrs, double& rte, double& wage);
    void funcOne(int& d, int& e);
    void nextChar(char& t);
    
    int main()
    {
        int x,y;
        char z, ch;
        double hours, rate;
        double amount;
        
        
        
        initialize(x,y,z);
        cout<<"After initilialization: x = "<<x<<" y = "<<y<<" z= "<<z<<endl;
         
        
        cout<<fixed<<setprecision(2); 
        hours = 0;
        rate = 0; 
        amount = 0;
        
        getHoursRate(hours, rate);
        payCheck(hours, rate,amount);
        printCheck(hours, rate, amount);
        
        x=35;
        y=20;
        cout<<"Before calling funcOne x = "<<x<< ", y = "<<y<<endl;
        funcOne(x,y);
        cout<<"After funcOne: x = "<<x<<endl;
        
        z = 'B';
        cout<<"z = "<<z<<endl;
        nextChar(z);
        cout<<"After nextChar: z = "<<z<<endl;
        
        system ("pause"); 
        cout<<"Press any key to continue "<<endl;
        cin.get(ch);
        
        return 0;
        
    }
    
    
    //function definitions
    void initialize(int& a, int& b, char c)            //definition of initialize step 1A.
    {
        a = 0;
        b=0;
        c = ' ';
        
    }
    
    void getHoursRate(double& h, double& r)      //definition of getHoursRate step 1B.      
    {
        
        cout<<"Enter hours worked: "<<endl;
        cin>>h;
    
        cout<<"Enter pay rate: "<<endl;
        cin>>r;
        
    }
    
    double payCheck(double h, double r, double wages)
    {
        if (h > 40.00)
        wages = (40.0 * r) + 
            (1.5 * r * (h - 40.0));
        
        else
        wages = h * r;
            
        return wages;
    }
    
    void printCheck(double& hrs, double& rte, double& wage)
    {
        cout<<fixed<<setprecision(0);
        cout<<setfill(' ')<<left<<setw(25)<<"Hours worked: "<<right<<setw(10)<<hrs<<endl;
        cout<<setfill(' ')<<left<<setw(25)<<"Pay Rate:  " <<right<<setw(10)<<"$"<<rte<<endl;
        cout<<setfill(' ')<<left<<setw(25)<<"This week's salary:  "<<right<<setw(10)<<"$"<<wage<<endl;
        
    }
    
    void funcOne(int& d, int& e)
    {
        int num;
        
        cout<<"Enter an integer: "<<endl;
        cin>>num;
        d = (2 * d) + (e - num);
        
        
    }
    
    void nextChar(char& t)
    {
           t = 'C';
           
    }
    In the void printCheck function I should be able to get the hours worked, the rate of pay and the amount paid. But all I get are zero's. HELP!
    Last edited by 77walker; 11-13-2004 at 01:06 AM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is a little something I whipped up.. (it took me weeks to do this program in assembly.. what took me 20 minutes to write in c++) Anywhoo.. it demostrates functions.. function prototyping.. function definitions.. how to pass values into your functions.. and how to use the return value of your functions. Also, it demonstrates how to use some of the <cctype> library functions. It's also good for those who haven't got into classes yet.


    Code:
    #include<iostream>
    #include<cstring>
    #include<cctype>
    #include<conio.h>
    
    using namespace std;
    
    //Function Prototypes
    bool is_vowel(const char&);      
    int  get_vowels(char*);
    int  get_consonants(char*);
    int  get_numbers(char*);
    
    //Global Variables
    int other;
    
    int main()
    {
    
    	do{
    
    	::other = 0;
    
    	char input[81];
    
    	clrscr();
    	
    	cout << "\n\tWelcome to String Manager";
    	cout << "\n\n\n\nEnter String: ";
    
    	cin.getline(input, 80);
    
    	cout << endl << endl;
    	cout << "\n\nThere are " << get_vowels(input)		<< " vowels.";
    	cout << "\n\nThere are " << get_consonants(input)	<< " consonants.";
    	cout << "\n\nThere are " << get_numbers(input)		<< " numbers.";
    	cout << "\n\nThere are " << strlen(input)-::other	<< " other characters.";
    	cout << "\n\nThere are " << strlen(input)		<< " total characters.";
    	cout << "\n\n\nAgain? (Y/N) ";
    	
    	}while (toupper(getch()) == 'Y');
    
    	return 0;	
    }
    
    
    
    //Function Definitions
    bool is_vowel(const char& input)
    {
    	switch(toupper(input))
    	{
    		case 'A': 
    		case 'E':
    		case 'I':
    		case 'O':
    		case 'U': return true;	
    
    		default:  return false;
    	}
    }
    
    
    int get_vowels(char* input)
    {
    	int counter = 0;
    
    	for (int c=0; c < strlen(input); c++)
    	{
    		if(is_vowel(input[c]))
    		{
    			++counter;
    			++::other;
    		}
    	}
    
    	return counter;
    }
    
    
    
    int get_consonants(char* input)
    {
    	int counter = 0;
    
    	for (int c=0; c < strlen(input); c++)
    	{
    		//IF input[c] is an alphabetic character, but not a vowel
    		if(isalpha(input[c]) && !is_vowel(input[c]))
    		{
    			++counter;
    			++::other;
    		}
    	}
    
    	return counter;
    }
    
    
    
    int get_numbers(char* input) 
    {
    	int counter = 0;
    
    	for (int c=0; c < strlen(input); c++)
    	{
    		if(isdigit(input[c]))
    		{
    			++counter;
    			++::other;
    		}
    	}
    
    	return counter;
    }
    Last edited by The Brain; 11-13-2004 at 12:38 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    4

    Unhappy

    I am a new programmer and I'm sorry if my code was a little difficult to read, but I don't think you had to yell at me so loudly when I am asking for help.

    I will try using the code tags in any of my future postings. Thanks for your comments.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    You just need a variable to hold the return value for
    Code:
    payCheck(hours, rate,amount);
    ie
    Code:
    double value=payCheck(hours, rate,amount);
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Thanks for the pointer. I am trying right now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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