LinXx
LinXx

Reputation: 1

C++ Read every second line from a text file and safe in vector

I am doing a small "translator" project. I have this text file which looks like this:

friend
amigo
mother
madre
telephone
teléfono

As you can see the first line is the English word and the following is the word to translate. I want to save every English word in a vector and every Spanish word into another vector. My problem is that I don't know exactly how to skip these lines.

Upvotes: 0

Views: 1069

Answers (3)

Maestro
Maestro

Reputation: 2552

The vector is not your best choice. Use std::map which is really good for a dictionary because elements in it are ordered and unique which is really good choice rather than using two vectors separating words from their definitions:

#include <map>
#include <iostream>
#include <string>

int main()
{

    std::map<std::string, std::string> content;
    std::ifstream in("Data.txt");
    std::string word, definition;

    while (std::getline(in, word) && std::getline(in, definition))
        content[word] = definition;

    for (const auto& p : content)
        std::cout << p.first << " : "
        << p.second << std::endl;
     std::cout << std::endl;

    // To search for a word in dictionary:
    std::string search = "mother";
    auto ret = content.find(search);
    if (ret != content.cend())
        std::cout << search << " : \n" <<
        ret->second << std::endl;
    else
        std::cout << search << " : was not found in dictionary!" << std::endl;


    std::cout << "\n";
}

Upvotes: 3

Ajay Dabas
Ajay Dabas

Reputation: 1444

My problem is that I don't know exactly how to skip these lines.

Here's an idea: assuming that you always have an English and a Spanish word(or line) occurring alternatively in the file, you can keep a boolean variable is_english to denote the language of the current word(or line). It's set to true initially, assuming that the first word(or line) is in English, and then set to false, true, false, etc. alternatively, each time you read a new word(or line). In every iteration, if is_english is true, you append the current word(or line) to English vector. Otherwise, append to Spanish vector. The code is given below for your reference.

Assuming you have a file like this:

friend
amigo
mother
madre
telephone
teléfono
#include <iostream>
#include <fstream>
#include <vector>

int main(){
    std::ifstream in("./input.txt");
    if(!in){
        std::cout<<"Error opening the file! Maybe the file does not exist!";
        return 0;
    }
    std::string one_line;
    std::vector<std::string> english, spanish;
    bool is_english = true; // Variable to denote current word (starts with english)
    while(in.eof()==0){ // Read until end of file
        getline(in,one_line);
        if(is_english) // If english
            english.push_back(one_line);
        else // Otherwise, spanish
            spanish.push_back(one_line);
        is_english = !is_english; // Flip boolean variable value
    }
    std::cout<<"English words:\n";
    for(const auto i:english)
        std::cout<<i<<std::endl;
    std::cout<<"\nSpanish words:\n";
    for(const auto i:spanish)
        std::cout<<i<<std::endl;
    return 0;
}

Output

English words:
friend
mother
telephone

Spanish words:
amigo
madre
teléfono

Upvotes: 0

Kyi Zet
Kyi Zet

Reputation: 59

I do not know what your intentions are or what you have tried. But I hope this helps.

#include <iostream>
#include <vector>
#include <string>
using namespace std; 

int main(){
    string array[10] = {"friend", "amigo", "mother", "madre", "telephone", "teléfono"};
    vector<string> englishWords;
    vector<string> spanishWords;

    for(int i = 0; i < 10; i+=2){
        englishWords.push_back(array[i]);
    }

    for(int i = 1; i < 10; i+=2){
        spanishWords.push_back(array[i]);
    }

    for(int i = 0; i < englishWords.size(); i++){
        cout << englishWords[i] << endl;
    }

    for(int i = 0; i < spanishWords.size(); i++){
        cout << spanishWords[i] << endl;
    }

 }

Upvotes: 0

Related Questions