The program should:

Accept a series of names and addresses from the console.
The user's input should be written to a text file in the CSV format described in the lecture. But, do not include the field names in the first row of the file.
Read the records from the text file and display them in a user friendly format.
Provide a menu to allow the user to either append records to the file, display the records or exit the application.

This is what I have. It compiles just fine. I can write to the file but I cannot read the file. Can someone tell me where I went wrong with the code?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);

const char FileName[] = "c://TestAddress/TestAddress.txt";
ifstream myAddress(FileName);

    string name = " ";
    string address = " ";
    string street= " ";
    string city = " ";
    string state = " ";
    string zipCode = " ";
    int record = 0;
    ofstream outMyAddress(FileName, ios::out);

int main ()
{
       
    menu();
    return 0;
} //end main

void menu(void)
{
    //allow user to choose to append records, display records or exit the program
    char userChoice = ' ';

    //Display Menu
    system("cls");
    cout << "\nName and Address database:" << endl;  
    cout << endl;
    cout << "\n\n(A)ppend record, (S)how Record, (E)xit:";
        cin >> userChoice;
        //Users Choice

    switch (userChoice)
    {
        case 'a':
        case 'A'://Append Record
            myAddress.open (FileName, ios::app);
            if (myAddress.is_open())
			{
				writeData();
			}
        break;

        case 's':
        case 'S'://Show record
            myAddress.open (FileName, ios:: in);
            if (myAddress.is_open())
			{
				readData();
			}
           
        break;

        case 'e':
        case 'E'://Exit
            myAddress.close();
        break;

        default:
            cout << "Invalid choice" << endl;
            cout << endl << endl << endl;
            break;
           
    }

    return;
   
}//end menu


void writeData(void) //Write the Address Info to a file
{    
   
    char answer = ' ';
    char response = ' ';

	if(myAddress.is_open())
	{
		//entering loop
    while (answer != 'n' || answer != 'N')
    {
        cout << endl;
        getline(cin, name);
        cout << "\nEnter name: ";
        getline(cin, name);
        cout << "\nEnter Street: ";
        getline(cin, street);
        cout << "\nEnter City: ";
        getline(cin, city);
        cout << "\nEnter State: ";
        getline(cin, state);
        cout << "\nEnter Zip Code: ";
        getline(cin, zipCode);
        cout << endl;

        outMyAddress << name << ", " << street << " ," << city << ", " << state << " ," << zipCode << endl;
        record++;
        
        cout << "\nWould you like to enter another record? (Y/N)" << endl;
        cin >> response;
   
       
     if (response == 'n' || response == 'N')
        {
        return menu();
        }
    }
	}

    myAddress.close();

       
}//end write data

void readData(void)
{      
    ifstream inMyAddress(FileName, ios::in);

	if(myAddress.is_open())
	{
    string firstLine;
    inMyAddress >> firstLine;
    getline (myAddress, firstLine, '\n'); 

                cout << endl;
                cout << "Reading the file(s)..." << endl;
                cout << endl;

                //read data from a file                                
                //use the split function to break a deliminated line of text into fields    

                cout << "Record #: " << record << endl;
                string *theField = split(firstLine, ',');
                cout << "Name......" << theField[0] << endl;
                cout << "Street......" << theField[1] << endl;
                cout << "City......" << theField[2] << endl;
                cout << "State......" << theField[3] << endl;
                cout << "Zip Code......" << theField[4] << endl;
	}
                inMyAddress.close();
                system("pause");
                return menu();
           
}//end read data

string * split(string theLine, char theDeliminator){
    //Break theline into fields and save the fields to an array.
    //Each field will occupy one element in a character array.
    //theLine is a string with fields separated with theDeliminator character.
    //Assumes the last field in the string is terminated with a newline.
    //Useage: string *theFields = split(lineBuffer, ',');

    //determine how many splits there will be so we can size our array
    int splitCount = 0;
    for(int i = 0; i < ((int)theLine.size()); i++){
    if (theLine[i] == theDeliminator)
    splitCount++;
    return 0;
}

splitCount++; //add one more to the count because there is not an ending comma
    //create an array to hold the fields
    string* theFieldArray;
    theFieldArray = new string[splitCount];
    //split the string into seperate fields
    string theField = "";
    int commaCount = 0;

    for(int i = 0; i < ((int)theLine.size()); i++){ //read each character and look for the deliminator
    if (theLine[i] != theDeliminator) {
    theField += theLine[i]; //build the field
    }
    else { //the deliminator was hit so save to the field to the array
    theFieldArray[commaCount] = theField; //save the field to the array
    theField = "";
    commaCount++;
    }
    }
    theFieldArray[commaCount] = theField; //the last field is not marked with a comma...

    return theFieldArray;
} //end split

