Sunday, September 24, 2017

Digest for comp.lang.c++@googlegroups.com - 5 updates in 2 topics

Paul N <gw7rib@aol.com>: Sep 24 02:26PM -0700

On Friday, September 22, 2017 at 2:12:13 AM UTC+1, Mwater07 wrote:
> for(int i=0; i<n;i++)
> head=new node (a[i],head);
> }
 
I think you have problems here. Firstly, what does "link" mean? Are you simply trying to set head to 0? Also, you don't seem to be supplying enough arguments to the "new node", you want to pass the index i, the value a[i] and head.
 
While that may get it working - you also said you were only going to store the non-zero values, so you need an "if"; you're throwing away any previous nodes in the list, which might be a bad move and it would certainly be usual to free them; the function doesn't "create" a list as such, it fills it up with the values from a vector so the name is a bit misleading; and you might want to use NULL instead of 0 for consistency with your other code.
 
Hope that helps.
Paul.
 
[Snip rest]
bintom <binoythomas1108@gmail.com>: Sep 24 07:19AM -0700

Hi!
 
I have the following short program to read a text file character-by-character using get().
 
int main()
{ char ch;
ifstream in("Text.txt");
 
while(in)
{ in.get(ch);
cout.put(ch);
}
 
in.close();
}
 
In the output, I am getting the very last character in the text file appearing twice. I have no clue why ...
 
Thanks for any help
bintom <binoythomas1108@gmail.com>: Sep 24 07:23AM -0700

I have the following code, which is printing the last character in the text file two times.
 
int main()
{ char ch;
ifstream in("Test.txt");

while(in)
{ in.get(ch);
cout.put(ch);
}
 
in.close();

getch();
}
 
Any help will be hugely appreciated.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 24 05:03PM +0200

On 9/24/2017 4:19 PM, bintom wrote:
 
> in.close(); }
 
> In the output, I am getting the very last character in the text file
> appearing twice. I have no clue why ...
 
Because you don't check the result of the `get` call.
 
Change the loop to
 
while( in.get( ch ) ) { cout.put( ch ); }
 
That's idiomatic and, because `get` returns a reference to the stream
which converts to `bool` via a call to `fail()` + negation, it's the
short version of
 
for( ;; )
{
char ch;
in.get( ch );
if( in.fail() )
{
break;
}
cout.put( ch );
}
 
But you don't want to write that.
 
 
Cheers & hth.,
 
- Alf
Barry Schwarz <schwarzb@dqel.com>: Sep 24 11:08AM -0700

On Sun, 24 Sep 2017 07:19:54 -0700 (PDT), bintom
 
> in.close();
>}
 
>In the output, I am getting the very last character in the text file appearing twice. I have no clue why ...
 
Alf has already given you the technical answer. Your confusion arises
because you made a false assumption about the state of in. Here is
one way to analyze the situation should you run into something similar
in the future. It falls under the general heading of "pretend you are
the computer".
 
Consider the sequence if the file contains two characters, '8' and
'9'.
You open the file. The ifstream object in now corresponds to that
file.
The while expression evaluates to true.
You read the character '8'. The operation completes normally.
You write the contents of ch ('8').
The while expression evaluates to true.
You read the character '9'. The operation completes normally.
You write the contents of ch ('9').
The while expression evaluates to true.
You attempt to read another character. There is none available.
The operation fails. Object ch is unchanged.
You write the contents of ch ('9').
Now the while expression evaluates to false and you close the file.
 
Reading the last character of a file and no further does not set "end
of file" status. That only happens when you attempt to read beyond
the last character.
 
--
Remove del for email
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: