How to separate words into different vectors

Ok so im trying to split words up into two different vectors, the words are on the same line separated by a space, and i just cant seem to figure it out. im loading the words in the LoadWords function near the top

My list contains

tomodachi friend
denwa telephone
denchi battery
neko cat


Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "Prototypes.h"
#include "Vars_Struct.h"

using namespace std;

Vars::Vars(string FILENAME, string TEMPJWORDSTORAGE, string TEMPEWORDSTORAGE, vector<string> V_JAPANESEWORDS, vector<string> V_ENGLISHWORDS): fileName(FILENAME),
                                                                                                                    tempJWordStorage(TEMPJWORDSTORAGE),
                                                                                                                    tempEWordStorage(TEMPEWORDSTORAGE),
                                                                                                                    v_JapaneseWords(V_JAPANESEWORDS),
                                                                                                                    v_EnglishWords(V_ENGLISHWORDS)
{

}

void LoadWords(Vars &vars)
{
    ifstream LoadFile;

    LoadFile.open(vars.fileName.c_str());

    while(!LoadFile.eof())
    {
        getline(LoadFile, vars.tempJWordStorage);

        vars.tempJWordStorage.find_first_of(" ");

        if(vars.tempJWordStorage == " ")
        {
            vars.v_JapaneseWords.push_back(vars.tempJWordStorage);
        }
        if(vars.tempJWordStorage != " ")
        {
            vars.v_EnglishWords.push_back(vars.tempJWordStorage);
        }
    }

    LoadFile.close();
}

int main()
{
    vector<string> tempJwords; //Japanese Words
    vector<string> tempEwords; //English words

    tempJwords.push_back("TempJword");
    tempEwords.push_back("TempEword");

    Vars vars("somefile", "JWords", "EWords", tempJwords, tempEwords);

    vars.v_JapaneseWords.pop_back();
    vars.v_EnglishWords.pop_back();

    MainProgram(vars);

    return 0;
}

void MainProgram(Vars &vars)
{
    cout << "Enter name of the file" << endl;
    getline(cin, vars.fileName);

    LoadWords(vars);

    for(int i = 0; i < vars.v_JapaneseWords.size(); i++)
    {
        cout << "DEBUG JAPANESE WORDS: " << vars.v_JapaneseWords[i] << endl;

        for(int j = 0; j < vars.v_EnglishWords.size(); j++)
        {
            cout << "DEBUG ENGLISH WORDS: " << vars.v_EnglishWords[j] << endl;
        }
    }
}



Prototypes.h

1
2
3
4
5
6
7
8
9
#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED

#include "Vars_Struct.h"

void MainProgram(Vars &vars);
void LoadWords(Vars &vars);

#endif // PROTOTYPES_H_INCLUDED 


Struct_Vars.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef VARS_STRUCT_H_INCLUDED
#define VARS_STRUCT_H_INCLUDED

#include <string>
#include <vector>

struct Vars
{
    Vars(std::string FILENAME, std::string TEMPJWORDSTORAGE, std::string TEMPEWORDSTORAGE, std::vector<std::string> V_JAPANESEWORDS, std::vector<std::string> V_ENGLISHWORDS);

    std::string tempJWordStorage;
    std::string tempEWordStorage;
    std::string fileName;
    std::vector<std::string> v_JapaneseWords;
    std::vector<std::string> v_EnglishWords;
};


#endif // VARS_STRUCT_H_INCLUDED 
Last edited on
bump
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
	vector<string> jwords, ewords;
	string j, e;
	while(cin >> j && cin >> e)
	{
		jwords.push_back(j);
		ewords.push_back(e);
	}

	for(int e = 0, j = 0; e < ewords.size() && j < jwords.size(); ++e, ++j)
	{
		cout << ewords[e] << ' ' << jwords[j] << endl;
	}
}

http://ideone.com/XkugCP

Also, I don't see a reason to pass temp strings to the var ctor.
And see comments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main()
{
    vector<string> tempJwords; //Japanese Words
    vector<string> tempEwords; //English words

    tempJwords.push_back("TempJword");//why do you do this
    tempEwords.push_back("TempEword");//and this?

    Vars vars("somefile", "JWords", "EWords", tempJwords, tempEwords);

    vars.v_JapaneseWords.pop_back();//and then this
    vars.v_EnglishWords.pop_back();//and this?

    MainProgram(vars);

    return 0;
}

void MainProgram(Vars &vars)
{
    cout << "Enter name of the file" << endl;
    getline(cin, vars.fileName);

    LoadWords(vars);

    //these loops shouldn't be nested
    for(int i = 0; i < vars.v_JapaneseWords.size(); i++)
    {
        cout << "DEBUG JAPANESE WORDS: " << vars.v_JapaneseWords[i] << endl;

        for(int j = 0; j < vars.v_EnglishWords.size(); j++)
        {
            cout << "DEBUG ENGLISH WORDS: " << vars.v_EnglishWords[j] << endl;
        }
    }
}
Last edited on
tempJwords.push_back("TempJword");//why do you do this
tempEwords.push_back("TempEword");//and this?


I do that because i thought you need to initialize the vector or else you get weird symbols, is this not true? I dont like doing that but i thought thats what i was supposed to do, as for lines 11-12 im removing the temp words so they dont appear in the output, what other way can i do this?
also im getting words from a file so i need to know how to break up the string from 2 words to one, the program needs to know where the space is and put the first word into the first vector and the other word into the other vector, im confused on that.
also im getting words from a file so i need to know how to break up the string from 2 words to one

the >> operator uses the space character as a delimiter so it will break the words apart automatically. Naraku's code will work for a file you just need to change the stream. So cin should be someFileStream.

ok awesome i got it working, now what about whjat he said about the temp vectors in main? im not sure how to do it any other way, someone showed me that, and thats what i have been doing.
Topic archived. No new replies allowed.