It tries to open a debugger when I try to read the file.

Recommended Answers

All 12 Replies

delete lines 13 and 22 because they are unnecessary global variables.

line 133: why is it reading just the first word on the first line of the file?

Here is how to read that file

// read until end-of-file
while( getline(name, inMyAddress, ',') )
{
        getline( street, inMyAddress, ',' );
        getline( city, inMyAddress, ',');
        getline( state, inMyAddress, ',');
        getline( zip, inMyAddress, ',');
      // now put this stuff in an array or vector
}

STAFF PERSONNEL SYSTEM
required to develop a Win32 console application to enter, search, edit and view personnel information of the staff in the company based on different access priority. This application will be used by the Human Resource personnel, staff and the administrator of this application. The main purpose of this application is to store and edit the complete personal record of each staff in the company


One of the most important modules of the staff personnel system is the LOGIN module. Each user of the system has its own user name and password. The administrator of the application has the highest access priority which enables him/her to perform all the functionalities that exist in the application. In addition to that, the administrator is able to register users from the human resource department. The staffs of the company have the lowest access priority that only enables them to view their personal information.
The human resource personnel are allowed to:
Register staff as users of the system
Add, search and edit personal information of staff in the company
Delete record of any staff in the company
The following details of each staff are recorded:
Staff Number
Name
Identification Card (IC) Number
Gender
Designation
Department
Date Joined
Nationality
Religion
Date Of Birth
Marital Status

Each staff has a unique staff number and this will be automatically generated by the system.
Classification of designation for staff is categorised as follows:
Managing Director
Senior Manager
Manager
Senior Executive
Executive
Junior Executive
The category of designation and the related department are determined by you in the system.
The search of any record in the system can be done by using the following categories:
A Staff Number or range of Staff Number
Name
IC Number
Classification
And other categories
If a list of record is displayed after the search, the system should allow fine searching in order to allow only one record to be displayed as the final selection. You should design the system so that the user is able to edit the selective category in the search record. After the edit process is done, the user is prompted for saving purpose.
You should determine the selection in the main menu for each login user. After each final result is displayed, the system should prompt the user whether he/she would like to continue or not.

please help me in my assignment

i have to use oo concepts of c++ in assignment .please help me out frnds

shashanderson - Are you asking for the project to be developed for you in someone elses thread that have actually needs help and not someone who wants their work to be done for them? :) Atleast make your own thread to ask

no frnd it is not like that ..i have tried but do not succeed please help me

frnd it is my assignment ..i have to submit it ...i try to make login page in visual 2008 it is giving error on sting header

1. Make your own thread
2. Post what you have done
:)
Or p/m me and I'll try and help you.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // for exit(1);

using namespace std;


void Login();

int main()
{
    Login();
    return 0;
}

void Login()
{
    char login_un[50], login_pw[50], username[50], password[50];
    int c;
    ifstream uin("user.txt");
    ifstream pin("pass.txt");

    cout<<"Main\n\n"
        <<"(1) Login\n"
        <<"(2) Quit\n";
    cin>> c;



    if (c==1)
    {
        uin.getline(username, 50);
        while (strcmp(login_un, username) !=0)
        {
            cout<<"Username: ";
            cin.getline(login_un, 50);
            if (strcmp(login_un, username) ==0) break;
            else
                cout<<"\nInvalid Username.\n";
        }

        pin.getline(password, 50);
        while (strcmp(login_pw, password) !=0)
        {
            cout<<"\nPassword: ";
            cin.getline(login_pw, 50);
            if (strcmp(login_pw, password) ==0) break;
            else
                cout<<"\nInvalid Password\n";
        }

    }

    else if (c==2)
    {
        cout<<"Quitting\n";
        exit(1);
    }
    return;
}

it is giving error in #include<string>
second it is not compilling

in visual studio 2008

when i put #include<iostream.h>

then some errors are removed ...very confused

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.