Read File Issue

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Read File Issue

 
0
  #11
Nov 20th, 2008
You need to make all your arrays that deal with the Loanc data type of that type - not int. In the case of function readData, it needs to have a return type of Loanc*, not int*. You will also need to modify your print function - it's fine for an int array, but has no clue about an array of Loanc object.

Remember, in a function, the type of value you return must agree with the datatype of the function header/prototype.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: CMacDady is an unknown quantity at this point 
Solved Threads: 0
CMacDady CMacDady is offline Offline
Newbie Poster

Re: Read File Issue

 
0
  #12
Nov 20th, 2008
I understand that much but when I plug Loanc into the function type, the compiler starts cursing at me saying that it isnt a viable function type. I cant seem to get the return type and the function type to match.

And yeah print array was giving me difficulties so i commented it out till I get the first problem sorted out.
Last edited by CMacDady; Nov 20th, 2008 at 6:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Read File Issue

 
0
  #13
Nov 20th, 2008
Here's your loanclass.cpp fixe. It now compiles. I won't swear that it's actually working correctly, as I've no data file to test with. Things I fixed are commented.
  1. //#include "stdafx.h" //do you really need this?
  2. #include <iostream>
  3. #include <cmath>
  4. #include "Loanc.h"
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. Loanc* readData(string fname, int & arrSz); //FIXED
  10. void printArray(Loanc arr[], int sz); //FIXED
  11.  
  12.  
  13. int main(){
  14.  
  15. cout << "Test to Create and display Loans: " << endl;
  16. Loanc l1;
  17. Loanc l2('H', 'A', 700);
  18. Loanc l3('H', 'X', 400);
  19. cout << "Add $500 to AA, returns " << l1.changeBalance(500) << ", " << l1 << endl;
  20. cout << "Subtract $500 from HA, returns " << l2.changeBalance(-500) << ", " << l2 << endl;
  21. cout << "Subtract $600 from HX, returns " << l3.changeBalance(-600) << ", " << l3 << endl;
  22. cout << "END LOAN TEST." << endl;
  23.  
  24.  
  25. int resultSize = 0;
  26. int fileSize = 0;
  27.  
  28. string fname = "cmpt128a4data1.txt";
  29.  
  30. //Read test file and print it
  31. Loanc* fileArr = readData(fname, fileSize); //FIXED
  32. if (fileArr != NULL){
  33. printArray(fileArr, fileSize);
  34. //cout << "resultSize initially set to " << resultSize << endl;
  35. //int arr[] = {1,2,2,2,4,5,7,7,7};
  36.  
  37. }else{
  38. cout << "File not read proporly";
  39. }
  40.  
  41. cout << endl;
  42.  
  43. // Free up dynamic memory
  44. delete[] fileArr;
  45. system("pause");
  46. return 0;
  47. }
  48.  
  49. Loanc* readData(string fname, int & arrSz){ //FIXED
  50.  
  51. Loanc* result = NULL;
  52. arrSz = 0;
  53. char f;
  54. char s;
  55. double bal;
  56.  
  57. // Create a file object and open the file
  58. ifstream inStream;
  59. inStream.open(fname.c_str());
  60.  
  61. // Only process file if opening it is successful
  62. if(!inStream.fail()){
  63. inStream >> arrSz;
  64. result = new Loanc[arrSz];
  65.  
  66. // Read file contents into result, now that size is known
  67. //create results array
  68. for(int i = 0; i < arrSz ; i++){
  69. inStream >> f >> s >> bal;
  70. result[i] = Loanc(f,s,bal);
  71. }
  72. inStream.close(); //don't forget to close file
  73. }
  74. return result;
  75. }
  76. void printArray(Loanc arr[], int sz){ //FIXED
  77. //cout << "{";
  78. //for (int i = 0; i < sz; i++){
  79. // cout << arr[i];
  80. // if (i < sz - 1){ //you gotta fix the body
  81. // cout << ",";
  82. // }
  83. //}
  84. //cout << "}";
  85. }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: CMacDady is an unknown quantity at this point 
Solved Threads: 0
CMacDady CMacDady is offline Offline
Newbie Poster

Re: Read File Issue

 
0
  #14
Nov 20th, 2008
-.-.... I forgot to change to forward decoration... god I feel dumb

Thank you for all your help. I have a much broader understanding of how types work.
Last edited by CMacDady; Nov 20th, 2008 at 6:42 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 977 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC