istream and while(cin >> i)

A

ax

to implement std::cout and file output seems simple but for file input
and cin i have some question for the "std::cin"

#include <iostream.h>

int main(void)
{int n, sum=0;
while(cin >> n) sum+=n;
cout << "somma = " << sum << endl;
return 0;
}

so if (cin >> n) is an object "istream" and it is not a pointer
how know "the while" when to stop the loop or to continue the loop?
i think something somethink of kind (cin >> n)!=(int)0

here seems that this little program has too many assembly language
translation. it seems it call a long function that has like argument
"cin" but i didn't find why all these lines

;
; while(cin >> n) sum+=n;
;
@2:
mov edx,dword ptr [ebp-4]
add dword ptr [ebp-8],edx
@3:
lea ecx,dword ptr [ebp-12]
push ecx
push offset @_STL@cin
call toolongcall
add esp,8
mov eax,dword ptr [ebp-12]
mov dword ptr [ebp-4],eax
mov edx,dword ptr [ebp-12]
mov dword ptr [ebp-16],edx
mov ecx,dword ptr [ebp-4]
cmp ecx,dword ptr [ebp-12]
je short @4
mov eax,dword ptr [ebp-16]
cmp eax,dword ptr [ebp-12]
je short @4
mov ebx,dword ptr [@_STL@cin]
mov esi,dword ptr [ebx+8]
or esi,4
cmp dword ptr [ebx+108],0
je short @6
mov eax,esi
jmp short @7
@6:
mov eax,esi
or eax,1
@7:
mov dword ptr [ebp-20],eax
mov edx,dword ptr [ebp-20]
mov dword ptr [ebx+8],edx
mov ecx,dword ptr [ebx+8]
and ecx,dword ptr [ebx+20]
je short @5
push ebx
call @@_STL@ios_base@_M_throw_failure$qv
pop ecx
@4:
@5:
mov eax,offset @_STL@cin
mov edi,dword ptr [eax]
test byte ptr [edi+8],5
je short @11
xor edi,edi
@11:
test edi,edi
jne short @2
Thank you
 
T

TB

ax skrev:
to implement std::cout and file output seems simple but for file input
and cin i have some question for the "std::cin"

#include <iostream.h>

int main(void)
{int n, sum=0;
while(cin >> n) sum+=n;
cout << "somma = " << sum << endl;
return 0;
}

so if (cin >> n) is an object "istream" and it is not a pointer
how know "the while" when to stop the loop or to continue the loop?
i think something somethink of kind (cin >> n)!=(int)0

basic_ios::eek:perator void *();

Returns a null pointer if fail(), otherwise non-null pointer to
signal "stream ok".
 
H

Howard

TB said:
ax skrev:

basic_ios::eek:perator void *();

Returns a null pointer if fail(), otherwise non-null pointer to
signal "stream ok".

Your answer is a little short on explanation, isn't it?

I think what you're trying to say is that there is an implicit conversion
made on the istream return value, using the operator void*() function, and
that, when the stream is in a fail state, that operator returns NULL.
Otherwise, it returns a non-NULL value. Correct?

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,824
Messages
2,569,755
Members
45,744
Latest member
PoppyRizzo

Latest Threads

Top