string stream help

how would you write a string stream from multiple txt files to change key words from each array? I'm sorry if my wording is bad i am very new to this and just trying to figure things out any help would be very appreciated thank you!!


bool WriteOriginals(string [], string [], string [], ofstream& out, string& results)
"stringstream" is a specific C++ feature, but I'm not sure that's what you're referring to.

Might help if you show an example inputs and output.
Last edited on
sorry. yes i meant stringstream ..

bool WriteOriginals(string sentnces[], string colors[], string phrases[], ofstream& out, string& results)

i have multiple files i have to read the sentence files to the screen and i need to change key words(colors) in that file to the matching color in the color file which will read out another phrase from the phrases file associated with the color from the color file..

i hope that makes any sense at all!!

im sorry i am so lost i dont really know how to explain what im doing.
What I'm asking for is an example of an input file, example of the color file, example of the phrases file, and example of the output produced once you do some operation on those files.

Otherwise, I'll just be wasting time trying to guess what the actual requirements are.

Here's an example of reading in a file, replacing some colors in it with phrases, and writing out the file again. Maybe it'll help you get some ideas.

colors.txt
red
purple

phrases.txt
sly, cunning
broken

input.txt
the red fox jumped over the purple fence


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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

vector<string> load_from_file(const string& filename)
{
    ifstream file(filename);
    vector<string> items;
    {
        string line;
        while (getline(file, line))
        {
            items.push_back(line);
        }
    }
    return items;
}

bool is_color(const string& word, const vector<string>& colors)
{
    for (const string& color : colors)
    {
        if (color == word)
            return true;
    }
    return false;
}

int main()
{
    vector<string> colors = load_from_file("colors.txt");
    vector<string> phrases = load_from_file("phrases.txt");
    
    ifstream input_file("input.txt");
    ofstream output_file("output.txt");
    
    size_t phrase_index = 0;
    string word;
    while (input_file >> word)
    {
        if (is_color(word, colors))
        {
            // replace color with phrase
            word = phrases[(phrase_index++) % phrases.size()];
        }
        
        output_file << word << " ";
    }
}


output.txt
the sly, cunning fox jumped over the broken fence

Last edited on
Alright, what you said there makes some sense Ganado. I'm working on this VERY SAME project with OP, so I'll add some more detail here. We were given these .txt files, Colors.txt and Sentences.txt where the USER inputs some words or whatever, and if one of the colors from the Colors.txt file is entered, it's replaced with the corresponding phrases in the Phrases.txt file.

1
2
3
4
5
6
7
red
blue
green
yellow
black
white
brown



1
2
3
4
5
6
7
expensive and elegant
gigantic yet gentle
beautiful and rich
sunny and warm
glittery with diamonds
massive and stupid
fast and agile


Are what the Colors.txt and Phrases.txt files contain respectively. We were asked to use arrays here instead of vectors, so the function that OP has in the first post are supposed to have arrays passed in. Yeah I'm just as lost as he is lol
Still not clear how Sentences.txt relates, but what you're saying sounds similar to my example, so I think it could still be helpful.

The fact that I used a vector instead of an array is really a minor detail. You could refactor it to use an array, e.g.
1
2
3
4
5
6
7
 string colors[1000];
string line:
int i = 0:
while (getline(file, line))
{
     colors[i++] = line:
}
If I interpret what H4X0R46 is saying correctly, then given the Colors.txt and Sentences.txt mentioned, this input:

When I was young, I lived in a red house and
we had a brown car and a yellow lab.


The output would be:
When I was young, I lived in a expensive and elegant house and
we had a fast and agile car and a sunny and warm lab.


I've underlined the important parts for emphasis.

Have I got that right?
Topic archived. No new replies allowed.