Home > front end >  How to print the all inf inside a vector?
How to print the all inf inside a vector?

Time:12-23

(=
This is a search function to search for the Full name the rest of the info about the person.

The problem is it prints the full name but not the rest of the lines about the person. Could someone, please guide me? I do really appreciate your help!

This is the file (=

Tom Cruise 
Los Angelse Manchester Street 234 1223
[email protected]
0354221112
23 July
Mission Accomplished      <=== Line about this person ends here! 
Jordan Jordansson
Georga computerStreet 12 34567
[email protected]
032456789
20 January
My new book is coming soon   <=== Line about this person ends here! 
Justin Larsson
London Manchester street 12 
[email protected]
02438372521 
21 January
I love you papa     <=== Line about this person ends here! 

This is the main part of the code focused on the loops and reading from a file and saving in a vector.


std::vector<std::string> my_vec;

std::string search{};
std::string lines{};
bool found = false;
std::cout << "Enter Full name to search: ";
std::getline(std::cin, search);


while (std::getline(in_out_file, lines))
{
  my_vec.push_back(lines); 
      
}
  auto itr = find(my_vec.begin(), my_vec.end(), search );
   
   if(itr != my_vec.end()) 
   {  
    std::cout << "Match found " <<  search << std::endl;
       } else {
         std::cout << "Match not found "<< std::endl;
       }

This is the entire code

#include<iostream>
#include<vector>
#include<string>
#include <algorithm>
#include <iomanip>
#include<fstream>
#include <map>

int search_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{""};

 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::vector<std::string> my_vec;

std::string search{};
std::string lines{};


std::cout << "Enter Full name to search: ";
std::getline(std::cin, search);


while (std::getline(in_out_file, lines))
{
  my_vec.push_back(lines); 
  
}


  auto itr = find(my_vec.begin(), my_vec.end(), search );
   if(itr != my_vec.end()) 
   {  
    std::cout << "Match found " <<  search << std::endl;
       } else {
         std::cout << "Match not found "<< std::endl;
       }

in_out_file.close();
return 0;
}

CodePudding user response:

If every entry contains 6 lines. Then you can print all the lines starting from the line that you found like:

auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end()) 
{  
  std::cout << "Match found " << std::endl;
  // print the next 6 lines
  for(int remaining = 6;remaining > 0 && itr!=my_vec.end(); itr  ,remaining--) {
    std::cout << *itr << std::endl;
  }
} else {
      std::cout << "Match not found "<< std::endl;
}

CodePudding user response:

There are a few style problems with your code:

  1. No need to explicitly initialize strings, they will be empty by default (see here).
  2. Keep a consistent style. For example, either always start brackets in the same line as the function signature or in the next line.
  3. No need to close the file explicitly at the end of the function, this is done when the object goes out of scope (see (destructor) here).
  4. No need to include <map> and <iomanip> headers.
  5. Don't keep unused variables.
  6. Give suggestive names to your variables.
  7. Do not return error codes to the OS when the app is working as it should. Not finding a name is not an error, is it?

It seems your file has 6 entries per contact, so all you have to do is print 5 more lines. You do not need to store the lines in a vector, just parse and print them as you go. Here is an example:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>

void findContact(std::string fullName, std::string contactListPath) {

    std::ifstream inFile{contactListPath};
    if (!inFile) {
        std::cerr << "File could not be open!" << std::endl;
        return;
    }

    std::string line;
    while (std::getline(inFile, line)) {
        if (line == fullName) {
            std::cout << "Match found: \n" << 
                "\nFull name: " << fullName <<
                "\nAddress: " << (std::getline(inFile, line), line) <<
                "\nE-mail: " << (std::getline(inFile, line), line) <<
                "\nPhone: " << (std::getline(inFile, line), line) <<
                "\nBirthday: " << (std::getline(inFile, line), line) <<
                "\nNote: " << (std::getline(inFile, line), line) << std::endl;
            return;
        }
    }

    std::cout << "Match not found " << std::endl;
}

int main() {

    std::string fullName;
    std::string contactListPath;

    std::cout << "Enter full name to search: ";
    std::getline(std::cin, fullName);

    std::cout << "Enter path to contact list: ";
    std::getline(std::cin, contactListPath);

    findContact(fullName, contactListPath);

    return 0;
}
  • Related