i am having trouble with arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ericstein81
    New Member
    • Jul 2007
    • 14

    i am having trouble with arrays

    I have tried to develop a code using arrays and void functions to take the current earnings of employees and using it to calculate their future income in ten years. This was performed by a 8% percent increase every year. below i've posted my attempt and for some reason the code will compile successfully however, when trying to run the program there are errors or bugs. If i can get any help it would be greatly appreciated.

    Ric712: [code=cpp]#include <iostream>
    #include <fstream>
    using namespace std;
    void input(char name[6][6], double salary[6], double &prog, int &j);
    void output(char name[6][6], double salary[6], double prog, int j);
    int main()
    {
    char name[6][6];
    double salary[6], prog;
    int j;
    input(name, salary, prog, j);
    output(name, salary, prog, j);
    return 0;
    }
    void input(char name[6][6], double salary[6], double &prog, int &j)
    {
    double p = 0.0;
    ifstream sam;
    sam.open("book. dat");
    while(!(sam.eof ()))
    {
    int i(0);
    j=j+1;
    sam >> name[j] >> salary [j];
    for(i=1; i <= 10; i++)
    {
    double inc = 0.0;
    inc = salary[j]*(1.08);
    p = inc + p;

    }
    }
    prog=p;
    sam.close();
    }// end input
    void output(char name[6][6], double salary[6], double prog, int j)
    {
    cout << "People and progressed salaries:\n";
    cout << "--------------------------------------------------\n";
    for(j=1;j<=6;j+ +)
    cout << name[j] << " " << salary [j] << endl;
    cout << "\nThe future salary in 10 years is " << prog << endl;
    }[/code]
    Last edited by sicarie; Jul 10 '07, 12:59 PM. Reason: Code tags
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    j is not initialized before you try to use it as an index into the name array. Judging by your code, you should be initializing it to 0. Otherwise you'll very likely crash when you get to this line:
    Code:
    sam >> name[j] >> salary [j];
    Also, since you only have room for six names in your name array, you should probably avoid using eof() as your loop terminator, as there could be more than 6 sets of data in the file. A for-loop would make more sense.

    Comment

    • ericstein81
      New Member
      • Jul 2007
      • 14

      #3
      Originally posted by scruggsy
      j is not initialized before you try to use it as an index into the name array. Judging by your code, you should be initializing it to 0. Otherwise you'll very likely crash when you get to this line:
      Code:
      sam >> name[j] >> salary [j];
      Also, since you only have room for six names in your name array, you should probably avoid using eof() as your loop terminator, as there could be more than 6 sets of data in the file. A for-loop would make more sense.
      Thank you Scruggsy, the program is running now. i forgot to mention that the data file used had only six names with individual payrolls. Thanks for your help

      Comment

      Working...