problem with arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 3
Reputation: LSUchemist is an unknown quantity at this point 
Solved Threads: 0
LSUchemist LSUchemist is offline Offline
Newbie Poster

problem with arrays

 
0
  #1
Oct 27th, 2009
I have an assignment to write a program using 1D and 2D parallel arrays with input coming from a .data file given to us. I am nearly a complete novice with writing code and right now I am having a very hard time getting my program to write any values from the arrays I created to my output file. I am not sure if it is an issue with the way I created the arrays, or the way I tried to output the information.

All of our work is done on the school's unix server, which we log into with putty on our own computers at home. If someone can tell me how to copy my work from the command prompt to paste here, so someone here could take a look at it for me, I'd really appreciate it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 63
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training
 
0
  #2
Oct 27th, 2009
what text editor in putty are you using? Can you not map your network drive and just open it in wordpad?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 455
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 52
jonsca jonsca is offline Offline
Posting Pro in Training
 
0
  #3
Oct 27th, 2009
Get a secure FTP program (e.g., http://www.coreftp.com/ but there are many others, or if your school doesn't use SSH you could use the insecure one in windows,ftp.exe ) and you should be able to log into your shell account and transfer files.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: LSUchemist is an unknown quantity at this point 
Solved Threads: 0
LSUchemist LSUchemist is offline Offline
Newbie Poster
 
0
  #4
Oct 27th, 2009
The editor I'm using is pico. Thanks for the link Jonsca, I can get the file now, but the code is all in one line, is there a way to keep the indentions without having to manualy go back and do it myself?
Last edited by LSUchemist; Oct 27th, 2009 at 6:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 455
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 52
jonsca jonsca is offline Offline
Posting Pro in Training
 
0
  #5
Oct 27th, 2009
Originally Posted by LSUchemist View Post
The editor I'm using is pico. I'm sure there is a way to simply open it up in wordpad, but like I said I'm a newb and I don't really know how.
I just tested it with putty now, highlight and hit control-C. There's also a command in the system menu in the upper left corner for Copy All To Clipboard, but that gets all your login stuff too and didn't get the Pico session when I tried it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 455
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 52
jonsca jonsca is offline Offline
Posting Pro in Training
 
0
  #6
Oct 27th, 2009
Originally Posted by LSUchemist View Post
The editor I'm using is pico. Thanks for the link Jonsca, I can get the file now, but the code is all in one line, is there a way to keep the indentions without having to manualy go back and do it myself?
There are converters (see http://en.wikipedia.org/wiki/CRLF for the reason why this occurs) but it's probably faster to do it by hand unless there are thousands of lines...
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: LSUchemist is an unknown quantity at this point 
Solved Threads: 0
LSUchemist LSUchemist is offline Offline
Newbie Poster
 
0
  #7
Oct 27th, 2009
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. //PROTOTYPES
  9. void SetFormat (ofstream& fout);
  10. void Header (ofstream& fout);
  11. void openFiles(ifstream& fin, ofstream& fout);
  12. int FillArrays(ifstream& fin, int employeeID[], int employeeTitleCode[],
  13. int countPayHis[]);
  14. void printArray(int employeeID[], int employeeTilteCode[],
  15. int payHistories[], ofstream& fout, int n);
  16.  
  17. const int MAX=50;
  18.  
  19.  
  20. int main()
  21. {
  22. ifstream fin;
  23. ofstream fout;
  24. int employeeID[MAX];
  25. int employeeTitleCode[MAX];
  26. int countPayHis[MAX];
  27. int n;
  28.  
  29. openFiles(fin, fout);
  30. Header(fout);
  31. n = FillArrays(fin, employeeID, employeeTitleCode, countPayHis);
  32. printArray(employeeID, employeeTitleCode, countPayHis, fout, n);
  33. return 0;
  34.  
  35. }// end main
  36.  
  37. void Header(ofstream& fout)
  38. {
  39. fout<<" BUSTER'S CATERING INC.\n\n"
  40. " Employee Data Calculator\n";
  41. }
  42.  
  43. void openFiles(ifstream& fin, ofstream& fout)
  44. {
  45. fin.open("Employees.data");
  46. if(fin.fail())
  47. {
  48. cout<< " The input file Employees.data failed to open.\n"
  49. " This program will now close.\n";
  50.  
  51.  
  52. exit(1);
  53. }
  54.  
  55. return;
  56. }
  57.  
  58. int FillArrays(ifstream& fin, int employeeID[], int employeeTitleCode[], int countPayHis[])
  59. {
  60. int ct = 0;
  61. int i = 0;
  62. int j = 0;
  63. int k = 0;
  64.  
  65. while(fin >> i)
  66. {
  67. fin >> j;
  68. fin >> k;
  69. employeeTitleCode[ct] = j;
  70. countPayHis[ct] = k;
  71.  
  72. ct++;
  73. }
  74. return ct;
  75. }
  76.  
  77.  
  78. void printArray(int employeeID[], int employeeTitleCode[], int countPayHis[], ofstream& fout, int n)
  79. {
  80. for(int ct = 0; ct < n; ct++)
  81. {
  82. fout << employeeID[ct];
  83. fout << employeeTitleCode[ct];
  84. while(fin >> i)
  85. {
  86. fin >> j;
  87. fin >> k;
  88. employeeTitleCode[ct] = j;
  89. countPayHis[ct] = k;
  90.  
  91. ct++;
  92. }
  93. return ct;
  94. }
  95.  
  96.  
  97. void printArray(int employeeID[], int employeeTitleCode[], int countPayHis[], ofstream& fout, int n)
  98. {
  99. for(int ct = 0; ct < n; ct++)
  100. {
  101. fout << employeeID[ct];
  102. fout << employeeTitleCode[ct];
  103. fout << countPayHis[ct] << endl;
  104.  
  105. }
  106. return;
  107. }
Last edited by LSUchemist; Oct 27th, 2009 at 6:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 455
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 52
jonsca jonsca is offline Offline
Posting Pro in Training
 
0
  #8
Oct 27th, 2009
Please put some code tags around it code /code (in brackets []) or highlight and hit the button on the toolbar of the editor
Last edited by jonsca; Oct 27th, 2009 at 6:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 455
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 52
jonsca jonsca is offline Offline
Posting Pro in Training
 
0
  #9
Oct 27th, 2009
You need to either pass in your fin into PrintArray, or more likely create a new ifstream object. Same with your j and k of the PrintArray() (the first one). They don't exist in the local scope (skim back through your text to clarify this for yourself). For these you can create a new local variable within that function.
Also, you are trying to overload a function PrintArray() with the same signature (i.e., int,int,int,ofstream&,int). How will the compiler tell the difference between the two? (it seems like you meant the one that returns ct to have a return type)
And, you don't need the return statements in your void functions.
Last edited by jonsca; Oct 27th, 2009 at 6:43 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC