Read File Issue

Thread Solved
Reply

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

Read File Issue

 
0
  #1
Nov 17th, 2008
I have been writing a Loan Program that requires at one point to read a .txt file and create an array from the contents.

This part is nothing new to me. The problem I have is a single line of code and I cant figure
out how to make it work.


It has to do with adding two chars and a double to a single pointer in an array.

It uses a class.
I have yet to find code to do this properly.

Here is an sample of my code

  1. int* readData(string fname, int & arrSz){
  2.  
  3. int* result = NULL;
  4. arrSz = 0;
  5. char f;
  6. char s;
  7. double bal;
  8.  
  9. // Create a file object and open the file
  10. ifstream inStream;
  11. inStream.open(fname.c_str());
  12.  
  13. // Only process file if opening it is successful
  14. if(!inStream.fail()){
  15. inStream >> arrSz;
  16. result = new int[arrSz];
  17.  
  18. // Read file contents into result, now that size is known
  19. //create results array
  20. for(int i = 0; i < arrSz ; i++){
  21. inStream >> f >> s >> bal;
  22. result[i] = Customer(f,s,bal);
  23. }
  24. inStream.close(); //don't forget to close file
  25. }
  26. return result;

the problem comes with the result[i] = Customer(f,s,bal) line
At the moment it only is set up like this for reference to what I want to do.
Class file and header files are separate.

The file that is read has a single number for the first line, to be read as arraySz
Then has each corresponding line as two Letters and a number.

If I have left any information out that is needed to help please clarify.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,670
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: 192
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Read File Issue

 
0
  #2
Nov 18th, 2008
What is your problem with result[i] = Customer(f,s,bal); What does Customer( ) return?
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
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: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Read File Issue

 
0
  #3
Nov 18th, 2008
I've looked at your post for about 5 minutes and have absolutely no idea what your question is about! OK, you're having a problem with a line. The line is only for reference -- whatever that means. Where do you explain what the problem is? Or what happens to show you it's wrong?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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
  #4
Nov 19th, 2008
I apologize for not making that clear. That line is reference because it was given to me like that for reference. I want to take those three variables that are enclosed by 'customer' and insert them into each part of return[i] array. I do not know how to do this. I keep getting errors with each method I try. I do not have an outside function for customer(). It does not exist outside this function.

I mean really I do not know if i am going about the right way of getting this done. Can it be done?

Do I have to create more thatn one array? Can I add structures to an array?

If this can be done I need a bit of guidance.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,670
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: 192
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Read File Issue

 
0
  #5
Nov 19th, 2008
Is customer supposed to be a struct or class? Are you creating an array filled with such objects? Or, are you supposed to write a Customer function that gives back some single value based on the three input values?

You need to be more detailed in the problem description, and show your complete code.

The batteries in my crystal ball need recharging.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
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
  #6
Nov 19th, 2008
OK. Customer() doesnt exist in any of my files. So far I have

Loan.h --- Header File

Loan.cpp --- mutator file

Loanmain.cpp --- Main C++ file.

I have my load class already made and my loan mutators already created.

The code I have shown above is a single function called by my main function in
the Loanmain.cpp
The main function is supposed to call this read file function and the read file function returns a dynamic array.
That array is supposed to have the data received from the file that is called.
The issue is that the file contains three variables per line, and each time the loop repeats itself in my read file function, I need to put those three variables into one array spot.

I asked around elsewhere and it turns out that Customer is actually supposed to be part
of my class calls. That call being my main constructor 'Loan()'.
However even this returns a undeclared identifier error.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,670
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: 192
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Read File Issue

 
0
  #7
Nov 19th, 2008
So, it appears you're supposed to have a Customer class, and the array that you're dealing with in your first post should be of type Customer, not type int.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
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
  #8
Nov 19th, 2008
error C2440: '=' : cannot convert from 'Loanc' to 'int'

Thats the error I keep getting.

Where Loanc is my Class file, equivalent to what customer is in my first post.

If I were to change the results are to a class type...how would I do this?
As far as I know this function still returns a integer... and the compiler would not understand returning a class if the function is a integer type.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,670
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: 192
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Read File Issue

 
0
  #9
Nov 19th, 2008
I still don't have my crystal ball charged up.

Without seeing your code, especially the loanc stuff, we can't give you any substantial help. From what you post, Customer( ) is creating/returning an object of type loanc? Then that's the type you must use for the array declaration.
  1. loanc* result = NULL;
  2.  
  3. //blah blah
  4.  
  5. result = new loanc[arrSz];
  6.  
  7. //blah blah
  8.  
  9. result[i] = Customer(f,s,bal);

Try that.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
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
  #10
Nov 20th, 2008
error C2440: '=' : cannot convert from 'Loanc' to 'int'

..still getting the same error.

Here is my whole project.

The reason I didnt post it all before was because I thought it was much more simple.

This might be a bit of a long post:

Loanc.h
  1. // Loanc.h
  2.  
  3. #pragma once
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. class Loanc{ // TODO: Add your methods for this class here.
  8. public:
  9.  
  10. Loanc();
  11. Loanc(char f, char s, double b);
  12.  
  13. double getFirst();
  14. double getSeccond();
  15. double getBalence();
  16.  
  17. double changeBalance(double ch);
  18.  
  19. bool operator<(const Loanc & l)const;
  20. bool operator>(const Loanc & l)const;
  21. bool operator<=(const Loanc & l)const;
  22. bool operator>=(const Loanc & l)const;
  23. bool operator!=(const Loanc & l)const;
  24. bool operator==(const Loanc & l)const;
  25. friend ostream & operator<<(ostream & os, const Loanc & l);
  26.  
  27.  
  28. private:
  29.  
  30. char first;
  31. char seccond;
  32. double balence;
  33. };
Loanc.cpp
  1. #include "StdAfx.h"
  2. #include <iostream>
  3. #include "Loanc.h"
  4.  
  5. Loanc::Loanc(){
  6. first = 'A';
  7. seccond = 'A';
  8. balence = 0.0;
  9. }
  10.  
  11. Loanc::Loanc(char f, char s, double b){
  12. first = f;
  13. seccond = s;
  14. balence = b;
  15. }
  16. double Loanc::getFirst(){
  17. return first;
  18. }
  19. double Loanc::getSeccond(){
  20. return seccond;
  21. }
  22. double Loanc::getBalence(){
  23. return balence;
  24. }
  25.  
  26. double Loanc::changeBalance(double ch){
  27. double temp = 0;
  28. balence = balence + ch;
  29. if(balence >= 0){
  30. return 0;
  31. }
  32. if(balence < 0){
  33. balence = temp;
  34. balence = 0;
  35. return temp;
  36. }
  37. }
  38. bool Loanc::operator<(const Loanc & l)const{
  39. if(first < l.first){
  40. return true;}
  41. if(seccond < l.seccond){
  42. return true;
  43. }else{
  44. return false;
  45. }
  46.  
  47. }
  48. bool Loanc::operator>(const Loanc & l)const{
  49. if(first > l.first){
  50. return true;}
  51. if(seccond > l.seccond){
  52. return true;
  53. }else{
  54. return false;
  55. }
  56. }
  57. bool Loanc::operator<=(const Loanc & l)const{
  58. if(seccond == l.seccond && first == l.first){
  59. return false;
  60. }else{
  61. return true;
  62. }
  63. if(first < l.first){
  64. return true;}
  65. if(seccond < l.seccond){
  66. return true;
  67. }else{
  68. return false;
  69. }
  70. }
  71. bool Loanc::operator>=(const Loanc & l)const{
  72. if(seccond == l.seccond && first == l.first){
  73. return false;
  74. }else{
  75. return true;
  76. }
  77. if(first > l.first){
  78. return true;}
  79. if(seccond > l.seccond){
  80. return true;
  81. }else{
  82. return false;
  83. }
  84. }
  85. bool Loanc::operator!=(const Loanc & l)const{
  86. if(seccond == l.seccond && first == l.first){
  87. return false;
  88. }else{
  89. return true;
  90. }
  91. }
  92. bool Loanc::operator==(const Loanc & l)const{
  93. if(seccond == l.seccond && first == l.first){
  94. return true;
  95. }else{
  96. return false;
  97. }
  98. }
  99. ostream & operator <<(ostream & os, const Loanc & l){
  100. os << l.first << l.seccond << " $" << l.balence;
  101. return os;
  102. }

Loanclass.cpp
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cmath>
  4. #include "Loanc.h"
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. int* readData(string fname, int & arrSz);
  10. void printArray(int arr[], int sz);
  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. int* fileArr = readData(fname, fileSize);
  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. int* readData(string fname, int & arrSz){
  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(int arr[], int sz){
  77. cout << "{";
  78. for (int i = 0; i < sz; i++){
  79. cout << arr[i];
  80. if (i < sz - 1){
  81. cout << ",";
  82. }
  83. }
  84. cout << "}";
  85. }


As you can see customer doesnt exist. It is represented by Loanc(). But even with this information, I ca seem to get it to work. Ive been told by more than one person I had to change my result[] type form int to Loanc type, but that error popped up.

Im so confused at whats going on and how to get around this.

If this code is at all hard to navigate I apologize in advance, I havent implemented any more of my comments or indentations.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC