Member Avatar for xcr

Hello all, This is my first post but i have lurked around the forums for a while (sans account)


my problem is thusly:

I have written a program to take information from a data file, copying values to a vector, allow the user to add/delete and print form this vector and then write out.

In theory, all of my code works, it compiles under gcc, and the program runs, the problem is, i do not think that it writes to the file, and i am not able to print entries from the vector even when i just created them.

I now link you to a pastebin with my code because i didn't want to spam the message board.

http://pastebin.be/17705

Many thanks!

>>i didn't want to spam the message board.
You won't. Just past the code between code tags and you will be all right. We can't copy from that link into our compiler's IDE.

[edit]Ok, so I didn't see the Download button at the bottom of the page :) [/edit]

>> input.open(\"data.txt\");

That should be input.open("data.txt"); -- remove the '\' characters because the quotes are not part of the filename.

You also need to delete the '\' characters every where else you want to print or use quoted text (strings). That character is called an escape character. Your compiler should have produced lots of errors throughout the code because of those escape '\' characters.

Member Avatar for xcr

I do not know what you mean by the '\' characters, they aren't there... the only time i used them is for newlines '\n' which are perfectly acceptable in all uses of code...

can you be more specific?

The "download" link somehow added backslashes before every single and double-quote.

I notice that you have no increment of the iterator in write_out. Add a ++itr before the end-brace of the while-loop.

If that doesn't fix the problem, can you be more specific? How would we recreate the problem?

[edit]
Now that I've actually run your code, I notice that you need to pass the vector by reference to add and del. So change their declarations like this:

void add(vector<people>& thing)
// ...
void del(vector<people>& thing)

You can probably think of a better name than "thing"!
[/edit]

>I notice that you need to pass the vector by reference to add and del.
Not only in add and del but in read_in too: this function fills a copy of its argument (passed by value) so the caller never gets any contents.

Member Avatar for xcr

thanks for your suggestion, I have not yet tried it out, but i will get back to you.

re: you can probably think of a better name than thing

yea, i probably could, but there are no points for creativity in programing

The "download" link somehow added backslashes before every single and double-quote.

Which is yet another reason to just simply post your code here.

> there are no points for creativity in programing

And how would you know? You don't seem to have much experience! :)

In fact, creativity is the only thing that gets points in programming. If it wasn't for the creativity and artistry involved, I wouldn't bother doing it. (Who would?)

And better names mean better programs. It has been said that good naming is one of the most difficult things to learn, and naming is certainly one of the worst parts of beginner programs (along with structure, a related problem).

In your case, I would rename your people struct to be People (with an uppercase P) and then call the vectors people (with a lowercase p).

Other points on your program:
* don't append to the file in write_out (overwrite the file instead)
* in read_in you need to check for eof after the first getline
* in struct address, declare numbers and zip as strings (they're not really integers since, for example, there's no sense in adding them)

Member Avatar for xcr

Thanks, I have incorperated most of your suggestions, (the rest I have not yet gotten to, specifically the naming, and my program works except for the not so small problem that it will not exit. I even rewrote the menu to toggle a bool ned exit and test the condition of the do while statement against that. Any more ideas?


In reference to the creativity involved in programing, I do find the idea of crafting something for a computer to execute to be a creative endeavor, and how better to improve than to watch and talk to masters of the craft.

Member Avatar for xcr

okay, one more small, but not so small detail that I have missed, I am turning to the wisdom of the masses.

The problem now is that the second entry is offset by a carriage return, I think that it has something to do with the read_in function, because when I ask for the entire phone book, the second entry is formatted wrong then.

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <ostream>
using namespace std;

void filecheck()
{
	ifstream input;
	input.open("data.txt");
	
	//check for existance
	if(!input)
	{
		cerr << "File could not be opened" << endl;
		exit(1);
	}
	input.close();
}
struct address{
	string numbers, zip, street, city, state;
};
	
struct people {
	// all the fields of data to record
	string name;
	address adr;
	string number;
	string DOB;
};
void add(vector<people>& mod)
{
	people temp;
	cout << "Please enter the person's info:\n"
	<< "name:\n" ;
	cin >> temp.name;
	cout << "address:\n";
	cin >> temp.adr.numbers >> temp.adr.street ;
	cout << "city:\n";
	cin >> temp.adr.city;
	cout << "state:\n";
	cin >>temp.adr.state;
	cout <<"zip:\n";
	cin >> temp.adr.zip;

	cout << "phone number:\n";
	cin >> temp.number;

	cout << "date of birth:\n";
	cin >> temp.DOB;;
// write to the vector
	mod.push_back(temp);

}
void del(vector<people>& mod)
{
	string delvar;
	cout << "Please enter the name of the person you wanted to delete: ";
	cin >> delvar;
	
	//delutil(delvar)
   vector<people>::iterator itr = mod.begin();
   while ( itr != mod.end() )
    {
	if ( itr->name == delvar )
	{
	     itr = mod.erase( itr ); // Will return next valid iterator
	      break;
	}
	else
	     itr++;
    }
}
void printer(people p)
{
	cout << p.name << " "<< p.adr.numbers <<" " << p.adr.street
		<< "\n" << p.adr.city << " " << p.adr.state << " " << p.adr.zip 
		<< "\n"<< p.number << " " << p.DOB <<"\n"; 
}
void print_all(vector<people> pr)
{
	for(int i = 0; i< pr.size(); ++i)
	{
		printer(pr[i]);
	}
}
void print_info(vector<people> thing)
{
	string entry;
	cout << "Please enter the name of the entry you wish to print: ";
	cin >> entry;

	//find and print to screen:
   vector<people>::iterator itr = thing.begin();
   while ( itr != thing.end() )
    {
	if ( itr->name == entry )
	{
		printer(*itr);
		break;
	}else{
	    itr++;  }
    }
}
void print_date(vector<people> thing)
{
	string entry;
	cout << "Please enter the name of the entry whose DOB you wish to print: ";
	cin >> entry;

	//find and print to screen:
   vector<people>::iterator itr = thing.begin();
   while ( itr <= thing.end() )
    {
	if ( itr->name == entry )
	{
		cout << "\n" << itr->DOB <<"\n";
		break;
	}else{
	    itr++;  }
    }
}
void print_num(vector<people> thing)
{
	string entry;
	cout << "Please enter the name of the entry whose number you wish to print: ";
	cin >> entry;

	//find and print to screen:
   vector<people>::iterator itr = thing.begin();
   while ( itr != thing.end() )
    {
	if ( itr->name == entry )
	{
		cout << "\n" << itr->number <<"\n";
		break;
	}else{
	    itr++;  }
    }
}
void write_out(vector<people> out)
{
	ofstream output;
	output.open("data.txt");
	vector<people>::iterator itr = out.begin();
  	 while ( itr != out.end() )
  	  {
	     output << itr->name << "~"<< itr->adr.numbers <<" " << itr->adr.street
		<< "~" << itr->adr.city << "~" << itr->adr.state << "~" << itr->adr.zip 
		<< "~"<< itr->number << "~" << itr->DOB <<"\n"; 
		++itr;
	  }
}
void read_in(vector<people>& enter)
{
	ifstream input;
	input.open ("data.txt", std::ios::in); //open the file
	people p;
	
	while(! input.eof())
	{
		getline(input,p.name,'~');
		getline(input,p.adr.numbers,' ');
		getline(input,p.adr.street,'~');
		getline(input,p.adr.city,'~');
		getline(input,p.adr.state,'~');
		getline(input,p.adr.zip,'~');
		getline(input,p.number,'~');
		getline(input,p.DOB,'~');
		
		enter.push_back(p);

		//cout << p.name;
		
	}
	input.close();
}

int main() {

filecheck();

	vector<people> book; // the vector to use in the program

	read_in(book);  // copy the file to memory

	int choice; //choice to read into
	bool exit = 0; //exit check

	do {

	cout << "Make a selection from these menu items:\n"
		<< "1. Add People\n"
		<< "2. Delete People\n"
		<< "3. Print the phonebook\n"
		<< "4. Print an individual\n"
		<< "5. Print a birthdate\n"
		<< "6. Print a phone number\n"
		<< "7. Quit    " << "Please enter your choice:\n";
	cin >> choice;

		switch (choice)
		   {
			case 1:		
			add(book); 		
			break;

			case 2:
			del(book);		
			break;

			case 3:
			print_all(book);
			break;

			case 4:
			print_info(book);
			break;

			case 5:
			print_date(book);
			break;

			case 6:
			print_num(book);
			break;

			case 7:
			exit = 1;
			break;

			default:
			cout << "You didn't enter a valid choice, please try again. \n";
			continue;
		   }
	}while ( exit != 1);

	write_out(book); // write to file
	cout << "Thanks! Have a nice day!\n";
	
     return 0;
}

I can post the data.txt file if anyone would like to see the output of the program.

Thanks a bunch again all!
~

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.