By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

i am having trouble with arrays

P: 14
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <fstream> 
  3. using namespace std; 
  4. void input(char name[6][6], double salary[6], double &prog, int &j); 
  5. void output(char name[6][6], double salary[6], double prog, int j); 
  6. int main() 
  7. char name[6][6]; 
  8. double salary[6], prog; 
  9. int j; 
  10. input(name, salary, prog, j); 
  11. output(name, salary, prog, j); 
  12. return 0; 
  13. void input(char name[6][6], double salary[6], double &prog, int &j) 
  14. double p = 0.0; 
  15. ifstream sam; 
  16. sam.open("book.dat"); 
  17. while(!(sam.eof())) 
  18. int i(0); 
  19. j=j+1; 
  20. sam >> name[j] >> salary [j]; 
  21. for(i=1; i <= 10; i++) 
  22. double inc = 0.0; 
  23. inc = salary[j]*(1.08); 
  24. p = inc + p; 
  25.  
  26. prog=p; 
  27. sam.close(); 
  28. }// end input 
  29. void output(char name[6][6], double salary[6], double prog, int j) 
  30. cout << "People and progressed salaries:\n"; 
  31. cout << "--------------------------------------------------\n"; 
  32. for(j=1;j<=6;j++) 
  33. cout << name[j] << " " << salary [j] << endl; 
  34. cout << "\nThe future salary in 10 years is " << prog << endl; 
  35. }
Jul 10 '07 #1
Share this Question
Share on Google+
2 Replies


100+
P: 147
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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Jul 10 '07 #2

P: 14
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:
Expand|Select|Wrap|Line Numbers
  1. 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
Jul 10 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.