[help] change all 4-letter string to 'love'

So the assignment is changing all 4-letter word to 'love', for example if my input is

"Ice cube cold"

It'll give output : "Ice love love"

The thing is, I've done until that part until the assignment told me to change word that start with capital letter as 'Love'. For example if my input is

"Cold hot Cube"

It'll give output : "Love hot Love"

I've been doing the assignment and stuck on 2nd part. This is my current code.

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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>


using namespace std;

int main()
{
    ifstream input;
    ofstream output;
    stringstream check_upper;

    output.open("4letters.txt");
    char next;
    cin.get(next);
    output << static_cast<char>(toupper(next));
    do
    {
        cin.get(next);
        output << next;
    } while (next != '\n');
    output.close();
    input.open("4letters.txt");
    string love;
    while (input >> love)
    {

        if (love.length() == 4)
        {
                char letter[4];
                check_upper << love;
                check_upper >> letter;
                if (isupper(letter[0]))
                {
                    love = "Love";
                    cout << love << " ";
                }
                else
                {
                    love = "love";
                    cout << love << " ";
                }


        }
        else
        {
            cout << love << " ";
        }
    }
    return 0;
}


I use sstream to check the capital letter of a string, but it only worked for first ecounter, for example the input is "Cold hot cube", the output will become "Love hot Love" and not "Love hot love"
Why such complexity with check_upper?

1
2
3
4
5
6
7
8
9
10
11
while (input >> love)
{   
    if (love.length() == 4)
    {   
        if(isupper(love[0]))
            love = "Love";
        else
            love = "love";
    }   
    cout << love << ' ';
}   
Thanks! It works! I never thought that string format can be checked per-letter
Topic archived. No new replies allowed.