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;
     }
}

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.

It's not strange at all.

The customer file contains the data. The program contains only code. You need to read the file to populate the program data before you can display the information.

It's not strange at all.

The customer file contains the data. The program contains only code. You need to read the file to populate the program data before you can display the information.

but, how would i do that? can you tell me please, which changes to the code should i make?

but, how would i do that? can you tell me please, which changes to the code should i make?

No, but I can help you change it.

You obviously know how to write a file. Look up how to read a file. That's your first step.

Second step is figuring out where in the code you need to do the read. That I leave to you the think about. Come up with an educated guess and ask again.

No, but I can help you change it.

You obviously know how to write a file. Look up how to read a file. That's your first step.

Second step is figuring out where in the code you need to do the read. That I leave to you the think about. Come up with an educated guess and ask again.

well I was looking how to read a file, I need
ifstream inFile;
inFile.open(customer.txt);
I have this piece of code in here, I think:

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();

but what should i change?

but what should i change?

I don't know. It's your code. I guess I'd change:
1) anything that doesn't work
2) all that gotoxy() crap
3) your compiler. Update to a new compiler, not one that's been dead since the mid 1980's. But I assume it's your lame instructor making you learn stuff that's 25 years old rather than giving you proper tools.

If you ask an answerable question rather than a vague idea of a question, you can really get good help. But since we aren't writing your code, we can't really answer vague "what do I do now?" questions easily.

I don't know. It's your code. I guess I'd change:
1) anything that doesn't work
2) all that gotoxy() crap
3) your compiler. Update to a new compiler, not one that's been dead since the mid 1980's. But I assume it's your lame instructor making you learn stuff that's 25 years old rather than giving you proper tools.

If you ask an answerable question rather than a vague idea of a question, you can really get good help. But since we aren't writing your code, we can't really answer vague "what do I do now?" questions easily.

I am trying to find what is causing the problem,but...unfortunately can't find it, well I have same piece of code, but with more info in it, it works fine...saves, displays everything..i am missing small piece of code in my program, and have to find what is it.
my compiler is fine, but when you have mentioned my instructor you were 100% correct)))))

I am trying to find what is causing the problem,but...unfortunately can't find it, well I have same piece of code, but with more info in it, it works fine...saves, displays everything..i am missing small piece of code in my program, and have to find what is it.

Well, In all the posts you've made here, not once did you acually ask a really good question. The only problem you mentioned was there is no data in your program when you start it running. I answered that one.

As for any other problems, start asking real questions about them.

my compiler is fine,

Sure it is. And when someone goes to Iraq, a Medieval Suit of Armor with a sword is fine, too. You learn 1980's technology, you can't get a job in the 2010's because you don't have the knowledge of the past 25 years. That's fine too, I guess.

Well, In all the posts you've made here, not once did you acually ask a really good question. The only problem you mentioned was there is no data in your program when you start it running. I answered that one.

As for any other problems, start asking real questions about them.

