Hey Folks. Just a quick question

This is total newb stuff so be gentle.

So I am supposed to use a "water pump" method of reading a text file. This parses character by character and dumps that into variable. I need that to be a string so I do the conversion. Then I need to count the number of characters to use for a different loop later on. I am having some issues related to the return of the string.

So within this requirement I am able to iterate through the file and gather a string. I am even able to do the string scrub and do a character count...However even though I defined my string outside of the while loop scope, when I test for the output outside of the loop, I get no value.

I really need this string value outside of this loop so I can maniplate the string as needed, but I HAVE to use cin.get and this while (cin.eof()) to go through each character. I can't change that part. I am going to sit here and fiddle with it anyway, but I have really been grinding on this and would appreciate any kind of insight!

Thanks dudes!


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
 #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main(int  argc, char *argv[], char **env)
{
    int i = 0;
    int n = 0;
    char str[n];
    int length = 0;
    string s = "";

    while ( ! cin.eof() )
    {
              // int i = 0;
               str[n] = cin.get();
              // string s(str, 1);
              s = str[n];

                if (!isalpha(str[n]))
                {

                   s.erase(0, 1);

                }
                else
                {
                    // cout << s; //COURRECT OUT
                     i++;
                }

cout << s; //CORRECT OUT

    }

cout << s;  // THIS GIVES NO VALUE. HOW DO I GET THE S STRING OUT OF THAT WHILE LOOP


return 0;

}
Last edited on
Just to say, you create an array with length zero over here:
1
2
int n = 0;
char str[n]

For your program, why not do something like this:
1
2
3
4
5
6
string str="";
while (!std::cin.eof())
{
char s = std::cin.get();
str+=s;
}

This basically reads a character until the end of the file, and appends that character to your string.
Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last character of the file, eof is still false. Your attempt to read past the last character sets eof, but you're not checking it there (line 4). You proceed as if you had read a good character. This will result in reading an extra (bad) character. The correct way to deal with this is to put the >> (or getl) operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }
Last edited on
Man...Thanks so much goldenchicken. This really helped me and totally worked. I wish I could have seen the solution myself. It is so simple.. I was doing crazy stuff to get this to work...My preprocessor was looking shameful...using fstream...all manner of ridiculous...and here is the simple solution. hah.
after you read the last character of the file, eof is still false

Sorry, @AbstractAnon, but perhaps I'm not understanding it the right way. Did you mean 'true'?
@Enoizat - No, I meant exactly what I posted.

When you read the last character of the file, the eof bit remains false. It is only when you attempt to read past the the last character that the eof bit becomes true.

Looking at goldenchicken's example, let's assume line 4 reads the last character of the file. The eof bit remains false. The test for !eof() at line 2 allows the loop to continue with another iteration. Line 4 attempts to read another character. Now, the eof bit becomes true, but the example does check for that. So what is now in s? Undefined. Line 5, undefined character now gets appended to the string.

Thanks a lot, AbstractionAnon!
Topic archived. No new replies allowed.