So below is a working program I wrote that mimics a rolodex. It stores the information on a text file with the number of cards listed at the top of the file and one card per line like this:


last first phone email
last first phone email
...

I'm currently storing the information on the text file, and that's become a problem. Per the assignment, I need to be storing the data on the text file and in an array of structs. I have arrays, and I have structs. They are not however, mingling. 4 of the 6 functions work fine, but I'm hesitant to mess with the last two if my data handling is a big problem.

Writing the data to file is reading in the data, but writing out like this:
last
first
phone
email
last
...
And I haven't started the delete card function because I can't visualize what my data is going to look like in an array of structs. Any help would be appreciated and let me know if there's anything else I should change. Thanks.

[LIST=1]
[*]#include<iostream>
[*]#include<string>
[*]#include<fstream>
[*]using namespace std;



[*]struct Card
[*]{
[*]	string firstName;
[*]	string lastName;
[*]    string phoneNumber;
[*]	string eMail;
[*]};



[*]void PrintMenu();                                //displays menu of options
[*]void cardCount();                                //reads number of cards from text file
[*]void readCard();                                 //reads card from file (option 1)
[*]void writeToFile();                              //writes to the file (option 2)
[*]void addCard();                                  //adds a new card to the file (option 3)
[*]void deleteCard();                               //deletes a card (option 4)
[*]void findCard();                                 //Finds card in file (option 5)
[*]void PrintToScreen();                            //Prints file to screen (option 6)


[*]int main()
[*]{

[*]Card card;
[*]int userChoice = 0;                              //initializing and declaring selection variable. 
[*]						                         
[*]PrintMenu();                                     

[*]while(userChoice < 7)                            
[*]{
[*]cin >> userChoice;
[*]{
[*]switch(userChoice)                      
[*]{
[*]		case 1:                           //Read in from the file    
[*]			readCard();
[*]			PrintMenu();
[*]	    break;
[*]		case 2:                           //Write to the file
[*]			writeToFile();
[*]			PrintMenu();
[*]			break;
[*]		case 3:                           //Add a new card
[*]			addCard();
[*]			PrintMenu();
[*]			break;
[*]		case 4:                           //Delete a card
[*]			deleteCard();
[*]			PrintMenu();
[*]			break;
[*]		case 5:                           //Search for a card
[*]			findCard();
[*]			PrintMenu();
[*]			break;
[*]		case 6:                           //Print rolodex to screen
[*]			PrintToScreen();              
[*]			PrintMenu();
[*]			break;
[*]		case 7:                           //Quit
[*]			break;
[*]}
[*]}
[*]		
[*]}
[*]return 0;
[*]}

[*]void PrintMenu()

[*]{
[*]cout << endl;
[*]cout << "What do you want to do? (enter number of choice)" << endl;
[*]cout << "1) Read in from the file" << endl;
[*]cout << "2) Write to the file" << endl;
[*]cout << "3) Add a new card" << endl;
[*]cout << "4) Delete a card" << endl;
[*]cout << "5) Search for a card" << endl;
[*]cout << "6) Print rolodex to screen" << endl;
[*]cout << "7) Quit" << endl;
[*]}
[*]//*******************************************************************************************


[*]void PrintToScreen()
[*]//Pre - Information is on text file
[*]//Post - Text file is printed on screen
[*]{
[*]  string line;
[*]  ifstream myFile ("rolodex.txt");
[*]  if (myFile.is_open())
[*]  {
[*]    while (!myFile.eof())
[*]    {
[*]      getline (myFile,line);
[*]	  cout << endl;
[*]      cout << line << endl << endl;
[*]    }
[*]    myFile.close();
[*]  }

[*]  else cout << "File not found"; 

[*]}

[*]//*****************************************************************************

[*]void findCard()
[*]//Pre- User inputs exact name desired (ie Jones != jones)
[*]//Post - Returns card information

[*]{
[*]	size_t found=' ';
[*]    char target[30] = "";
[*]    string line;
[*]    ifstream myFile ("rolodex.txt");
[*]    {
[*]	cout << "Enter the last name to search for: ";
[*]	cin >> target;

[*]	myFile.clear();
[*]	myFile.seekg(0);
[*]	while (getline(myFile,line))
[*]	{
[*]		if ( (found = line.find (target, 0)) == 0 )
[*]		{
[*]          cout << endl;                            
[*]          cout << line << endl;	
[*]          break;
[*]	    }
[*]	    }
[*]	
[*]		if ( (found = line.find (target, 0)) != 0 )
[*]		{
[*]		cout << target << " not found" << endl;
[*]		}

[*]		myFile.close();
[*]	}
[*]	
[*]}

