I'm trying to compile my program using library functions I created. I can't figure out how to get it to work. I'm going to attach my files so if someone could help me that would be great
trunks1212 0 Newbie Poster
/*
The program will read the valid credit card numbers and available credit
and store them in parallel 1-dimensional arrays. The data should then be sorted,
using a reasonably efficient bubble sort or selection sort. For each credit card
purchase (from the second input file) to be validated, the program should
perform a binary search to determine whether or not the credit card number is
valid, and, if so, whether or not the account has sufficient credit available
for the purchase. If the purchase is valid, the available credit must be
adjusted to account for the new purchase.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SIZE = 100;
#include "proglib.h"
int main()
{
ifstream fin, //input file 1
fin2; //input file 2
ofstream fout; //output file
int numbers[SIZE] = {0}; //array of credit card numbers
double balance[SIZE] = {0}; //array of account balances
string message; //attempted purchase message
int cardNumber, //card number of purchase
count, //count of numbers in array
mid; //variable to find key in array
double purchase; //amount of purchase
bool found; //card number found or not found
fin.open("prog03inp1.txt");
fin2.open("prog03inp2.txt");
fout.open("prog03out.txt");
fout << "Credit Cards" << endl << endl
<< setw(10) << "Card Number" << setw(18) << "Purchase Amount"
<< setw(30) << "Transaction Approval" << endl << endl;
count = get_accounts(fin, numbers, balance);
sort_accounts(numbers, balance, count);
while(fin2 >> cardNumber >> purchase)
{
search_array(numbers, count, cardNumber, found, mid);
message = get_transaction_approval(found, balance, mid, purchase);
print_output(fout, cardNumber, purchase, message);
}
fin.close();
fin2.close();
return 0;
}
void print_output(ofstream& fout, int cardNumber, double purchase, string message)
{
fout << setw(10) << cardNumber << setprecision(2) << fixed
<< setw(10) << "$"
<< setw(9) << purchase << setw(30) << message << endl;
}
/*
File: proglib.cpp
*/
#include "proglib.h"
// Function: get_accounts
// constant: none
// receives:
// returns:
int get_accounts(ifstream& fin, int numbers[], double balance[])
{
int count = 0;
while(fin >> numbers[count] >> balance[count])
{
count++;
}
return count;
}
// Function: sort_accounts
// constant: none
// receives:
// returns:
void sort_accounts(int numbers[], double balance[], int count)
{
int pass = 1;
bool done = false;
while(!done)
{
done = true;
for (int i = 0; i < count - pass; i++)
{
if (numbers[i] > numbers[i+1])
{
swap(numbers[i], numbers[i+1]);
swap(balance[i], balance[i+1]);
done = false;
}
}
pass++;
}
}
// Function: search_array
// constant: none
// receives:
// returns:
void search_array(const int numbers[], int count, int key, bool& found, int& mid)
{
int low = 0;
int high = count - 1;
found = false;
while(!found && low <= high )
{
mid = (low + high) / 2;
if (numbers[mid] == key)
{
found = true;
}
else if(numbers[mid] > key)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
}
// Function: get_transaction_approval
// constant: none
// receives:
// returns:
string get_transaction_approval(bool found, double balance[], int mid,
double purchase)
{
string message;
if(found)
{
if(balance[mid] >= purchase)
{
balance[mid] = balance[mid] - purchase;
message = "Successful Transaction";
}
else
{
message = "Insufficient Credit";
}
}
else
{
message = "Invalid Credit Card";
}
return message;
}
/*
File: proglib.h
*/
int get_accounts(ifstream&, int[], double[]);
void sort_accounts(int[], double[], int);
void search_array(const int[], int, int, bool&, int&);
string get_transaction_approval(bool, double[], int, double);
void print_output(ofstream&, int, double, string);
Lazaro Claiborn 1 Junior Poster
I'm trying to compile my program using library functions I created. I can't figure out how to get it to work. I'm going to attach my files so if someone could help me that would be great
First thing I'd suggest is to use condition prepressor directives to prevent header inclusion. In your header file "proglib.h" enclose your code like this:
#ifndef PROGLIB_H
#define PROGLIB_H
....function prototypes and/or constants and/or etc...
#endif
Next make sure your header file and its corresponding .cpp file are in the same directory, unless you've got your compiler configured to search certainly and not by default. Actually, what compiler you using? Is it command line or IDE GUI? Let me know.
Good luck, LamaBot
Clinton Portis 211 Practically a Posting Shark
what compiler are ye' using?
for most IDE's like Dev CPP and MSVC++ 6.0, there is an option to, "add to project".. make sure the you are adding all 3 files to your project, then try to compile.
If you are using a command line compiler like borland, you will have to learn the codes for linking files together.. which I used to remember but I don't anymore.
trunks1212 0 Newbie Poster
I'm using Dev CPP
John A 1,896 Vampirical Lurker Team Colleague
>I can't figure out how to get it to work.
So then tell us what doesn't work, so we don't have to guess.
trunks1212 0 Newbie Poster
Ok sorry I figured it out. I had to put #include <fstream> and using namespace std; in my header file and now it works fine. And I did what you said about putting all the files in a project. So sorry again and thank you for all your help.
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.