CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jul 2010
    Posts
    22

    for loop question

    i have a for loop where i use an object to input information like this:

    I want the emp objects to increase as the loop goes around. so for each time to loop goes around i need it to go from emp1 to emp2 to emp3 up to max of 3. how would i go about doing this. thank you in advance

    Code:
    //Defines employee objects
    EmployeeClass emp1;
    EmployeeClass emp2;
    EmployeeClass emp3;
    
    //displays output so user can input information
    cout << "\nWelcome to the Employee Pay Center\n\n" ;
    for(int i = 0; i <= max; i++)
    
    //asking for information for employee 1
    cout << "Enter the employee name    = ";
    cin >> emp1.EmployeeName;
    cout << "Enter the hours worked     = ";
    cin >> emp1.hours;
    cout << "Enter his or her hourly wage   = ";
    cin >> emp1.wage;
    cout << endl;

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: for loop question

    You need an EmployeeClass array or vector.

    More than likely you want your loop to be < max, not <= max.

  3. #3
    Join Date
    Jul 2010
    Posts
    22

    Re: for loop question

    okay i got it now, so how do you do infinite array, for example i dont know how many people i will be inputing i just want to input until i say stop

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: for loop question

    so how do you do infinite array
    You can't define something 'infinite'. Just use a vector. You can keep adding to it.

  5. #5
    Join Date
    Dec 2009
    Posts
    145

    Re: for loop question

    To add, there is nothing infinite, only dynamic allocation exists, vector does have its own capacity to hold maximal number of elements

  6. #6
    Join Date
    Jul 2010
    Posts
    22

    Re: for loop question

    ill give it a try, thanks for you help. as you can tell i am new to c++ and only know that basics. but thanks for you help

  7. #7
    Join Date
    Jul 2010
    Posts
    22

    Re: for loop question

    so i did the array with my class but when i reach the last element in the array and the compiler gives me an error like this:

    '9.exe': Loaded 'C:\Users\bwilson\Desktop\Axis\9\Debug\9.exe', Symbols loaded.
    '9.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
    '9.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
    '9.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
    '9.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
    '9.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
    '9.exe': Loaded 'C:\Windows\System32\apphelp.dll', Cannot find or open the PDB file
    First-chance exception at 0x6565c9c7 (msvcr100d.dll) in 9.exe: 0xC0000005: Access violation reading location 0xcccccccc.
    Unhandled exception at 0x6565c9c7 (msvcr100d.dll) in 9.exe: 0xC0000005: Access violation reading location 0xcccccccc.

    what does all the mean? the code is below.

    Code:
    //Defines employee objects
    int i;
    EmployeeClass emp[3];
    //EmployeeClass emp1;
    //EmployeeClass emp2;
    //EmployeeClass emp3;
    
    //displays output so user can input information
    cout << "\nWelcome to the Employee Pay Center\n\n" ;
    //for(int i = 0; i <= max; i++)
    for(i = 0; i < 3; i++){
    //asking for information for employee 1
    cout << "Enter the employee name    = ";
    cin >> emp[i].EmployeeName;
    cout << "Enter the hours worked     = ";
    cin >> emp[i].hours;
    cout << "Enter his or her hourly wage   = ";
    cin >> emp[i].wage;
    cout << endl;
    }

  8. #8
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: for loop question

    this...

    '9.exe': Loaded 'C:\Users\bwilson\Desktop\Axis\9\Debug\9.exe', Symbols loaded.
    '9.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
    '9.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
    '9.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
    '9.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
    '9.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
    '9.exe': Loaded 'C:\Windows\System32\apphelp.dll', Cannot find or open the PDB file
    Is just information which can safely be ignored.

    this...
    0xC0000005: Access violation reading location 0xcccccccc.
    Is the reason why your program crashed. 'Access violation reading location 0xcccccccc' usually means that you are using a uninitialized pointer. Run your program in debug mode and when the crash happens you can see which line it is that causes the crash.

  9. #9
    Join Date
    Jul 2010
    Posts
    22

    Re: for loop question

    thats all it gave me, when it was finished it sent me to memcpy.asm and pointed to a line.

    Code:
            rep     movsd           ;N - move all of our dwords

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: for loop question

    Yeah, but check the call stack. Somewhere on there should be some of your code; that's the relevant bit.

  11. #11
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: for loop question

    May I start a guessing game?
    Is your EmployeeName a char*?
    Is it not initialized?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  12. #12
    Join Date
    Jul 2010
    Posts
    22

    Re: for loop question

    employee name is string and no and here is the complete code:

    Code:
    // 9.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    //
    //CLASS DECLARATION SECTION
    //
    class EmployeeClass {
    public:
    	void ImplementCalculations(string EmployeeName, double hours, double wage);
    	void DisplayEmployInformation(string EmployeeName, double basepay, double ot_hours, double ot_pay, double total_pay);
    	void Addsomethingup (EmployeeClass, EmployeeClass, EmployeeClass);
    	string EmployeeName ;
    	int hours ;
    	float wage ;
    	float basepay ;
       	int overtime_hours ;
    	float overtime_pay ;
    	float overtime_extra ;
    	float iTotal_salaries ;
    	float iIndividualSalary ;
    	int iTotal_hours ;
    	int iTotal_OvertimeHours ;
    };
    
    //main class
    int main(){
    
    system("cls");
    
    //Defines employee objects
    int i;
    EmployeeClass emp[3];
    //EmployeeClass emp1;
    //EmployeeClass emp2;
    //EmployeeClass emp3;
    
    //displays output so user can input information
    cout << "\nWelcome to the Employee Pay Center\n\n" ;
    //for(int i = 0; i <= max; i++)
    for(i = 0; i < 3; i++){
    //asking for information for employee 1
    cout << "Enter the employee name    = ";
    cin >> emp[i].EmployeeName;
    cout << "Enter the hours worked     = ";
    cin >> emp[i].hours;
    cout << "Enter his or her hourly wage   = ";
    cin >> emp[i].wage;
    cout << endl;
    }
    ////asking for information for employee 2
    //cout << "Enter the employee name    = ";
    //cin >> emp2.EmployeeName;
    //cout << "Enter the hours worked     = ";
    //cin >> emp2.hours;
    //cout << "Enter his or her hourly wage   = ";
    //cin >> emp2.wage;
    //cout << endl;
    //
    ////asking for information for employee 3
    //cout << "Enter the employee name    = ";
    //cin >> emp3.EmployeeName;
    //cout << "Enter the hours worked     = ";
    //cin >> emp3.hours;
    //cout << "Enter his or her hourly wage   = ";
    //cin >> emp3.wage;
    //cout << endl;
    
    //sends data to function to calculate totals and display information back
    emp[1].ImplementCalculations(emp[1].EmployeeName,emp[1].hours,emp[1].wage);
    emp[2].ImplementCalculations(emp[2].EmployeeName,emp[2].hours,emp[2].wage);
    emp[3].ImplementCalculations(emp[3].EmployeeName,emp[3].hours,emp[3].wage);
    
    //send all three employees to function to calculate totals for all employees
    emp[1].Addsomethingup(emp[1],emp[2],emp[3]);
    
    system("pause");
    
    } //End of Main Function
    
    //function for overtime calcuations
    void EmployeeClass::ImplementCalculations (string EmployeeName, double hours, double wage){
    //Initialize overtime variables
    overtime_hours=0;
    overtime_pay=0;
    overtime_extra=0;
    
    	if (hours > 40) //if hours are over 40 do this
    	{
                overtime_hours = hours - 40;
                overtime_pay = (wage * 1.5) * overtime_hours;
                basepay = (hours - overtime_hours) * wage;
                iIndividualSalary = (overtime_pay) + (basepay);
    
                DisplayEmployInformation(EmployeeName, basepay, overtime_hours, overtime_pay, iIndividualSalary);
    
    	}	// if (hours > 40)
            else if (hours <= 40) //if hours are under 40 do this
    	{
                basepay = hours * wage;
                iIndividualSalary = basepay;
    
                DisplayEmployInformation(EmployeeName, basepay, overtime_hours, overtime_pay, iIndividualSalary);
    
    
    	} // End of the else
    
    } //End of Primary Function
    
    //function to display individual information
    void EmployeeClass::DisplayEmployInformation (string EmployeeName, double basepay, double ot_hours, double ot_pay, double total_pay) {
    // This function displays all the employee output information.
    
        cout << "Employee Name ................ =" << EmployeeName << endl;
        cout << "Base Pay ..................... =" << basepay << endl;
        cout << "Hours in Overtime ............ =" << ot_hours << endl;
        cout << "overtime Pay Amount .......... =" << ot_pay << endl;
        cout << "Total Pay .................... =" << total_pay << endl << endl;
    
    
    } // END OF Display Employee Information
    
    //function to display summary of all employees information
    void EmployeeClass::Addsomethingup (EmployeeClass Employ1, EmployeeClass  Employ2, EmployeeClass Employ3){
    
        iTotal_hours = Employ1.hours + Employ2.hours + Employ3.hours;
        iTotal_salaries = Employ1.iIndividualSalary + Employ2.iIndividualSalary + Employ3.iIndividualSalary;
        iTotal_OvertimeHours = Employ1.overtime_hours + Employ2.overtime_hours + Employ3.overtime_hours;
    
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl
         << "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" << endl
         << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl
         << "%%%% Total Employee Salaries ..... = " << iTotal_salaries << endl
         << "%%%% Total Employee Hours ........ = " << iTotal_hours << endl
         << "%%%% Total Overtime Hours......... = " << iTotal_OvertimeHours << endl
         << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl
         << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    
    	} // End of function

  13. #13
    Join Date
    Jul 2009
    Posts
    46

    Re: for loop question

    Quote Originally Posted by exdox77 View Post
    ill give it a try, thanks for you help. as you can tell i am new to c++ and only know that basics. but thanks for you help
    If you don't know what the max size will be, shouldn't you use a vector and not a normal array?

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: for loop question

    Arrays are 0 based. Valid indexes for an array of size 3 are 0, 1 and 2, not 1, 2 and 3.

  15. #15
    Join Date
    Jul 2010
    Posts
    22

    Re: for loop question

    Are you kidding me I should known that. Arrays get me every time well thanks everyone for your help. Next step is to try the vector and grow my array as I input people. Any ideas to start from.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured