// 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; }
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.
# ) was asked too much?
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.)
while ( in.getline( strFirstName, 50, ' ' ) ) { in.getline( strRestOfLine, 100, '\n' ); //compare the first name, do stuff }
// 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; }
case 4: , with added //<MARKER (x) comments to indicate the lines that are wrong and/or unnecessary...
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* .//< 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* .//< MARKER (3) : Follow the same advice I gave for //< MARKER (1) .//< 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.//< MARKER (5) : You mentioned that your file is delimited using commas, so your getline statement might resemble the following: getline(in, strDataStore, ',') .//< MARKER (6) :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:
if(strcmp(strDataStore, strSearchData) == 0) { // Code to do whatever you want to do }
while(getline(....)) loop. Check this link for more information on strcmp ...char** (array of strings) to sort it using bubble sort.
// 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; }
| DaniWeb Message | |
| Cancel Changes | |