Calling all programmers

  1. #1
    Cache is offline Super Moderator

    Calling all programmers

    I've been learning a bit of C++ and decided to make a simple console app. The only thing is I dont know how to make it actually do what I want lol.

    What it does (menu options):
    Takes names and phone numbers, storing them in a text file.
    Prints the whole text file back to the screen.
    Exits the program.

    What I want it to do:
    Take names and phone numbers then store them in a text file.
    Print a single name/number after asking the user who's number they would like.
    Exit the program.

    So the question is, how do I search a text file (.txt) for a specific string (in this case a name) then print that line containing the sting back to the screen?


    Here is the code I have, it may have been better to use switch instead of all the "if else" statements but im not really bothered about all that right now:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    int main(int argc, char *argv[]){
    	
    	while (1) {
    	system("CLS");
    	string answer;
    	cout << "Bog Standard Phone Number Thingy.\n" << endl;
    	cout << "Please choose from the folowing options..." << endl;
    	cout << "1: " << "Add New Number To List" << endl;
    	cout << "2: " << "View Existing Numbers" << endl;
    	cout << "3: " << "Exit Program" << endl;
    	cout << "\n" << ":";
    	cin >> answer;
    	
    	if (answer == "1"){
    			   string name;
    			   string number;
    			   
    			 cout << "\n" << "Please input name..." << endl;
    			   cout << ":";
    			   cin >> (name);
    			 cout << "\n" << "Please input number..." << endl;
    			   cout << ":";
    			   cin >> (number);
    			   
    			   string namenumber;
    			   namenumber += "\n" + name + " " + number + "\n";
    			   
    			   vector<string> addnamenumber;
    			   string newnamenumber;
    			   
    			   ifstream prenamenumber ("namenumber.txt");
    			   while (getline(prenamenumber, newnamenumber)) {
    					 addnamenumber.push_back(newnamenumber + "\n");
    					 } 
    			   addnamenumber.push_back(namenumber);
    			   prenamenumber.close();
    			   
    			   ofstream namenumberstream ("namenumber.txt");
    			   if (namenumberstream.is_open()) {
    											 for (int i=0; i < addnamenumber.size(); i++)
    											 namenumberstream << addnamenumber[i];
    											 namenumberstream.close();
    											 cout << "\n" << "New Entry Successfully Added" << endl;
    											 system("PAUSE");
    											 } else if (! namenumberstream.is_open()) {
    												 cout << "\n\nError opening file" << endl;
    												 exit(1);
    												 }
    	} else if (answer == "2") {
    		   ifstream numberfile ("namenumber.txt");
    		   string line;
    		   while (getline(numberfile, line)) {
    				 cout << line << "\n";
    				 }
    		   cout << endl;
    		   system("PAUSE");
    		   } else if (answer == "3") {
    				   break;
    				   } else {
    						 cout << "\n" << "ERROR: " << answer << " is not an option." << endl;
    						 cout << "please select 1, 2, or 3 ";
    						 system("PAUSE");
    						 }
    	} 
    			   
    	return EXIT_SUCCESS;
    }
    OS: WinXP SP2
    Compiler: Dev-C++


  2. #2
    imported_Cameron is offline Junior Member
    lookup the function strstr()

    Code:
     char *  strstr ( const char * string1, const char * string2 );
    http://www.cplusplus.com/ref/cstring/strstr.html

  3. #3
    Cache is offline Super Moderator
    Thanks Cameron, I'll have to get my thinking cap on now lol. Oh, can you tell me if this strstr ignores white spaces, you see the .txt file output from the program (the file which will then become the input to search) looks like this:

    name number

    name number

    name number

    I will need this strstr to first find "name" then store the whole line "name" was found in order to print back both "name number".

    EDIT: The quote bellow is from the link you gave:

    Find substring.
    Scans string1 for the first occurrence of string2.
    The search does not include terminating null-characters.
    I guess this measn strstr does not ingore white spaces, right?

  4. #4
    imported_Cameron is offline Junior Member
    strstr should ignore whitespace, as what it does is goes throw a string (e.g. the file), and when it comes upon a match it returns the match or true depending on how its used I believe.

  5. #5
    Cache is offline Super Moderator
    Any idea's on how I could then make it aslo store "number" since it "number" will not be used as a search criteria. i.e. search for "name" if true store "name number" rather tham just "name".

  6. #6
    imported_Cameron is offline Junior Member
    an array would be the most logical way, however arrays in C++ are a *****

  7. #7
    Cache is offline Super Moderator
    Ok, thanks for your help Cameron, Ill post back when (or if) I get my head around this.

  8. #8
    imported_Cameron is offline Junior Member
    hehe.

    You may want to try a vB script instead (you can do console apps with it )

  9. #9
    Cache is offline Super Moderator
    Quote Originally Posted by Cameron
    hehe.

    You may want to try a vB script instead (you can do console apps with it )
    Im happy with C++ at the minute, I actually like it.

    Anywho, I have solved my problem now, using "find".
    Here is the finished code:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    int main(int argc, char *argv[]){
    	
    	while (1) {
    	system("CLS");
    	string answer;
    	cout << "Bog Standard Phone Number Thingy.\n" << endl;
    	cout << "Please choose from the folowing options..." << endl;
    	cout << "1: " << "Add New Number To List" << endl;
    	cout << "2: " << "View Existing Number" << endl;
    	cout << "3: " << "Exit Program" << endl;
    	cout << "\n" << ":";
    	cin >> answer;
    	
    	if (answer == "1"){
    			   string name;
    			   string number;
    			   
    			 cout << "\n" << "Please input name..." << endl;
    			   cout << ":";
    			   cin >> (name);
    			 cout << "\n" << "Please input number..." << endl;
    			   cout << ":";
    			   cin >> (number);
    			   
    			   string namenumber;
    			   namenumber += "\n" + name + " " + number + "\n";
    			   
    			   vector<string> addnamenumber;
    			   string newnamenumber;
    			   
    			   ifstream prenamenumber ("namenumber.txt");
    			   while (getline(prenamenumber, newnamenumber)) {
    					 addnamenumber.push_back(newnamenumber + "\n");
    					 } 
    			   addnamenumber.push_back(namenumber);
    			   prenamenumber.close();
    			   
    			   ofstream namenumberstream ("namenumber.txt");
    			   if (namenumberstream.is_open()) {
    											 for (int i=0; i < addnamenumber.size(); i++)
    											 namenumberstream << addnamenumber[i];
    											 namenumberstream.close();
    											 cout << "\n" << "New Entry Successfully Added" << endl;
    											 system("PAUSE");
    											 } else if (! namenumberstream.is_open()) {
    												 cout << "\n\nError opening file" << endl;
    												 exit(1);
    												 }
    	} else if (answer == "2") {
    		   string findname;
    		   cout << "Please input a name" << endl;
    		   cout << ":";
    		   cin >> (findname);
    		   ifstream numberfile ("namenumber.txt");
    		   string line;
    		   while (getline(numberfile, line)) {
    				 string::size_type position = line.find(findname);
    				 if (position != string::npos) {
    					 cout << "\n" << line << endl;
    				 } else if (position == string::npos) {
    					 cout << "\n" << "Name Not Found" << endl;
    					 break;
    					    }
    				 }
    		   cout << endl;
    		   system("PAUSE");
    		   } else if (answer == "3") {
    				   break;
    				   } else {
    						 cout << "\n" << "ERROR: " << answer << " is not an option." << endl;
    						 cout << "please select 1, 2, or 3 ";
    						 system("PAUSE");
    						 }
    	} 
    			   
    	return EXIT_SUCCESS;
    }
    This is the new/edited section:
    Code:
    else if (answer == "2") {
    		   string findname;
    		   cout << "Please input a name" << endl;
    		   cout << ":";
    		   cin >> (findname);
    		   ifstream numberfile ("namenumber.txt");
    		   string line;
    		   while (getline(numberfile, line)) {
    				 string::size_type position = line.find(findname);
    				 if (position != string::npos) {
    					 cout << "\n" << line << endl;
    				 } else if (position == string::npos) {
    					 cout << "\n" << "Name Not Found" << endl;
    					 break;
    					    }
    				 }

  10. #10
    imported_jazzman is offline Elite Member
    Quote Originally Posted by Cache
    Im happy with C++ at the minute, I actually like it.

    Anywho, I have solved my problem now, using "find".
    Here is the finished code:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
     
    using namespace std;
     
    int main(int argc, char *argv[]){
     
    	while (1) {
    	system("CLS");
    	string answer;
    	cout << "Bog Standard Phone Number Thingy.\n" << endl;
    	cout << "Please choose from the folowing options..." << endl;
    	cout << "1: " << "Add New Number To List" << endl;
    	cout << "2: " << "View Existing Number" << endl;
    	cout << "3: " << "Exit Program" << endl;
    	cout << "\n" << ":";
    	cin >> answer;
     
    	if (answer == "1"){
    			 string name;
    			 string number;
     
    			 cout << "\n" << "Please input name..." << endl;
    			 cout << ":";
    			 cin >> (name);
    			 cout << "\n" << "Please input number..." << endl;
    			 cout << ":";
    			 cin >> (number);
     
    			 string namenumber;
    			 namenumber += "\n" + name + " " + number + "\n";
     
    			 vector<string> addnamenumber;
    			 string newnamenumber;
     
    			 ifstream prenamenumber ("namenumber.txt");
    			 while (getline(prenamenumber, newnamenumber)) {
    					 addnamenumber.push_back(newnamenumber + "\n");
    					 } 
    			 addnamenumber.push_back(namenumber);
    			 prenamenumber.close();
     
    			 ofstream namenumberstream ("namenumber.txt");
    			 if (namenumberstream.is_open()) {
    											 for (int i=0; i < addnamenumber.size(); i++)
    											 namenumberstream << addnamenumber[i];
    											 namenumberstream.close();
    											 cout << "\n" << "New Entry Successfully Added" << endl;
    											 system("PAUSE");
    											 } else if (! namenumberstream.is_open()) {
    												 cout << "\n\nError opening file" << endl;
    												 exit(1);
    												 }
    	} else if (answer == "2") {
    		 string findname;
    		 cout << "Please input a name" << endl;
    		 cout << ":";
    		 cin >> (findname);
    		 ifstream numberfile ("namenumber.txt");
    		 string line;
    		 while (getline(numberfile, line)) {
    				 string::size_type position = line.find(findname);
    				 if (position != string::npos) {
    					 cout << "\n" << line << endl;
    				 } else if (position == string::npos) {
    					 cout << "\n" << "Name Not Found" << endl;
    					 break;
    					 }
    				 }
    		 cout << endl;
    		 system("PAUSE");
    		 } else if (answer == "3") {
    				 break;
    				 } else {
    						 cout << "\n" << "ERROR: " << answer << " is not an option." << endl;
    						 cout << "please select 1, 2, or 3 ";
    						 system("PAUSE");
    						 }
    	} 
     
    	return EXIT_SUCCESS;
    }
    This is the new/edited section:
    Code:
    else if (answer == "2") {
    		 string findname;
    		 cout << "Please input a name" << endl;
    		 cout << ":";
    		 cin >> (findname);
    		 ifstream numberfile ("namenumber.txt");
    		 string line;
    		 while (getline(numberfile, line)) {
    				 string::size_type position = line.find(findname);
    				 if (position != string::npos) {
    					 cout << "\n" << line << endl;
    				 } else if (position == string::npos) {
    					 cout << "\n" << "Name Not Found" << endl;
    					 break;
    					 }
    				 }

    nice little code :P

+ Reply to Thread
Page 1 of 2 1 2 LastLast