Trouble with formatting

I can't seem to figure out what I am doing wrong here. I am supposed to format a format text so that fist letter is capitalized and all other are lower case. Also the test needs to keep its originale format so:

tHIs iS A sAmple
oF tExt tO be
FOrmaTEd.

should look like this:

This Is A Sample
Of Text To Be
Formatted.

could anyone point me in the right direction.

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

using namespace std;

bool at_endl(ifstream&);
void skip_blanks(ifstream&);
void reformat(string&);
void getfilename(string,string&);

int main()
{
    string word, filename;
    char ch;
    ifstream input;
    getfilename("input",filename);
    input.open(filename.c_str());
    input >> word;
    while (!input.eof())
        {
            reformat(word);
            cout << word;
            skip_blanks(input);
            if (at_endl(input))
                {
                    cout << endl;
                }
            else
                cout << " ";
            input >> word;
        }
    return 0;
}

void getfilename(string filetype, string& filename)
{
    cout << "Enter the name of " << filetype << " file\n";
    cin >> filename;
}

bool at_endl(ifstream& input)
{
    return (cin.peek() == '\n');
}

void skip_blanks(ifstream& input)
{
    char ch;
    while (cin.peek() == ' ')
        cin.get(ch);
}

void reformat(string& word)
{
    int wlength;
    wlength = word.length();
    word[0] = toupper(word[0]);
    for (int i=1; i<wlength; i++)
        word[i] = tolower(word[i]);
}

line 45, 51 and 52, you work with cin here, use input and everything works like you want it to
Last edited on
thank you, I thought the logic was right just a silly mistake like always. Thanks again!
Topic archived. No new replies allowed.