[*]//********************************************************************************

[*]void addCard()

[*]{
[*]	Card newCard;
[*]	ofstream outFile ("rolodex.txt",ofstream::app);
[*]	cout << "Please enter first name:" << endl;
[*]    cin >> newCard.firstName;
[*]	cout << "Please enter last name:" << endl;
[*]	cin >> newCard.lastName;
[*]	cout << "Enter phone number (no spaces):" << endl;
[*]	cin >> newCard.phoneNumber;
[*]	cout << "Please enter the email address:" << endl;
[*]	cin >> newCard.eMail;
[*]	cout << endl;
[*]	cout << newCard.firstName <<" "<< newCard.lastName<< " has been added." << endl;
[*]	
[*]	outFile << newCard.lastName <<" "<< newCard.firstName <<" "<< newCard.phoneNumber <<" "<< newCard.eMail << endl;
[*]	outFile.close();
[*]}
[*]//*****************************************************************************************************

[*]void readCard()
[*]{
[*]    string array[50]; 
[*]    int i = 0;
[*]    short loop=0; 
[*]    string line; 
[*]    
[*]	ifstream myFile ("rolodex.txt"); 
[*]    if (myFile.is_open()) 
[*]    {
[*]        while (! myFile.eof()) 
[*]        {
[*]			i++;
[*]            getline (myFile,line); 
[*]            array[loop] = line;
[*]            loop++;
[*]			
[*]        }
[*]		cout << endl;
[*]		cout << i - 2 << " cards read" << endl;
[*]        myFile.close(); 
[*]    }
[*]    else cout << "File not found"; 
[*]}
[*]//*************************************************************************************

[*]void writeToFile()
[*]{
[*]	string newFile;
[*]	int i = 0;
[*]    string array[100];
[*]    int max = 100;
[*]    int j = 0;
[*]    ifstream inFile("rolodex.txt",ios::in | ios::binary);
[*]	cout << "Enter the file name:";
[*]	cin >> newFile;
[*]	ofstream outFile(newFile.c_str());
[*]	
[*]  while( inFile >> array[j] && j < max)
[*]  {
[*]  j++;
[*]  }
[*]  
[*]  for(i = 0; i < 50; i++)
[*]  {
[*]  outFile << array[i] <<endl;
[*]  }
[*]  cin.get();
[*]  
[*]  inFile.close();
[*]  outFile.close();

[*]}
[*]//***********************************************************************************************
[*]void deleteCard()
[*]{

[*]	//code 

[*]}
[/LIST]

On line 182, every time you output you also output a newline character.

On line 182, every time you output you also output a newline character.

That would just print them all on one line. Like this:

lastfirstphoneemaillastfirstphoneemaillastfirstphoneemail...

But thanks for the tip. I should have noticed that earlier.

Yes, obviously removing causes it to be put on one line, just wanted to let you know where the problem was :)

I got the write to file function working. Just need to finish up the delete function. In case anyone was paying attention.

[LIST=1]
[*]Card card[100];
[*]string newFile;
[*]int num = 0;
[*]int counter = 0;
[*]int i = 0;
[*]int j = 0;

[*] Card temp;
[*] ifstream inFile;
[*] inFile.open("rolodex.txt");
[*] 
[*]    cout << "Enter the file name:";
[*]	cin >> newFile;
[*]	ofstream outFile(newFile.c_str());
[*]	while(!inFile.eof())
[*] { 
[*]	 counter+= 1;
[*] 
[*]  inFile >> card[i].lastName;
[*]  inFile >> card[i].firstName;
[*]  inFile >> card[i].phoneNumber;
[*]  inFile >> card[i].eMail;
[*]  i++;
[*] }
[*] 
[*] outFile << counter - 2 << endl;
[*] for (i=1; i < counter; i++)
[*] {
[*]  outFile << card[i].lastName << " " << card[i].firstName << " " << card[i].phoneNumber <<" " << card[i].eMail<<endl;
[*] }
[*] inFile.close();
[*]  outFile.close();
[*]  cout << endl;
[*]  cout << newFile << " has been written" << endl;

[*]}
[/LIST]
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.