Thread: Vector problem

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

    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;
       }
    }
    Double Helix STL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,688
    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.
    Double Helix STL

Popular pages Recent additions subscribe to a feed