well yes, I haven't asked really good questions....but like i've mentioned in one of my posts, i am new to c++ programming, and unfortunately dont really know all aspects of it. so, if you can help me my current problem, please help, because i am trying to find where the problem is, but nothing good yet(( that is actually how people learn, on examples. thanks

Edited 6 Years Ago by dinamit875: n/a

if you are trying to have you data shown on console in your write to file method also have it printed out to console using cout.

now, the previous code that i was doing, it was working well, but unfortunately requirement have been changed, and here is a new code, it is almost working, but can anyone tell me what is wrong with number 2 menu functrion, when i am typing in 2 to see current customers, from the start, the program is asking to enter customer details, seems to be it calls first function in the menu.

#include <iostream>
#include <iomanip>              //used for setw precision
#include <fstream>
#include <cstring>
using namespace std;

#define MAXNAME 100
#define FILENAME "customer.txt"

/************ Structs *************/
struct CUSTREC
{
       char name[MAXNAME];
       char surname[MAXNAME];
       char address[MAXNAME];
       int age;
       int ID;// *************NB has to be the same as Flight ID**************
       //float adultPrice;
       //float childPrice;
};
typedef struct CUSTREC C;

/********* Functions Declaration **********/
int menu();
void getCustomer (fstream & file, int numCustomers, int& countCustomers, int& countAdults, int& countChild);
//void getTotal (C cArray[], int& countAdults, int& countChild, int numAdults, int numChild, float adultPrice, float childPrice, string cFile);
void printCustomers (fstream & file, int& countCustomers, int& countAdults, int& countChild);
/********** Functions Declaration End **********/

int menu()
{
     int response;
     do
     {
          system ("CLS");
	 	  cout << "\n\n\n\n\n\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;
}

int main(void)
{    
    system ("CLS");
	system ("COLOR F9"); 
    
    C rec;
         
    int response = 0;
    int countCustomers = 0; 
    int countAdults = 0;
    int countChild = 0;
    
    fstream file;   
    int numCustomers;
    
    int numAdults;
    int numChild;
    
    //float adultPrice = 125.99;
    //float childPrice = 94.49;
    
    file.open(FILENAME, ios::out | ios::in|  ios:: binary);
    if(file == NULL) 
    {
	cout << "Error: cannot open file for modifying\n\n";
	//return 1;
    }
    //cout << "Enter the number of customers: "<<endl;
    //cin >> numCustomers;
    cout << "\n\n\n\n\n\n\n\n\n" << endl;
    cout <<setw(48) << " *** WELCOME *** " << endl;
    
    cin.ignore();
    menu();
    numCustomers = 1;
    
    getCustomer(file, numCustomers, countCustomers, countAdults, countChild);
    printCustomers(file, countCustomers, countAdults, countChild);
    //file.close();
    
    do
    {
        response = menu(); // call of menu function
        
        if (response == 1)
         {
             getCustomer (file, numCustomers, countCustomers, countAdults, countChild);
             system ("CLS");
         }
        else if (response == 2)
         {             
             printCustomers(file, countCustomers, countAdults, countChild);
             cout << endl << setw(15) << ""; 
         }
        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");
       
    file.seekg (0,ios:: end);    
    file.write(reinterpret_cast< char *>(&rec), sizeof(C)); 
       
    //write a number of records to a sequential file
    getCustomer (file, numCustomers, countCustomers, countAdults, countChild);
    //read records from a sequential file  
    printCustomers (file, countCustomers, countAdults, countChild);
    file.close();
    return 0;
}
/************ Functions *************/

/************ Enters new Customer *************/
void getCustomer (fstream & file, int numCustomers, int& countCustomers, int& countAdults, int& countChild)
{
     C rec;
     char answer;
     do
     {
         system ("CLS");
	     cout << "\n\n\n";
         cout << setw(35) <<""<< "Customer Details:" << endl;
         file.seekg (0,ios::end);
         
         for (int i = 0; i < numCustomers; i++)
         {  
            cout << setw(25) <<""<< "Enter Customer "<<"*"<<i+1<<"*"<<" Name: ";
            //cin.getline(rec.name, MAXNAME-1);
            cin >> rec.name;
            cout << setw(25) <<""<< "Enter Customer ID: ";
	        cin >> rec.ID;
            cout << setw(25) <<""<< "Enter Customer Surname: ";
            cin>>  rec.surname;
            cout << setw(25) <<""<< "Enter Customer Age: ";
	        cin >> rec.age;
	       	     
	        if (rec.age >= 1 && rec.age <= 17)
	     
                 countChild ++;
         
            else if (rec.age >= 18 && rec.age <= 100)
         
                 countAdults ++;
         
            else;  
            
      	    cout << setw(25) <<""<< "Enter Customer Address: ";
	        //cin.getline(rec.address, MAXNAME-1);
            cin >> rec.address;
            cout <<"\n"<< endl;
            
            countCustomers ++;
            cin.ignore();
            
            file.write(reinterpret_cast< char *>(&rec), sizeof(C)); 
          }
            cout << setw(25) <<""<< "Enter new Customer? (Y/N): ";  
            cin >> answer;
            countCustomers ++;
            answer = toupper(answer);
      }// end do statement
      while (answer != 'N');
}// end void statement

/*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(fstream & file, int& countCustomers, int& countAdults, int& countChild)
{
     C rec;
     file.seekg (0,ios:: beg);      //  - goto beginning of file
     file.read( reinterpret_cast< char *>(&rec ), sizeof(CUSTREC));

     /*if(file == NULL)
     {
     cout << "Empty List\n\n" << endl;
     }*/
     
     system ("CLS");
     cout << "\n\n\n";
     cout << setw(31) <<""<< "All Customer Details:" << endl << endl;
     
       while(!file.eof())
       {
         cout << setw(25) <<""<< "Customer Name: " << rec.name << endl;
         cout << setw(25) <<""<< "Customer ID: " << rec.ID << endl;
         cout << setw(25) <<""<< "Customer Surname: " << rec.surname << endl;
         cout << setw(25) <<""<< "Customer Age: " << rec.age << endl;
         cout << setw(25) <<""<< "Customer Address: " << rec.address << endl;
         cout << endl << endl;
                  
         file.read( reinterpret_cast< char *> (&rec),sizeof(C));
         
       }
         //cout << setw(25) <<""<< "Customer Name: " << rec.name << endl;
         cout << endl;
         cout << setw(25) <<""<< "Number of Customers: " << countCustomers << endl; 
         cout << setw(25) <<""<< "Number of Adults: " << countAdults << endl;
         cout << setw(25) <<""<< "Number of Child: " << countChild << endl;
         system("pause");
}
This article has been dead for over six months. Start a new discussion instead.