Displaying Data from a File

This is a discussion on Displaying Data from a File within the C++ Programming forums, part of the General Programming Boards category; Hello, I have written a program were the user can enter information about a person. This information is saved into ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    52

    Displaying Data from a File

    Hello,

    I have written a program were the user can enter information about a person. This information is saved into a file.

    My only problem is displaying the data in the file. The ouput is all over the place and I can't figure out how to solve this. Any ideas/suggestions will be appreciated.

    thanks

    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    
    #define cMax 100
    
    using namespace std;
    
    void crtNewClnt(char cName[], char cNum[], char cAddrs[], char cCity[], char cZip[]);
    void dispClnts(char cName[], char cNum[], char cAddrs[], char cCity[], char cZip[]);
    void clntMenu();
    
    
    /***********************************************************************************
    **    This function will be called by user selection in the main.cpp file.         *
    **    The main menu for Client Management will be displayed.                       *
    ***********************************************************************************/
    
    int main()
    {
        int mSelec;
        bool bDone = false;
    
    	do{
    
    cout << "---------------------------" << endl;
    cout << "---------------------------" << endl;
    cout << "\n1. Client Management" << endl;
    cout << "\nPlease enter your selection" << endl;
    cin >> mSelec;
    while (mSelec < 1  || mSelec > 5)
    
    {
            cout << "Please enter a correct selection between 1-5" << endl;
            cin >> mSelec;
    }
    
    switch(mSelec)
    {
        case 1: clntMenu();
        break;
    
        case 5: cout << "You are now closing the program!" << endl;
    bDone = true;
    
       }
    }while (!bDone);
    return 0;
    
    }
    
    void clntMenu()
    
     {
        char cName[cMax];
        char cNum[cMax];
        char cAddrs[cMax];
        char cCity[cMax];
        char cZip[cMax];
    
        int iSelec = 0;
    	system("cls");
    
    cout << "*---------------------------*" << endl;
    cout << setw(20) << " The DriftSpot" << endl;
    cout << setw(22) << "   Client Management" << endl;
    cout << "*---------------------------*" << endl;
    cout << "\n1. Create/add a new customer" << endl;
    cout << "2. Display all customers" << endl;
    cout << "\nPlease enter your selection [1-2]" << endl;
    cin >> iSelec;
    while (iSelec < 1  || iSelec > 6)
    {
            cout << "Please enter a correct selection. [1-6]" << endl;
            cin >> iSelec;
    }
    
    switch(iSelec)
    {
        case 1:
        	crtNewClnt(cName,cNum,cAddrs,cCity,cZip);
        break;
    
    
        case 2:
    	//	This calls out the function to display all items in inventory.
    		dispClnts(cName,cNum,cAddrs,cCity,cZip);
    	break;
    
    }
      system("cls");
    
     }
    
    void crtNewClnt(char cName[], char cNum[], char cAddrs[], char cCity[], char cZip[])
    
    {
        int xOut;
    	system("cls");
    	ofstream outputFile;
    
    	outputFile.open("clntInfo.txt", ios::app);
    
    				cout << "Please enter the name of the client:__";
                        cin.ignore();
                        cin.getline(cName, cMax);
    
    				cout << "Please enter the phone number of the client:__";
                        cin.getline(cNum, cMax);
    
                    cout << "Please enter the address of the client:__";
    
                        cin.getline(cAddrs, cMax);
    
                    cout << "Please enter the city:__";
    
                        cin.getline(cCity, cMax);
    
                    cout << "Please enter the zip code:__";
    
                        cin.getline(cZip, cMax);
    
            outputFile << cName << setw(20) << cNum << setw(20) << cAddrs << setw(20) << cCity << setw(20) << cZip << endl;
    		outputFile.close();
    
                        cout << "\n************************************************";
    				cout << "\nYou have just added a new client to the data file!";
                        cout << "\n************************************************" << endl << endl;
                        cout << "Do you want to exit to the main menu now? 1 = [YES] , 0 = [NO]" << endl;
    		cin >> xOut;
    while (xOut < 0  || xOut > 1)
    {
            cout << "Please enter a value between; 1 = [YES] , 0 = [NO]" << endl;
            cin >> xOut;
    		}
    }
    
    void dispClnts(char cName[], char cNum[], char cAddrs[], char cCity[], char cZip[])
    
    {
        int xOut;
    
    	system("cls");
        ifstream inputFile;
    	inputFile.open("clntInfo.txt");
                   if( !inputFile ) {
                   cout << "\n************************************************\n";
                   cout << "ERROR5: Could not open or locate InvItems.txt\n";
                   cout <<	"	Make sure that the file exists.\n";
                   cout << "************************************************\n";
            }
    
    		while(!inputFile.eof())
    		{
    
    	inputFile >> cName >> cNum >> cAddrs >> cCity >> cZip;
    
    	cout << cName << setw(20) << cNum << setw(20) << cAddrs << setw(20) << cCity << setw(20) << cZip << endl;
    		}
    system("pause");
    		inputFile.close();
    		cout << "\nDo you want to exit to the main menu now? 1 = [YES] , 0 = [NO]" << endl;
    cin >> xOut;
    while (xOut < 0  || xOut > 1)
    {
            cout << "Please enter a value between; 1 = [YES] , 0 = [NO]" << endl;
            cin >> xOut;
    		}
    
    system("cls");
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    Uhm, right away I would suggest save the data in a comma delimited format, and develop a constant standard. Something like... Name,Sex,Age

    Bob,Male,NULL
    Jen,Female,26
    Zack,Male,24
    Jes,Female,29

    Then you read in the file one line at a time, split the string up by the commas, and cout it one property at a time per individual.

    I'm guessing you just didn't really think about the formatting much so you're getting unpredictable results.

    Plan everything out carefully, go through the process step by step as you develop it.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    for your output you might use something like

    Code:
    inputFile >> cName >> cNum >> cAddrs >> cCity >> cZip;
    
    cout << "Customer " << customerNumber << endl;
    cout << "----------" << endl;
    cout << "Customer Name: " << cName << endl;
    cout << "Customer Number: " << cNum << endl;
    cout << "Customer Address: " << cAddrs << endl;
    cout << "Customer City: " << cCity << endl;
    cout << "Customer Zip: " << cZip << endl;
    cout << endl;
    This will produce a clear output...As stated above you need to look at your input and store it in the file.

    Additionally read up on using .eof() for while loops. If you run your file with one entry it will print it out twice???

    Also right here

    Code:
    void dispClnts(char cName[], char cNum[], char cAddrs[], char cCity[], char cZip[])
    
    {
        int xOut;
        int customerNumber = 1;
    
    	system("cls");
        ifstream inputFile;
    	inputFile.open("clntInfo.txt");
                   if( !inputFile ) {
                   cout << "\n************************************************\n";
                   cout << "ERROR5: Could not open or locate InvItems.txt\n";
                   cout <<	"	Make sure that the file exists.\n";
                   cout << "************************************************\n";
                   
                   exit(0); // USE THIS
            }
    the exit(0); command which comes with stdlib.h will tell the program to close if the file can not open. Without it the program will go into an infinite loop taking in information.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    Most problems are if there is a space entered.
    For example if I enter the customer name a John Doe the Doe is passed to the next outputline and it all get's massed up. This is not a big deal with the name, but the issue is with the address

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanning and Displaying Data File
    By MetaMorphicX in forum C Programming
    Replies: 4
    Last Post: 01-09-2009, 02:38 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21