Visual C++ records

Started by
24 comments, last by bigjoe11a 16 years, 3 months ago
Its fine to ask for direction and help, but its generally frowned upon to ask people to provide you with complete solutions. Not only that, but its also usually a sign that you're trying to grasp further than your reach. In other words, if you have to ask someone to do it all for you, you're not ready to be doing it in the first place.

There's also some concern over those who come here asking for solutions to homework assignments, and a policy against providing solutions when that scenario is apparent. Forum goers are typically weary of providing complete solutions due to the possibility of a poster lying about whether it is homework or not.

throw table_exception("(? ???)? ? ???");

Advertisement
Quote:Original post by ravyne2001
Its fine to ask for direction and help, but its generally frowned upon to ask people to provide you with complete solutions. Not only that, but its also usually a sign that you're trying to grasp further than your reach. In other words, if you have to ask someone to do it all for you, you're not ready to be doing it in the first place.

There's also some concern over those who come here asking for solutions to homework assignments, and a policy against providing solutions when that scenario is apparent. Forum goers are typically weary of providing complete solutions due to the possibility of a poster lying about whether it is homework or not.


ravyne, Sorry theres nothing I can do about that problem. If kids want to cheat then theres nothing you or I can do.

The only reason I'm here is to learn C++., Mike sample doesn't work. any way. I getting more errors then before. It telling me that a missing ; is needed and the demo I saw didn't say to put one there. Where Mike didn't put them it's telling me to add one there. So right now I don't know what to do. I'm 48 Years old and disabled. I have nothing else to do.





Apologies for the assumption then. We don't get many 48 year olds who are here to learn C++, usually the one's we deal with that are new to programming here are 12 year olds who discovered this site, then jump way too far ahead of themselves because they suddenly think they can write the next big MMO, when they should be learning for loops.

Anyhow, Mike did update his sample while I was making the last post, so hopefully that has helped.

If you'd like some good, free, online resources I can recommend Thinking in C++ and Thinking in Patterns by Bruce Eckel who is one of the foremost authors on C++ and programming. Both come with PDF and Source code, and a list of download mirrors is here!

throw table_exception("(? ???)? ? ???");

Quote:Original post by ravyne2001
Apologies for the assumption then. We don't get many 48 year olds who are here to learn C++, usually the one's we deal with that are new to programming here are 12 year olds who discovered this site, then jump way too far ahead of themselves because they suddenly think they can write the next big MMO, when they should be learning for loops.

Anyhow, Mike did update his sample while I was making the last post, so hopefully that has helped.

If you'd like some good, free, online resources I can recommend Thinking in C++ and Thinking in Patterns by Bruce Eckel who is one of the foremost authors on C++ and programming. Both come with PDF and Source code, and a list of download mirrors is here!


Well I sorry, too. I guess I'm just getting too old. any way like I said I all ready tried mike's update one and well it's giving me all kinds of errors. Right now at this time I have no idea what to do.

Any way I downloaded some of the tutorials and well I just don't under stand them. I wish I had a tutor.

Here is a slightly different example of a phone book skeleton.
There is still a lot more to do, and its not the most efficient phone book on the planet, in particular:
-There is other data structures better suited for searching than the vector.
-It will not allow you to use multiple words in names and addresses.
-Its case sensitive.
-There is no edit function. Its quite easy to write it though. Pretty much the same as the findEntry function, except rather than printing the items in the entry one would modify them based on user input

#include <vector>#include <string>#include <sstream> // stringstream#include <fstream> // ifstream, ofstream#include <iostream>#include <cstdlib> // systemusing namespace std;struct Entry // The record struct{		string name, address, phone;};vector<Entry> book; // The phone book // This function will create a new entry based on user input and add it to the phone bookvoid createEntry(){	Entry entry;	cout << "Name: ";	getline(cin, entry.name);	cout << "Address: ";	getline(cin, entry.address);	cout << "Phone: ";	getline(cin, entry.phone);	book.push_back(entry);}// Function to find and delete an entry in the bookvoid deleteEntry(){	string name;	cout << "Name to delete: ";	getline(cin, name);	for(vector<Entry>::iterator i = book.begin(); i != book.end(); i++)	{		if(i->name == name)		{			book.erase(i);			break;		}	}}// Function to find and display an entryvoid findEntry(){	string name;	cout << "Name to find: ";	getline(cin, name);	for(vector<Entry>::iterator i = book.begin(); i != book.end(); i++)	{		if(i->name == name)		{			cout << "Name: " << i->name << " Address: " << i->address << " Phone: " << i->phone << endl;			break;		}	}}// Function to list all entries in the phone bookvoid listBook(){	for(vector<Entry>::iterator i = book.begin(); i != book.end(); i++)		cout << i->name << " " << i->address << " " << i->phone << endl;}// Function to load entries from file into the phone bookvoid loadBook(){	string line; // working string	Entry entry; // working entry	ifstream fin("phonebook.txt");	if(fin.is_open()) // if file exists...	{		while(fin.good()) // loop until error or end of file...		{			getline(fin, line); // get next line from file			stringstream ss(line); // fill a stringstream with the line			ss >> entry.name >> entry.address >> entry.phone; // parse the line into the working entry			book.push_back(entry); // store the entry in the book		}	}}// Function to store entries from the phone book into filevoid saveBook(){	ofstream fout("phonebook.txt");	// Iterate through each entry in the book and save it on a single line	for(vector<Entry>::iterator i = book.begin(); i != book.end(); i++)			fout << i->name << " " << i->address << " " << i->phone << endl;	}int main(){		string input; // working string	loadBook(); // Load the phone book at startup...	while(1) // loop forever...	{		system("cls");		cout << "Commands: create, delete, find, list, quit" << endl;		getline(cin, input);		// Execute our functions based on what command the user typed		if(input == "quit")			break; // break out of the loop and quit program		else if(input == "create")			createEntry();			else if(input == "delete")			deleteEntry();			else if(input == "find")			findEntry();			else if(input == "list")			listBook();					else cout << "Unknown command" << endl;		system("pause");	}		saveBook(); // Store the current phone book to file before we exit	return 0;}


