(=
I'm trying to build a simple menu program that adds data, searches for data, and deletes data depending on what the user wants to do.
I have used Struct here as someone here told me, and also someone else told me to use a vector to make it easier for myself. I did both ways but however, I don't know why it is not working at all!
It prints the first person's full name in the file but how can I make it so the code prints all the info as I write the person's full name to the console.
This is an example of how the file looks like.
Tom Cruise
Los Angelse Manchester Street 234 1223
[email protected]
0354221112
23 July
Mission Accomplished
........................................... <== Empty line here
Jordan Jordansson
Georga computerStreet 12 34567
[email protected]
032456789
20 January
My new book is coming soon
........................................... <== Empty line here
So on
..
...
...
...
...
This is my code for the searching function.
#include<iostream>
#include<vector>
#include<string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include<fstream>
#include <map>
struct searching_contact
{
std::string full_name{""};
std::string address{""};
std::string e_post{""};
std::string phone_num{""};
std::string birth_date{""};
std::string anything_else{""};
std::string new_line{""};
};
int search_contact() {
searching_contact info;
std::ifstream in_out_file{"out_in_file.txt"};
if(!in_out_file){
std::cerr << "File could not be open!" << std::endl;
return 1;
}
std::string search{};
std::string lines{};
std::vector<std::string> my_vec;
int line{};
bool found = false;
std::cout << "Enter Key to search: ";
std::getline(std::cin, search);
// std::getline(in_out_file , info.full_name);
// std::getline(in_out_file , info.address);
// std::getline(in_out_file , info.e_post);
// std::getline(in_out_file , info.phone_num);
// std::getline(in_out_file , info.birth_date);
// std::getline(in_out_file , info.anything_else);
while (std::getline(in_out_file, lines))
{
my_vec.push_back(lines);
}
for(int i = 0;i < my_vec.size(); i){
if(search == my_vec[i]){
std::cout << search << std::endl;
std::cout << "Match found" << std::endl;
break;
// std::cout << "\n\tThe fullname is : " << info.full_name;
// std::cout << "\n\tThe address is : " << info.address;
// std::cout << "\n\tThe E-post is : " << info.e_post;
// std::cout << "\n\tThe number is : " << info.phone_num;
// std::cout << "\n\tThe birthday is : " << info.birth_date;
// std::cout << "\n\tAnything else : " << info.anything_else;
} else {
std::cout << "Match not found "<< std::endl;
break;
}
}
in_out_file.close();
return 0;
}
CodePudding user response:
Hello here is a working example. The robustness of your program depends on how you check the content of the lines vs the user search pattern. Here I removed all space in search pattern an line for the matching.
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <map>
using namespace std;
std::string embedded_test_file = R"(Tom Cruise
Los Angelse Manchester Street 234 1223
[email protected]
0354221112
23 July
Mission Accomplished
Jordan Jordansson
Georga computerStreet 12 34567
[email protected]
032456789
20 January
My new book is coming soon)";
struct searching_contact
{
std::string full_name{""};
std::string address{""};
std::string e_post{""};
std::string phone_num{""};
std::string birth_date{""};
std::string anything_else{""};
std::string new_line{""};
};
string remove_white_space(const string &var)
{
string res;
for (const auto &c:var)
{
if (c != ' ') {res.push_back(c);}
}
return res;
}
int search_contact()
{
searching_contact info;
std::stringstream in_out_file{embedded_test_file};
std::string search{};
std::vector<std::string> lines;
bool found = false;
std::cout << "Enter Key to search: ";
std::getline(std::cin, search);
std::string line;
while (std::getline(in_out_file, line))
{
lines.push_back(line);
}
auto search_no_space = remove_white_space(search);
for (int i = 0; i < lines.size(); i)
{
cout << "searching: "<< search <<" in <" <<lines[i] <<">" << endl;
auto line_no_space = remove_white_space(lines[i]);
if (search_no_space.compare(line_no_space) == 0)
{
std::cout << "Match found" << std::endl;
info.full_name = lines[i];
i ;
info.address = lines[i];
i ;
info.e_post = lines[i];
i ;
info.phone_num = lines[i];
i ;
info.birth_date = lines[i];
i ;
info.anything_else = lines[i];
i ;
info.new_line = lines[i];
i ;
found = true;
break;
}
}
return found == true ? 0 : -1;
}
int main(void)
{
return search_contact();
}