| | |
stuck with strings and there relation to a file.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 16
Reputation:
Solved Threads: 0
my code so far......
if you notice under case 4 - there is a an attempt at some rather bad search code. please help me make my file searchable. i have tried so many different ideas like linear search. but nothing is working. note strcmp and strcpy is a must. please point me in the right direction if you read this code.
many thanks to those who help me - no need to rush.
sincerly stephen.a.barratt - posted name to show it is my coding.
C++ Syntax (Toggle Plain Text)
// ProjectDevelopment.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> // This libary allows standard input and output operations. #include <string> // strings are used as tempory storage - for the input to a text based file #include <vector> // at one point in my program i expermented with vectors for data storage using namespace std; // (or maybe std::getline;, std:: string;, std:: cout; , std:: cin; (namespace howecver is a short hand version to reduce code and possible errors. #include <fstream> // this allows filestreaming and allows the reading and writing of a file #include <iomanip> #include <cstring> int intEnterANumber; int intConfirmation; ifstream in ( "Store" ); string temp; string strConfirmation; string strFirstName; string strSurname; string strMiddleName; string strNickName; string strTelephoneNo; string strMobileNo; string strHouseNo; string strRoadStreet; string strVillageTown; string strPostcode; int AddFirstName(int intEnterANumber) { cout << "Please Enter the FirstName \n"; cin >> strFirstName; ofstream File; File.open("Store.txt",ios::app); File << strFirstName; File <<","; File.close(); return 0; } int AddSurname(int intEnterANumber) { cout << "Please Enter the Surname \n"; cin >> strSurname; ofstream File; File.open("Store.txt",ios::app); File << strSurname; File <<","; File.close(); return 0; } int AddMiddleName(int intEnterANumber) { cout << "Please Enter the Middle Name\n"; cin >> strMiddleName; ofstream File; File.open("Store.txt",ios::app); File << strMiddleName; File <<","; File.close(); return 0; } int AddTelephoneNo(int intEnterANumber) { cout <<"Please Enter a Telephone Number\n"; cin >> strTelephoneNo; ofstream File; File.open("Store.txt",ios::app); File << strTelephoneNo; File <<","; File.close(); return 0; } int AddAddress(int intEnterANumber) // a function which is called by the switch statement. { cout <<"Please Enter the House No\n"; cin >> strHouseNo; ofstream File; File.open("Store.txt",ios::app); File << strHouseNo; File <<","; File.close(); cout <<"Please Enter the road or street name\n"; // use street(ST) or (R) for road or street check validation. cin >> strRoadStreet; File.open("Store.txt",ios::app); File << strRoadStreet; File <<","; File.close(); cout <<"Please Enter the Village or town name\n"; // use Village or (VI)or Town (T) for validation check on town or street cin >> strVillageTown; File.open("Store.txt",ios::app); File << strVillageTown; File <<","; File.close(); cout <<"Please Enter the Postcode\n"; cin >> strPostcode; // Check length during validation. With country as well we could have allowed selection of zipcodes etc. File.open("Store.txt",ios::app); File << strPostcode; File <<","; File <<"\n"; File.close(); return 0; } int main(int argc, char *argv[]) // could just be just int main { do { int intEnterANumber; cout << "Welcome To Your Phone \n\n"; cout << " (1) Enter a New Person's Details \n"; cout << " (2) Enter a New Person FirstName and telephone No. \n"; cout << " (3) Search Phone Book for all Names \n"; cout << " (4) Search Phone Book by Firstname n\n"; cout << " (5) Exit \n\n"; cout << "Please Select A Number From the Options Above\n "; cin >> intEnterANumber; switch (intEnterANumber) { case 1: AddFirstName(intEnterANumber); AddMiddleName(intEnterANumber); AddSurname(intEnterANumber); AddTelephoneNo(intEnterANumber); AddAddress(intEnterANumber); break; case 2: AddFirstName(intEnterANumber); AddTelephoneNo(intEnterANumber); break; //case 3: //if(!in){ // cout << "Cannot open file."; // exit (1); //} //char str[255]; //while(in){ //in.getline(str, 255); //cout << str << endl; //} // in.close(); //} break; case 4: //bits of code not quite right char* strSearchData; cout << "Please enter a name for searching \n"; cin >> strSearchData,255 ; in.open("Store.txt"); char* strDataStore; while ( in.getline( strDataStore, 50)); // tried to write token to seperate words in text file (creating split). //using commors. strcmp (strDataStore,strSearchData ); // sought of works cause error on intilzation. { // Planning to use if strcmp is true do below if false do other) cout << strDataStore <<endl; in.close(); } break; } // case 5: // exit(EXIT_SUCCESS) // or // exit(1) } while (intEnterANumber !=6 ); return 0; }
many thanks to those who help me - no need to rush.
sincerly stephen.a.barratt - posted name to show it is my coding.
Last edited by Narue; May 24th, 2009 at 10:51 pm. Reason: added code tags
Please, post your code with code tag:
[code=cplusplus]
source
[/code]
Read this forum announcement: http://www.daniweb.com/forums/announcement8-3.html
[code=cplusplus]
source
[/code]
Read this forum announcement: http://www.daniweb.com/forums/announcement8-3.html
Is it so difficult to post using code tags?
It's nearly mentioned everywhere: in the forum announcement which you didn't read, on the background of the text box where you type in your message when making a post, above the forum announcements, in the 'Read this before posting'.
It's nearly mentioned everywhere: in the forum announcement which you didn't read, on the background of the text box where you type in your message when making a post, above the forum announcements, in the 'Read this before posting'.
Last edited by tux4life; May 24th, 2009 at 2:05 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: May 2009
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
Is it so difficult to post using code tags?
It's nearly mentioned everywhere: in the forum announcement which you didn't read, on the background of the text box where you type in your message when making a post, above the forum announcements, in the 'Read this before posting'.
suppose not sorry i have been ill - and did not read everything i was supposed too. so can anyone help me with my problem please.
•
•
•
•
suppose not sorry i have been ill - and did not read everything i was supposed too. so can anyone help me with my problem please.
# ) was asked too much?You must be very ill then...
If you're ill then it's better to stay away from your computer and rest a bit
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Case 4 - where to start!?
You cannot just declare a char* and then use it for input - you must allocate memory to the pointer. Why not just allocate it as a char array, like
Same for strDataStore. Note that you are using getline( ) reading into it from the file, but again, it has no memory allocated.
When you use strcmp( ) you need to either store the return value or use the function's return as a condition in a branching right then and there. Remember that it returns a value less than 0, 0 or greater than 0. It does not give a correct true/false evaluation - actually it's backwards, in that 0 (false) return indicates a match.
If all you're trying to match is the first name, which one would expect is the first item on the data file line, you could use getline( ) with blank space as the delimiter, so you read in only first name. Then read the rest of the line into another string, using newline as delimiter.
Oh, watch out for the semicolon following the while( in.getline( )...) - that's going to make your loop just read through the whole file and do nothing to it.
You cannot just declare a char* and then use it for input - you must allocate memory to the pointer. Why not just allocate it as a char array, like
char strSearchData[128]; (Choose a size you think is sufficient for any fool who attempts to abuse your program.) Then use getline( ) to read in the input, which will protect against buffer overflow (that is, writing beyond the bounds of the array.)Same for strDataStore. Note that you are using getline( ) reading into it from the file, but again, it has no memory allocated.
When you use strcmp( ) you need to either store the return value or use the function's return as a condition in a branching right then and there. Remember that it returns a value less than 0, 0 or greater than 0. It does not give a correct true/false evaluation - actually it's backwards, in that 0 (false) return indicates a match.
If all you're trying to match is the first name, which one would expect is the first item on the data file line, you could use getline( ) with blank space as the delimiter, so you read in only first name. Then read the rest of the line into another string, using newline as delimiter.
C++ Syntax (Toggle Plain Text)
while ( in.getline( strFirstName, 50, ' ' ) ) { in.getline( strRestOfLine, 100, '\n' ); //compare the first name, do stuff }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: May 2009
Posts: 16
Reputation:
Solved Threads: 0
update of my code new question.
thank you - last poster it works without errors now but iam am still a very stuck noob.
when you said i need other stuff what time of stuff do i need - to complete my search. don't have to be specfic but some read up topics would be nice thanks.
C++ Syntax (Toggle Plain Text)
// ProjectDevelopment.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> // This libary allows standard input and output operations. #include <string> // strings are used as tempory storage - for the input to a text based file #include <vector> // at one point in my program i expermented with vectors for data storage using namespace std; // (or maybe std::getline;, std:: string;, std:: cout; , std:: cin; (namespace howecver is a short hand version to reduce code and possible errors. #include <fstream> // this allows filestreaming and allows the reading and writing of a file #include <iomanip> #include <cstring> int intEnterANumber; int intConfirmation; ifstream in ( "Store" ); string temp; string strConfirmation; string strFirstName; string strSurname; string strMiddleName; string strNickName; string strTelephoneNo; string strMobileNo; string strHouseNo; string strRoadStreet; string strVillageTown; string strPostcode; int AddFirstName(int intEnterANumber) { cout << "Please Enter the FirstName \n"; cin >> strFirstName; ofstream File; File.open("Store.txt",ios::app); File << strFirstName; File <<" "; File.close(); return 0; } int AddSurname(int intEnterANumber) { cout << "Please Enter the Surname \n"; cin >> strSurname; ofstream File; File.open("Store.txt",ios::app); File << strSurname; File <<" "; File.close(); return 0; } int AddMiddleName(int intEnterANumber) { cout << "Please Enter the Middle Name\n"; cin >> strMiddleName; ofstream File; File.open("Store.txt",ios::app); File << strMiddleName; File <<" "; File.close(); return 0; } int AddTelephoneNo(int intEnterANumber) { cout <<"Please Enter a Telephone Number\n"; cin >> strTelephoneNo; ofstream File; File.open("Store.txt",ios::app); File << strTelephoneNo; File <<" "; File.close(); return 0; } int AddAddress(int intEnterANumber) // a function which is called by the switch statement. { cout <<"Please Enter the House No\n"; cin >> strHouseNo; ofstream File; File.open("Store.txt",ios::app); File << strHouseNo; File <<" "; File.close(); cout <<"Please Enter the road or street name\n"; // use street(ST) or (R) for road or street check validation. cin >> strRoadStreet; File.open("Store.txt",ios::app); File << strRoadStreet; File <<" "; File.close(); cout <<"Please Enter the Village or town name\n"; // use Village or (VI)or Town (T) for validation check on town or street cin >> strVillageTown; File.open("Store.txt",ios::app); File << strVillageTown; File <<" "; File.close(); cout <<"Please Enter the Postcode\n"; cin >> strPostcode; // Check length during validation. With country as well we could have allowed selection of zipcodes etc. File.open("Store.txt",ios::app); File << strPostcode; File <<" "; File <<"\n"; File.close(); return 0; } int main(int argc, char *argv[]) // could just be just int main { do { int intEnterANumber; cout << "Welcome To Your Phone \n\n"; cout << " (1) Enter a New Person's Details \n"; cout << " (2) Enter a New Person FirstName and telephone No. \n"; cout << " (3) Search Phone Book for all Names \n"; cout << " (4) Search Phone Book by Firstname n\n"; cout << " (5) Exit \n\n"; cout << "Please Select A Number From the Options Above\n "; cin >> intEnterANumber; switch (intEnterANumber) { case 1: AddFirstName(intEnterANumber); AddMiddleName(intEnterANumber); AddSurname(intEnterANumber); AddTelephoneNo(intEnterANumber); AddAddress(intEnterANumber); break; case 2: AddFirstName(intEnterANumber); AddTelephoneNo(intEnterANumber); break; //case 3: //if(!in){ // cout << "Cannot open file."; // exit (1); //} //char str[255]; //while(in){ //in.getline(str, 255); //cout << str << endl; //} // in.close(); //} break; case 4: //bits of code not quite right char strSearchData[255]; cout << "Please enter a name for searching \n"; cin >> strSearchData,255 ; in.open("Store.txt"); char strDataStore[255]; char strRestOfLine[255]; while ( in.getline( strDataStore, 255, ' ' ) ) { in.getline( strRestOfLine, 255, '\n' ); while(strcmp (strDataStore,strSearchData) !=0); { cout << strDataStore << strRestOfLine <<endl; } } in.close(); } break; // case 5: // exit(EXIT_SUCCESS) // exit(1) } while (intEnterANumber !=6 ); return 0; } // ProjectDevelopment.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> // This libary allows standard input and output operations. #include <string> // strings are used as tempory storage - for the input to a text based file #include <vector> // at one point in my program i expermented with vectors for data storage using namespace std; // (or maybe std::getline;, std:: string;, std:: cout; , std:: cin; (namespace howecver is a short hand version to reduce code and possible errors. #include <fstream> // this allows filestreaming and allows the reading and writing of a file #include <iomanip> #include <cstring> int intEnterANumber; int intConfirmation; ifstream in ( "Store" ); string temp; string strConfirmation; string strFirstName; string strSurname; string strMiddleName; string strNickName; string strTelephoneNo; string strMobileNo; string strHouseNo; string strRoadStreet; string strVillageTown; string strPostcode; int AddFirstName(int intEnterANumber) { cout << "Please Enter the FirstName \n"; cin >> strFirstName; ofstream File; File.open("Store.txt",ios::app); File << strFirstName; File <<" "; File.close(); return 0; } int AddSurname(int intEnterANumber) { cout << "Please Enter the Surname \n"; cin >> strSurname; ofstream File; File.open("Store.txt",ios::app); File << strSurname; File <<" "; File.close(); return 0; } int AddMiddleName(int intEnterANumber) { cout << "Please Enter the Middle Name\n"; cin >> strMiddleName; ofstream File; File.open("Store.txt",ios::app); File << strMiddleName; File <<" "; File.close(); return 0; } int AddTelephoneNo(int intEnterANumber) { cout <<"Please Enter a Telephone Number\n"; cin >> strTelephoneNo; ofstream File; File.open("Store.txt",ios::app); File << strTelephoneNo; File <<" "; File.close(); return 0; } int AddAddress(int intEnterANumber) // a function which is called by the switch statement. { cout <<"Please Enter the House No\n"; cin >> strHouseNo; ofstream File; File.open("Store.txt",ios::app); File << strHouseNo; File <<" "; File.close(); cout <<"Please Enter the road or street name\n"; // use street(ST) or (R) for road or street check validation. cin >> strRoadStreet; File.open("Store.txt",ios::app); File << strRoadStreet; File <<" "; File.close(); cout <<"Please Enter the Village or town name\n"; // use Village or (VI)or Town (T) for validation check on town or street cin >> strVillageTown; File.open("Store.txt",ios::app); File << strVillageTown; File <<" "; File.close(); cout <<"Please Enter the Postcode\n"; cin >> strPostcode; // Check length during validation. With country as well we could have allowed selection of zipcodes etc. File.open("Store.txt",ios::app); File << strPostcode; File <<" "; File <<"\n"; File.close(); return 0; } int main(int argc, char *argv[]) // could just be just int main { do { int intEnterANumber; cout << "Welcome To Your Phone \n\n"; cout << " (1) Enter a New Person's Details \n"; cout << " (2) Enter a New Person FirstName and telephone No. \n"; cout << " (3) Search Phone Book for all Names \n"; cout << " (4) Search Phone Book by Firstname n\n"; cout << " (5) Exit \n\n"; cout << "Please Select A Number From the Options Above\n "; cin >> intEnterANumber; switch (intEnterANumber) { case 1: AddFirstName(intEnterANumber); AddMiddleName(intEnterANumber); AddSurname(intEnterANumber); AddTelephoneNo(intEnterANumber); AddAddress(intEnterANumber); break; case 2: AddFirstName(intEnterANumber); AddTelephoneNo(intEnterANumber); break; //case 3: //if(!in){ // cout << "Cannot open file."; // exit (1); //} //char str[255]; //while(in){ //in.getline(str, 255); //cout << str << endl; //} // in.close(); //} break; case 4: //bits of code not quite right char strSearchData[255]; cout << "Please enter a name for searching \n"; cin >> strSearchData,255 ; in.open("Store.txt"); char strDataStore[255]; char strRestOfLine[255]; while ( in.getline( strDataStore, 255, ' ' ) ) { in.getline( strRestOfLine, 255, '\n' ); while(strcmp (strDataStore,strSearchData) !=0); { cout << strDataStore << strRestOfLine <<endl; } } in.close(); } break; // case 5: // exit(EXIT_SUCCESS) // exit(1) } while (intEnterANumber !=6 ); return 0; }
thank you - last poster it works without errors now but iam am still a very stuck noob.
when you said i need other stuff what time of stuff do i need - to complete my search. don't have to be specfic but some read up topics would be nice thanks.
Firstly, I am not going to do the functions for you, but I'll show you where you are going wrong, and give you ideas on how to correct them. The following code is extracted from your program's
EDIT: (To the OP) You have fixed this...
EDIT: (To the OP) This(^) still needs to be looked into...
EDIT: (To the OP) This(^) has been fixed...
EDIT: (To the OP) This(^) still needs to be looked into...
EDIT: (To the OP) This(^) still needs to be looked into...
Note: This statement is causing an error because you are passing uninitialized pointers into the function.
This statement should probably be inside your
Note: The above should be within your
EDIT: (To the OP) This(^) still needs to be looked into...
As for your question on sorting through a list of first-names - The bubble-sort algorithm is easy to learn and apply, so I would recommend you apply this as the sorting technique. Note: You will have to load all the First-Names into a
Here: A tutorial on the Bubble sort algorithm
Here: A program that shows how to implement the bubble sort algorithm for an array of strings.
Hope this helps!
EDIT: The post directly above this was posted as I was writing this... sorry about that... but the links might still be useful, as the new code also requires some tinkering...
case 4: , with added //<MARKER (x) comments to indicate the lines that are wrong and/or unnecessary... C++ Syntax (Toggle Plain Text)
case 4: { // Added this brace for clarity //bits of code not quite right char* strSearchData; //< MARKER (1) cout << "Please enter a name for searching \n"; cin >> strSearchData,255 ; //< MARKER (2) in.open("Store.txt"); char* strDataStore; //< MARKER (3) while ( in.getline( strDataStore, 50)); //< MARKER (4) // tried to write token to seperate words in text file (creating split). //using commors. //< MARKER (5) strcmp (strDataStore,strSearchData ); //< MARKER (6) // sought of works cause error on intilzation. { // Planning to use if strcmp is true do below if false do other) cout << strDataStore <<endl; //< MARKER (7) <- EDIT: Ignore this marker in.close(); } //< MARKER (8) <- EDIT: Ignore this marker } break;
//< MARKER (1) : The char* is uninitialized (memory has not been allocated for it to act as an array of characters). Its basically unusable for the purpose you have outlined it for. I would recommend for you to go through this tutorial on pointers, and their correct usage. Try the new operator to allocate memory for the char* .EDIT: (To the OP) You have fixed this...
//< MARKER (2) : What exactly where you thinking when you wrote this? Here is a tutorial on how to use the getline function to receive a character array as input from the user, and consequentially store it in your char* .EDIT: (To the OP) This(^) still needs to be looked into...
//< MARKER (3) : Follow the same advice I gave for //< MARKER (1) .EDIT: (To the OP) This(^) has been fixed...
//< MARKER (4) : Check this link for information on basic File Input/Output operations. You should find an example where getline is used to read the file content. There's a (misplaced?) semicolon there as well, that probably shouldn't be, because right now, the while loop doesn't do anything useful.EDIT: (To the OP) This(^) still needs to be looked into...
//< MARKER (5) : You mentioned that your file is delimited using commas, so your getline statement might resemble the following: getline(in, strDataStore, ',') .EDIT: (To the OP) This(^) still needs to be looked into...
//< MARKER (6) :Note: This statement is causing an error because you are passing uninitialized pointers into the function.
This statement should probably be inside your
while loop. The function strcmp returns a value indicating whether or not the two strings are equal or not, and as you are checking for equality, it should return 0. Assuming, of course, that you have initialized your char* pointers to act as an array, and you want to check for equality, then the function strcmp will have to be put within an if(-else) construct, like the following: C++ Syntax (Toggle Plain Text)
if(strcmp(strDataStore, strSearchData) == 0) { // Code to do whatever you want to do }
Note: The above should be within your
while(getline(....)) loop. Check this link for more information on strcmp ...EDIT: (To the OP) This(^) still needs to be looked into...
As for your question on sorting through a list of first-names - The bubble-sort algorithm is easy to learn and apply, so I would recommend you apply this as the sorting technique. Note: You will have to load all the First-Names into a
char** (array of strings) to sort it using bubble sort.Here: A tutorial on the Bubble sort algorithm
Here: A program that shows how to implement the bubble sort algorithm for an array of strings.
Hope this helps!
EDIT: The post directly above this was posted as I was writing this... sorry about that... but the links might still be useful, as the new code also requires some tinkering...
Last edited by amrith92; May 24th, 2009 at 6:50 pm.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
•
•
Join Date: May 2009
Posts: 16
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
// ProjectDevelopment.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> // This libary allows standard input and output operations. #include <string> // strings are used as tempory storage - for the input to a text based file #include <vector> // at one point in my program i expermented with vectors for data storage using namespace std; // (or maybe std::getline;, std:: string;, std:: cout; , std:: cin; (namespace howecver is a short hand version to reduce code and possible errors. #include <fstream> // this allows filestreaming and allows the reading and writing of a file #include <iomanip> #include <cstring> int intEnterANumber; int intConfirmation; ifstream storage ( "Store.txt" ); ifstream in ( "Store.txt" ); string strConfirmation; string strFirstName; string strSurname; string strMiddleName; string strNickName; string strTelephoneNo; string strMobileNo; string strHouseNo; string strRoadStreet; string strVillageTown; string strPostcode; int StoreNameNumber(int intEnterANumber) { cout << "Please Enter the FirstName \n"; cin >> strFirstName; ofstream Store; Store.open("StoreNamePhone.txt",ios::app); Store << strFirstName; Store <<","; cout << "Please Enter the MobilePhone \n"; cin >> strMobileNo; Store << strMobileNo; Store <<","; Store <<" "; Store.close(); return 0; } int AddFirstName(int intEnterANumber) { cout << "Please Enter the FirstName \n"; cin >> strFirstName; ofstream File; File.open("Store.txt",ios::app); File << strFirstName; File <<" "; File.close(); return 0; } int AddSurname(int intEnterANumber) { cout << "Please Enter the Surname \n"; cin >> strSurname; ofstream File; File.open("Store.txt",ios::app); File << strSurname; File <<" "; File.close(); return 0; } int AddMiddleName(int intEnterANumber) { cout << "Please Enter the Middle Name\n"; cin >> strMiddleName; ofstream File; File.open("Store.txt",ios::app); File << strMiddleName; File <<" "; File.close(); return 0; } int AddTelephoneNo(int intEnterANumber) { cout <<"Please Enter a Telephone Number\n"; cin >> strTelephoneNo; ofstream File; File.open("Store.txt",ios::app); File << strTelephoneNo; File <<" "; File.close(); return 0; } int AddAddress(int intEnterANumber) // a function which is called by the switch statement. { cout <<"Please Enter the House No\n"; cin >> strHouseNo; ofstream File; File.open("Store.txt",ios::app); File << strHouseNo; File <<" "; File.close(); cout <<"Please Enter the road or street name\n"; // use street(ST) or (R) for road or street check validation. cin >> strRoadStreet; File.open("Store.txt",ios::app); File << strRoadStreet; File <<" "; File.close(); cout <<"Please Enter the Village or town name\n"; // use Village or (VI)or Town (T) for validation check on town or street cin >> strVillageTown; File.open("Store.txt",ios::app); File << strVillageTown; File <<" "; File.close(); cout <<"Please Enter the Postcode\n"; cin >> strPostcode; // Check length during validation. With country as well we could have allowed selection of zipcodes etc. File.open("Store.txt",ios::app); File << strPostcode; File <<"\n"; File.close(); return 0; } int main(int argc, char *argv[]) // could just be just int main { do { int intEnterANumber; cout << "Welcome To Your Phone \n\n"; cout << " (1) Enter a New Person's Details \n"; cout << " (2) Enter a New Person FirstName and telephone No. \n"; cout << " (3) Search Phone Book for all Names \n"; cout << " (4) Search Phone Book by Firstname n\n"; cout << " (5) Exit \n\n"; cout << "Please Select A Number From the Options Above\n "; cin >> intEnterANumber; switch (intEnterANumber) { case 1: AddFirstName(intEnterANumber); AddMiddleName(intEnterANumber); AddSurname(intEnterANumber); AddTelephoneNo(intEnterANumber); AddAddress(intEnterANumber); break; case 2: StoreNameNumber(intEnterANumber); break; case 3: if(!storage){ cout << "Cannot open file."; exit (1); } char str[255]; while(storage){ storage.getline(str, 255); cout << str << endl; } storage.close(); //} break; break; case 4: //bits of code not quite right char strSearchData[255]; cout << "Please enter a name for searching \n"; cin >> strSearchData,255 ; storage.open("Store.txt"); char strDataStore[255]; char strRestOfLine[255]; char match; while ( storage.getline( strDataStore, 255, ',' ) ) { storage.getline( strRestOfLine, 255, '\n' ); match = strcmp (strDataStore,strSearchData); if (match == 0) { cout << strDataStore << strRestOfLine <<endl; } else { cout <<"Data Not found"; } } storage.close(); break; case 5: exit(EXIT_SUCCESS); exit(1); } } while (intEnterANumber !=6 ); return 0; }
![]() |
Similar Threads
- I got stuck on loading the matrix from file (C++)
- Binary File IO (C#)
- Exporting multiple Strings to a text file? (Java)
- Edit and replace strings in a text file (C++)
- File handling: How to search and replace strings in file using user input (C)
- Read from a file then placing strings into a 2d array (C)
- Help with comparing user input to a text file! (C++)
- Read and writing strings from structures (C++)
Other Threads in the C++ Forum
- Previous Thread: Linked List Copy Constructor
- Next Thread: any idea on this error?
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