edit:
Each entry is represented in the file by a single line, and each item on that line is separated by a whitespace. However, I noticed a bug, since each line written to file ends with a newline, the code above will read one line at the end containing only a newline, resulting in an extra entry being added to the book.
Here is a dirty fix:
...// Function to load entries from file into the phone bookvoid loadBook(){	string line; // working string	Entry entry; // working entry	ifstream fin("phonebook.txt");	if(fin.is_open()) // if file exists...	{		while(!fin.eof()) // loop until end of file...		{			getline(fin, line); // get next line from file			if(line.length() < 2) // dirty fix to skip the last newline				break;			stringstream ss(line); // fill a stringstream with the line			ss >> entry.name >> entry.address >> entry.phone; // parse the line into the working entry			book.push_back(entry); // store the entry in the book		}	}}...


[Edited by - pulpfist on January 5, 2008 4:00:40 AM]
By changing loadBook and saveBook slightly it will work with multiple words.
The separators in the file is now a : rather than whitespace
// Function to load entries from file into the phone bookvoid loadBook(){	char cbuf[128];	string line; // working string	Entry entry; // working entry	ifstream fin("phonebook.txt");	if(fin.is_open()) // if file exists...	{		while(!fin.eof()) // loop until error or end of file...		{			getline(fin, line); // get next line from file			if(line.length() < 2) // dirty fix to skip the last newline				break;			stringstream ss(line);	                        // extract three items from the stream separated by :			ss.getline(cbuf, 128, ':');			entry.name = cbuf;			ss.getline(cbuf, 128, ':');			entry.address = cbuf;			ss.getline(cbuf, 128, '\n');			entry.phone = cbuf;						book.push_back(entry); // store the entry in the book		}	}}// Function to store entries from the phone book into filevoid saveBook(){	ofstream fout("phonebook.txt");	// Iterate through each entry in the book	for(vector<Entry>::iterator i = book.begin(); i != book.end(); i++)			fout << i->name << ':' << i->address << ':' << i->phone << endl;	}
Thanks I will give your options a try

Quote:Original post by pulpfist
By changing loadBook and saveBook slightly it will work with multiple words.
The separators in the file is now a : rather than whitespace
*** Source Snippet Removed ***


Any way I tried your sample and well I'm getting errors on your sample too.

Heres a list of the errors

c:\documents and settings\joe\my documents\visual studio 2008\projects\phone book\phone book\phone book.cpp(135) : error C2084: function 'void loadBook(void)' already has a body
c:\documents and settings\joe\my documents\visual studio 2008\projects\phone book\phone book\phone book.cpp(71) : see previous definition of 'loadBook'
c:\documents and settings\joe\my documents\visual studio 2008\projects\phone book\phone book\phone book.cpp(187) : error C2084: function 'void saveBook(void)' already has a body
c:\documents and settings\joe\my documents\visual studio 2008\projects\phone book\phone book\phone book.cpp(90) : see previous definition of 'saveBook'
Build log was saved at "file://c:\Documents and Settings\Joe\My Documents\Visual Studio 2008\Projects\Phone Book\Phone Book\Debug\BuildLog.htm"
Phone Book - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I don't get any of this. I was hoping it would be easy to write program in C++ and I guess not. Until I can get it to work. Then I can take time out to study the lines and find out what each line does and how it works


Oh yea the first code example is the complete source.
The next two is just newer versions of the loadBook and saveBook functions.
Try to compile the first example first. If that works you can replace the loadBook and saveBook functions with those in the last code example.

Programming C++ is not exactly easy. I did a lot of hair pulling myself the first years =)
Quote:Original post by pulpfist
Oh yea the first code example is the complete source.
The next two is just newer versions of the loadBook and saveBook functions.
Try to compile the first example first. If that works you can replace the loadBook and saveBook functions with those in the last code example.

Programming C++ is not exactly easy. I did a lot of hair pulling myself the first years =)


Cool, That works. Thanks. Now I have some reading to do. some questions. Your ample doesn't required an record ID. Like the others were saying. Can I ask why you didn't add one. Or is there some thing else that I missed.

Some of the other members have been trying to help me add some colors to my work. Like adding colors to my prompts and so on.
This is what I was told to use. I just have no idea on how to use it.
They all tried to help witha gotoXY() command. and I can that to work too
void gotoxy(SHORT X, SHORT Y){	COORD position = {X, Y};	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);}// Change cursor position to overwrite some of the previous text COORD cursorPosition; cursorPosition.X = 9; cursorPosition.Y = 0; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPosition); std::cout << " Overwriting text..." << std::endl; //Change text to base colours  WORD textAttribute = FOREGROUND_RED;  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), textAttribute);  std::cout << "Red! ";  textAttribute = FOREGROUND_BLUE;  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), textAttribute);  std::cout << "Blue! ";  textAttribute = FOREGROUND_GREEN;  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), textAttribute);  std::cout << "Green! ";// Text can be made into other colours by combining the base colours  textAttribute = FOREGROUND_BLUE | FOREGROUND_GREEN;  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), textAttribute);  std::cout << "Cyan!" << std::endl; }


Any ideas form you would be great and again thanks for your sample.

This topic is closed to new replies.

Advertisement