Character I/O, and inputting spaces.

Pages: 123
@degausser

Skiming back through the thread, I spotted (this time!) that your post of Oct 19, 2012 at 10:07pm states:

"This is what should happen:

Enter a character followed by 5 spaces followed by a character:
84ad67b
You entered:
8     b


The program just ignores everything in between and outputs is as blank space."

This doesn't fit with the other examples, as far as I can see. Thay're no spaces to count.

Andy

Last edited on
Got it to work without arrays and just using the language we know in class.

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
void getChar(ofstream& out_stream);
void wait5Spaces(ofstream& out_stream);
void display(ifstream& in_stream);

int main()
{
    ofstream out_stream;
    out_stream.open("inFile.txt");

    cout << "Enter a character followed by 5 spaces, followed by a character: ";

    getChar(out_stream);
    wait5Spaces(out_stream);
    getChar(out_stream);

    out_stream.close();

    ifstream in_stream;
    in_stream.open("inFile.txt");

    display(in_stream);

    in_stream.close();
    cout << endl;
    return 0;
} 

void getChar(ofstream& out_stream)
{
    char cMyChar;
    cin >> noskipws >> cMyChar;
    out_stream << cMyChar;
}

void wait5Spaces(ofstream& out_stream)
{
    int count = 0;
    char cMyChar;
    out_stream.unsetf(ios_base::skipws);
    while (count < 5)
        {
        cin.get(cMyChar);
        if ( (isspace(cMyChar)) && (cMyChar != '\n') )
            {
            count++;
            out_stream << " ";
            }
        }
}

void display(ifstream& in_stream)
{
    char cNextChar;
    in_stream.unsetf(ios_base::skipws);
    while ( in_stream >> cNextChar )
        {
        cout << cNextChar;
        }

} 

Ah, but you used a "cache" file to store the results!

To tie down the requirements, I guess we should have asked what topics had been covered so far!

Andy

PS I would prob. use cin.get() throughout, as you do in wait5Spaces, rather than cin >>, to remove the need for all the skipws stuff.
@andywestken: what would you replace them with?

And what exactly do these two lines do?

1
2
out_stream.unsetf(ios_base::skipws);
in_stream.unsetf(ios_base::skipws);
From what I understand, The extraction operator >>, by default, skips white spaces. Those lines will change that setting and take white spaces into consideration. This allows the program to output and input white spaces as if they were characters.

Replacing the extraction operator with .get() and also using .put(), I think that those lines can be deleted anyway.
Last edited on
As Kealow has already said, the unsetf lines would need to be removed rather than replaced.
Last edited on
Topic archived. No new replies allowed.
Pages: 123