Hi everyone, I have written a program here about customers, it works fine so far, but I am having a problem with getting customer details, for example, when I enter customer name, age...etc, it saves this info into a customer.txt file, I can see this information until I exit the program. But when I exit it and run same program again, I would like to see this info (option 2 in the menu), it shows nothing((( strange!!!! because the customer.txt file is not empty, and has info in it. Could anyone help me with this, please...Thanks in advance.
heres the code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <fstream>
#include "cursor.h"
using namespace std;
/************ Structs *************/
struct Customers
{
int ID;// *************NB has to be the same as Flight ID**************
string name, surname, address;//, adult, child;
int age;
float adultPrice;
float childPrice;
};
typedef struct Customers C;
/********* Functions Declaration **********/
int menu();
void loadFromFile(C cArray[], int& countCustomers, int& countAdults, int& countChild, int numCustomers, int numAdults, int numChild, string cFile);
void getCustomer (C cArray[], int& countCustomers, int& countAdults, int& countChild, string cFile);
void getTotal (C cArray[], int& countAdults, int& countChild, int numAdults, int numChild, float adultPrice, float childPrice, string cFile);
void printCustomers(C cArray[], int& countCustomers, int& countAdults, int& countChild);
void saveToFile(C cArray[], int& countCustomers, int& countAdults, int& countChild, string cFile);
/********** Functions End **********/
int main ()
{
system ("CLS");
system ("COLOR F1");
string cFile = "customer.txt";
int response = 0;
int countCustomers = 0;
int countAdults = 0;
int countChild = 0;
int numCustomers;
int numAdults;
int numChild;
float adultPrice = 125.99;
float childPrice = 94.49;
cout << endl << setw(15) << "" << "Enter number of Customers: ";
cin >> numCustomers;
C * cArray = new C[numCustomers];
loadFromFile(cArray, countCustomers, countAdults, countChild, numCustomers, numAdults, numChild, cFile);
do
{
response = menu(); // call of menu function
if (response == 1)
{
getCustomer (cArray, countCustomers, countAdults, countChild, cFile);
system ("CLS");
}
else if (response == 2)
{
printCustomers(cArray, countCustomers, countAdults, countChild);
cout << endl << setw(15) << ""; system("pause");
}
else if (response == 3)
{
getTotal (cArray, countAdults, countChild, numAdults, numChild, adultPrice, childPrice, cFile);
cout << endl << setw(15) << ""; system("pause");
}
else if (response == 0)
{
return 0;
} /* exit point from program when user presses "0"*/
} while (response != 0);
system ("PAUSE");
return 0;
}
/************ Functions *************/
int menu()
{
int response;
do
{
system ("CLS");
cout << "\n\n\n";
cout << setw(35) <<"" << "MENU:" << endl;
cout << setw(37) << right << "1 : "; cout << "Enter Customer Details\n";
cout << setw(37) << right << "2 : "; cout << "Print all Customers\n";
cout << setw(37) << right << "3 : "; cout << "Show Price Breakdown\n";
cout << setw(37) << right << "0 : "; cout << "Exit\n";
cout << setw(37) << right << "Enter choice : ";
cin >> response;
} while ((response != 1) && (response != 2) && (response != 3) && (response != 0) );
return response;
}
/************ Load from files *************/
void loadFromFile(C cArray[], int& countCustomers, int& countAdults, int& countChild, int numCustomers, int numAdults, int numChild, string cFile)
{
/*Customer File*/
ifstream fileInCustomer;
fileInCustomer.open(cFile.c_str());
if (!fileInCustomer)
{
gotoxy(12,8); cout << "Customers text file, Customer.txt, cannot be used.";
gotoxy(12,9); cout << "If the file is empty continue, otherwise exit.";
gotoxy(12,11); system ("pause");
// message when file dosn't exist or there is a problem to open it
}
for (countCustomers = 0; ; countCustomers++)
{
fileInCustomer >> cArray[countCustomers].ID; fileInCustomer.ignore();
getline (fileInCustomer, cArray[countCustomers].name);
getline (fileInCustomer, cArray[countCustomers].surname);
getline (fileInCustomer, cArray[countCustomers].address);
fileInCustomer >> cArray[countCustomers].age; fileInCustomer.ignore();
if (fileInCustomer.fail()) break;
if (countCustomers == numCustomers - 1)
{
system ("CLS");
gotoxy(12,7); cout << "Error!";
gotoxy(12,8); cout << "Please exit the program.";
gotoxy(12,9); cout << "Please enter number of Patiants bigger than " << numCustomers << ".";
gotoxy(12,11); system ("pause");
break;
}
}
fileInCustomer.close();
}
/************ Enters new Customer *************/
void getCustomer (C cArray[], int& countCustomers, int& countAdults, int& countChild, string cFile)
{
char answer;
do
{
system ("CLS");
cout << "\n\n\n";
cout << setw(35) <<""<< "Customer Details:" << endl;
cout << setw(25) <<""<< "Enter Customer ID: ";
cin >> cArray[countCustomers].ID; cin.ignore();
cout << setw(25) <<""<< "Enter Customer Name: ";
getline(cin, cArray[countCustomers].name);
cout << setw(25) <<""<< "Enter Customer Surname: ";
getline(cin, cArray[countCustomers].surname);
cout << setw(25) <<""<< "Enter Customer Age: ";
cin >> cArray[countCustomers].age; cin.ignore();
if (cArray[countCustomers].age >= 1 && cArray[countCustomers].age <= 17)
countChild ++;
else if (cArray[countCustomers].age >= 18 && cArray[countCustomers].age <= 100)
countAdults ++;
else;
cout << setw(25) <<""<< "Enter Customer Address: ";
getline(cin, cArray[countCustomers].address);
cout << setw(25) <<""<< "Enter new Customer? (Y/N): ";
cin >> answer;
countCustomers ++;
answer = toupper(answer);
} while (answer != 'N');
/********Save to Patiant file******/
ofstream fileOut;
fileOut.open(cFile.c_str());
if (!fileOut)
{
cout << "Can not write to file...\n";
system ("pause");
// message when file is "Read Only" or program couldn't open it
}
for (int i=0; i < countCustomers; i++)
{
fileOut << cArray[i].ID << endl;
fileOut << cArray[i].name << endl;
fileOut << cArray[i].surname << endl;
fileOut << cArray[i].age << endl;
fileOut << cArray[i].address << endl;
}
}
void getTotal (C cArray[], int& countAdults, int& countChild, int numAdults, int numChild, float adultPrice, float childPrice, string cFile)
{
system("CLS");
cout << "\n\n\n";
cout << " Price For All Adults Will Be :" << numAdults*adultPrice << endl;
cout << " Price For All Children Will Be :" << numChild*childPrice << endl;
}
/************ Prints all Customers *************/
void printCustomers(C cArray[], int& countCustomers, int& countAdults, int& countChild)
{
system ("CLS");
cout << "\n\n\n";
cout << setw(31) <<""<< "All Customer Details:" << endl << endl;
for (int i=0; i < countCustomers; i++)
{
cout << setw(25) <<""<< "Customer ID: " << setw(15) << cArray[i].ID << endl;
cout << setw(25) <<""<< "Customer Name: " << setw(14) << cArray[i].name << endl;
cout << setw(25) <<""<< "Customer Surname: " << setw(12) << cArray[i].surname << endl;
cout << setw(25) <<""<< "Customer Age: " << setw(13) << cArray[i].age << endl;
cout << setw(25) <<""<< "Customer Address: " << setw(10) << cArray[i].address << endl << endl;
}
cout << setw(25) <<""<< "Number of Customers : " << setw(5) << countCustomers << endl;
cout << setw(25) <<""<< "Number of Adults: " << setw(9) << countAdults << endl;
cout << setw(25) <<""<< "Number of Child: " << setw(10) << countChild << endl;
}
void saveToFile (C cArray[], int& countCustomers, int& countAdults, int& countChild, string cFile)
{
/********Save to Customer file******/
ofstream fileOut;
fileOut.open(cFile.c_str());
if (!fileOut)
{
cout << "Can not write to file...\n";
system ("pause");
// message when file is "Read Only" or program couldn't open it
}
for (int i=0; i < countCustomers; i++)
{
fileOut << cArray[i].ID << endl;
fileOut << cArray[i].name << endl;
fileOut << cArray[i].surname << endl;
fileOut << cArray[i].age << endl;
fileOut << cArray[i].address << endl;
}
}