problems using peek()

Dec 19, 2011 at 9:17pm
Im SOO stuck. Writing a program to read a txt file and output the contents according to the linelength. I'm having problems with reading the spaces and \n characters. If there is more than one ' ' then the program correctly counts and outputs it however if there is only 1 it fails to identify it and skips its output. There is also irregular output of \n characters. Can anyone help?

here is the 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
while (!ins.eof())
		{
			if (ins.peek() == '\n') // if newline character
				{
					cout << '\n';
					ins.ignore(1, '\n'); // move input forward
					n = 0;
					total = 0;
				}
			else if (ins.peek() == ' ')  // if space count spaces
				{
					 while (ins.peek() == ' ') // counts number of spaces and stores in n
						{
							n++;
							ins.ignore(1, ' '); // move input forward
						}

				}
			else // else we are looking at a word
				{

					string s;
					ins >> s; // input word
					if (n +	total + static_cast<int>(s.length()) > linelength) // if  spaces plus word plus total amount > line length
						{
							cout << '\n'; 					// got to new line
							cout << s; 						// output s
							total = s.length(); 			// reset total to length of s
							n = 0; 							// resets space counter
							total2 += total;
						}
					else     							// other wise
						{
							for (int p = 0; p < n; p++) // output spaces
							cout << '_';
							cout << s;     				// output the word
							total += n + static_cast<int>(s.length());   // increase total
							n = 0; // resets the space counter
							total2 += total;
						}
				}
		}


thanks.
Last edited on Dec 19, 2011 at 9:56pm
Dec 19, 2011 at 9:58pm
I am a bit confused about what you're trying to do. What do you mean by "output the contents according to the linelength"?

It might be better to process your file line by line, using std::getline() and std::string. That should make it easier to solve your problem.

An alternative approach would be to use an ostream_iterator, if you like that kind of thing.

Andy
Dec 19, 2011 at 10:08pm
Its an assignment. I have a preset txt file and have to design a program in which a user is able to enter a number for the linelength. I am going to pass that as the argument for this procedure. I'm not allowed to do it by storing the txt.file line by line... This is why i am trying to use the peek() functions but i may be going down the wrong path. It correctly prints spaces when there is more than 1 ie. "red__car" using underscores for clarity. However when there is just 1, the peek() function just returns the next char after the space and the program executes the final else. So when the input is "James Smith" the peek() function returns the ascii for S and thus the final else is executed but when the input is "James Smith" (2 spaces) the peek() function returns ascii for " " (32) so the else if is executed (this strangely correctly counts 2 spaces).

I'm trying to figure out whether this is to do with the input position after the input of a word.

after line 23 the future input should be the char directly after the word just inputted right?

thanks andy
Last edited on Dec 19, 2011 at 10:20pm
Dec 19, 2011 at 10:09pm
Why does it only work for more than 1? Cos it still counts the correct number when theres more than 1 not one less. So wierd.
Last edited on Dec 19, 2011 at 10:12pm
Dec 19, 2011 at 10:30pm
I'm trying to figure out whether this is to do with the input position after the input of a word.

Well, that's the behaviour of cin >>

It extracts up to the first whitespace char, but discards it (it has to have read it to have found the end of the word).

cin doesn't think you care about spaces. So the next character will be one after the first space, as you've found.

If your not keen on a line by line approach, what about a char by char one?
Last edited on Dec 19, 2011 at 10:41pm
Dec 19, 2011 at 10:36pm
Yeah but after its extracted the word would peek() perfrom on the whitespace after or not?

e.g. using underscores as spaces for clarity

using cin:

james_taylor - after one input operation the input pointer should be at

_taylor - thus peek() should return 32


this is not happening tho...

It works at the begginning before any input of words has occured but after that it misses one
Last edited on Dec 19, 2011 at 10:40pm
Dec 19, 2011 at 10:42pm
just realised u answered my question...

thanks
Last edited on Dec 19, 2011 at 10:42pm
Dec 19, 2011 at 10:53pm
do you know if the get() function e.g. ins.get will retrieve \n or " " ?
Dec 19, 2011 at 11:18pm
no worries done it.
Dec 21, 2011 at 7:58pm
Cool!

What was your final approach, then??
Topic archived. No new replies allowed.