Vector problem

This is a discussion on Vector problem within the C++ Programming forums, part of the General Programming Boards category; Hi I never usually post assignments I need help on but this one has me stumped. This program is not ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Vector problem

    Hi I never usually post assignments I need help on but this one has me stumped. This program is not finished and its basically in dummy format. I have been adding a little bit as I go along.

    Anyway the assignment is to create a simple payroll program using one dimensional arrays and a vector to store the names. I am not allowed to use OOP which would make things easier. The problem lies in the first function after main. It never allows me to enter the correct amount of names. This value is passed from main and entered by the user.

    For exmaple, if I type in 3 names to process I can only enter 2 names but the other functions appear to be working as nornal.

    I dont know what I am doing wrong, the rest of the code works as it should. I declared the vector correct in the function.

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <vector>
    
    // function prototyes
    void inputNames ( int& );
    void inputPayRate ( const std::vector<std::string> &, int& );
    void inputHoursAndProcessResult ( const std::vector<std::string> &,
         int&, double[] );
    
    // main - begins program execution /////////////////////////////////////////////
    //
    int main(int argc, char *argv[])
    {
       std::cout << std::setprecision( 2 ) << std::fixed;
    
       int totalEmployees;
    
       std::cout << "Enter total amount of employees to compute: ";
       std::cin >> totalEmployees;
    
       while ( totalEmployees != -1 )
       {
          inputNames ( totalEmployees );
    
          std::cout << "\nEnter total amount of employees to compute: ";
          std::cin >> totalEmployees;
       }
    
       std::cin.get(); // freeze console output window
    
       return 0; // return value from main to OS
    }
    
    // funcion to input the names of the employees
    void inputNames ( int &rTotEmp )
    {
       std::vector<std::string> names( rTotEmp, "" );
    
       std::cout << "\n\nEnter the " << rTotEmp << " employee names pressing return"
                 << "\nafter each input\n\n> ";
    
       for ( int i = 0; i < names.size(); i++ )
       {
          getline ( std::cin, names[ i ] );
       }
    
       inputPayRate ( names, rTotEmp );
    }
    
    // function to enter the hourly rate of each employee
    void inputPayRate ( const std::vector<std::string> &vec, int &rTotEmp )
    {
       double payRate [ rTotEmp ];
    
       std::cout << "\n\nEnter the " << rTotEmp << " pay rate for each\n"
                 << "employee pressing return after each input\n\n> ";
    
       for ( int i = 0; i < rTotEmp; i++ )
       {
          std::cin >> payRate[ i ];
       }
    
       inputHoursAndProcessResult ( vec, rTotEmp, payRate );
    }
    
    // function to input the hours worked, calcuate salary and
    // display result it tabular format
    void inputHoursAndProcessResult ( const std::vector<std::string> &vec,
         int &rTotEmp, double pay[] )
    {
       double hours [ rTotEmp ];
    
       std::cout << "\n\nEnter the " << rTotEmp << " hours for each\n"
                 << "employee pressing return after each inpuy\n\n> ";
    
       for ( int i = 0; i < rTotEmp; i++ )
       {
          std::cin >> hours[ i ];
       }
    
       std::cout << "\nEMPLOYEE NAME" << std::setw( 12 ) << "PAY RATE"
                 << std::setw( 12 ) << "HOURS\n\n";
    
       // display final data in table format
       for ( int i = 0; i < rTotEmp; i++ )
       {
          std::cout << vec[ i ] << std::setw( 19 ) << pay[ i ] << std::setw( 19 )
                    << hours[ i ] << std::endl;
       }
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,985
    The probable reason is the usual "new line left on buffer after std::cin >> totalEmployees".

    Incidentally, wouldnt it be better if inputNames() returned a vector of names instead of calling inputPayRate()? That seems to fit in better with "do one thing and do it well".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Thanks laserlight. Yes I am going to modify the code format and layout once I have the core of the program working correctly.

    Thanks for the function return suggestion that would make it more efficent.
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

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