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

int* readData(string fname, int & arrSz){
	
	int* result = NULL;
	arrSz = 0;
	char f;
	char s;
	double bal;
	
	// Create a file object and open the file
	ifstream inStream;
	inStream.open(fname.c_str());
	
	// Only process file if opening it is successful
	if(!inStream.fail()){
	inStream >> arrSz;
	result = new int[arrSz];

		// Read file contents into result, now that size is known
		 //create results array
		for(int i = 0; i < arrSz ; i++){
			inStream >> f >> s >> bal;
			result[i] = Customer(f,s,bal); 
		}
			inStream.close(); //don't forget to close file
	}
	return result;

the problem comes with the result = 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.

Recommended Answers

All 13 Replies

What is your problem with result[i] = Customer(f,s,bal); What does Customer( ) return?

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?

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 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.

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. :P

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.

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.

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.

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.

loanc* result = NULL;

//blah blah

result = new loanc[arrSz];

//blah blah

result[i] = Customer(f,s,bal);

Try that.

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

// Loanc.h

#pragma once
#include <iostream>
using namespace std;

class Loanc{	// TODO: Add your methods for this class here.
	public:
	
	Loanc();
	Loanc(char f, char s, double b);

	double getFirst();
	double getSeccond();
	double getBalence();

	double changeBalance(double ch);

	bool operator<(const Loanc & l)const;
	bool operator>(const Loanc & l)const;
	bool operator<=(const Loanc & l)const;
	bool operator>=(const Loanc & l)const;
	bool operator!=(const Loanc & l)const;
	bool operator==(const Loanc & l)const;
	friend ostream & operator<<(ostream & os, const Loanc & l);
	
	
	private:
	
		char first;
		char seccond;
		double balence;
	};

Loanc.cpp

#include "StdAfx.h"
#include <iostream>
#include "Loanc.h"

Loanc::Loanc(){
first =  'A';
seccond =  'A';
balence = 0.0;
}

Loanc::Loanc(char f, char s, double b){
first = f;
seccond = s;
balence = b;
}
double Loanc::getFirst(){
return first;
}
double Loanc::getSeccond(){
return seccond;
}
double Loanc::getBalence(){
return balence;
}

double Loanc::changeBalance(double ch){
	double temp = 0;
	balence = balence + ch;
		if(balence >= 0){
			return 0;
		}
		if(balence < 0){
			balence = temp;
			balence = 0;
			return temp;
		}
}
bool Loanc::operator<(const Loanc & l)const{
			if(first < l.first){
		return true;}
			if(seccond < l.seccond){
		return true;
			}else{
		return false;
	}
	
}
bool Loanc::operator>(const Loanc & l)const{
			if(first > l.first){
		return true;}
			if(seccond > l.seccond){
		return true;
			}else{
		return false;
	}
}
bool Loanc::operator<=(const Loanc & l)const{
			if(seccond == l.seccond && first == l.first){
		return false;
			}else{
		return true;
	}
			if(first < l.first){
		return true;}
			if(seccond < l.seccond){
		return true;
			}else{
		return false;
	}
}
bool Loanc::operator>=(const Loanc & l)const{
			if(seccond == l.seccond && first == l.first){
		return false;
			}else{
		return true;
	}
			if(first > l.first){
		return true;}
			if(seccond > l.seccond){
		return true;
			}else{
		return false;
	}
}
bool Loanc::operator!=(const Loanc & l)const{
			if(seccond == l.seccond && first == l.first){
		return false;
			}else{
		return true;
	}
}
bool Loanc::operator==(const Loanc & l)const{
			if(seccond == l.seccond && first == l.first){
		return true;
			}else{
		return false;
	}
}
ostream & operator <<(ostream & os, const Loanc & l){
os << l.first << l.seccond << " $" << l.balence;
return os;
}

Loanclass.cpp

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include "Loanc.h"
#include <fstream>

using namespace std;

int* readData(string fname, int & arrSz);
void printArray(int arr[], int sz);


int main(){

cout << "Test to Create and display Loans: " << endl;
Loanc l1;
Loanc l2('H', 'A', 700);
Loanc l3('H', 'X', 400);
cout << "Add $500 to AA, returns " << l1.changeBalance(500) << ", " << l1 << endl;
cout << "Subtract $500 from HA, returns " << l2.changeBalance(-500) << ", " << l2 << endl;
cout << "Subtract $600 from HX, returns " << l3.changeBalance(-600) << ", " << l3 << endl;
cout << "END LOAN TEST." << endl;


int resultSize = 0;
int fileSize = 0;
	
string fname = "cmpt128a4data1.txt";

	//Read test file and print it
	int* fileArr = readData(fname, fileSize);
	if (fileArr != NULL){
		printArray(fileArr, fileSize);
		//cout << "resultSize initially set to " << resultSize << endl;
		//int arr[] = {1,2,2,2,4,5,7,7,7};
		
	}else{
		cout << "File not read proporly";
	}

cout << endl;
	
	// Free up dynamic memory
		delete[] fileArr;
	system("pause");
return 0;
}

int* readData(string fname, int & arrSz){
	
	Loanc* result = NULL;
	arrSz = 0;
	char f;
	char s;
	double bal;
	
	// Create a file object and open the file
	ifstream inStream;
	inStream.open(fname.c_str());
	
	// Only process file if opening it is successful
	if(!inStream.fail()){
	inStream >> arrSz;
	result = new Loanc[arrSz];

		// Read file contents into result, now that size is known
		 //create results array
		for(int i = 0; i < arrSz ; i++){
			inStream >> f >> s >> bal;
			result[i] = Loanc(f,s,bal); 
		}
			inStream.close(); //don't forget to close file
	}
	return result;
}
void printArray(int arr[], int sz){
	cout << "{";
	for (int i = 0; i < sz; i++){
		cout << arr[i];
		if (i < sz - 1){
			cout << ",";
		}
	}
	cout << "}";
}

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.

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.

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.

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.

//#include "stdafx.h"    //do you really need this?
#include <iostream>
#include <cmath>
#include "Loanc.h"
#include <fstream>

using namespace std;

Loanc* readData(string fname, int & arrSz);  //FIXED
void printArray(Loanc arr[], int sz);        //FIXED


int main(){

cout << "Test to Create and display Loans: " << endl;
Loanc l1;
Loanc l2('H', 'A', 700);
Loanc l3('H', 'X', 400);
cout << "Add $500 to AA, returns " << l1.changeBalance(500) << ", " << l1 << endl;
cout << "Subtract $500 from HA, returns " << l2.changeBalance(-500) << ", " << l2 << endl;
cout << "Subtract $600 from HX, returns " << l3.changeBalance(-600) << ", " << l3 << endl;
cout << "END LOAN TEST." << endl;


int resultSize = 0;
int fileSize = 0;
	
string fname = "cmpt128a4data1.txt";

	//Read test file and print it
	Loanc* fileArr = readData(fname, fileSize);    //FIXED
	if (fileArr != NULL){
		printArray(fileArr, fileSize);
		//cout << "resultSize initially set to " << resultSize << endl;
		//int arr[] = {1,2,2,2,4,5,7,7,7};
		
	}else{
		cout << "File not read proporly";
	}

cout << endl;
	
	// Free up dynamic memory
		delete[] fileArr;
	system("pause");
return 0;
}

Loanc* readData(string fname, int & arrSz){     //FIXED
	
	Loanc* result = NULL;
	arrSz = 0;
	char f;
	char s;
	double bal;
	
	// Create a file object and open the file
	ifstream inStream;
	inStream.open(fname.c_str());
	
	// Only process file if opening it is successful
	if(!inStream.fail()){
	inStream >> arrSz;
	result = new Loanc[arrSz];

		// Read file contents into result, now that size is known
		 //create results array
		for(int i = 0; i < arrSz ; i++){
			inStream >> f >> s >> bal;
			result[i] = Loanc(f,s,bal); 
		}
			inStream.close(); //don't forget to close file
	}
	return result;
}
void printArray(Loanc arr[], int sz){     //FIXED
	//cout << "{";
	//for (int i = 0; i < sz; i++){
	//	cout << arr[i];
	//	if (i < sz - 1){            //you gotta fix the body
	//		cout << ",";
	//	}
	//}
	//cout << "}";
}

-.-.... 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.