Tell Me Why It Happens!!!!!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 5
Reputation: prathys is an unknown quantity at this point 
Solved Threads: 0
prathys prathys is offline Offline
Newbie Poster

Tell Me Why It Happens!!!!!!!

 
0
  #1
Oct 20th, 2004
hello guys,

I came across a strange thing when doing program in c++ in visualc++ environment.just run this code and after entering the array elements press up arrow and down arrow.

Here's the code

#include<iostream.h>

void main()
{
int arr[5];
cout<<"Enter the array elements : "<<endl;
for(int i=0;i<5;i++)
cin>>arr[i];
cout<<"\n\n";
for(i=0;i<5;i++)
{
cout<<"The element no."<<i<<" is "; \\PRESS THE DIRECTION KEYS NOW
cin>>arr[i];
}
cout<<"\n\n";
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Tell Me Why It Happens!!!!!!!

 
0
  #2
Oct 20th, 2004
Originally Posted by prathys
I came across a strange thing when doing program in c++ in visualc++ environment.just run this code and after entering the array elements press up arrow and down arrow.
Would you mind telling us what this "strange thing" was?

Or might you be marvelling at the command shell in which your code is running? (F7 is really cool, too!)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Tell Me Why It Happens!!!!!!!

 
0
  #3
Oct 20th, 2004
just run this code and after entering the array elements press up arrow and down arrow
I did so but nothing unusual happened.

But ur code just reminded me of a question that i always wanted to ask,
  1. for(int i=0;i<5;i++)
  2. cin>>arr[i];
As u can c u declared the int i within the for loop, so this makes it local to the block of this for loop and should not be used outside this for loop. So the following code should produce some compile time error saying undeclared variable i.
  1. for(i=0;i<5;i++)
  2. {
  3. cout<<"The element no."<<i<<" is ";
  4. cin>>arr[i];
  5. }
That is exactly what happens with the latest borland 5.5 compiler. However, my Visual C++ doesnt produce any error and the program runs fine. Why is this so, why is this two compiler behaving differently. Has it not been standarized that i's scope is limited within the block of "for" loop?
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Tell Me Why It Happens!!!!!!!

 
0
  #4
Oct 20th, 2004
Originally Posted by Asif_NSU
However, my Visual C++ doesnt produce any error and the program runs fine. Why is this so, why is this two compiler behaving differently. Has it not been standarized that i's scope is limited within the block of "for" loop?
MSVC6 just got it wrong.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 5
Reputation: prathys is an unknown quantity at this point 
Solved Threads: 0
prathys prathys is offline Offline
Newbie Poster

Re: Tell Me Why It Happens!!!!!!!

 
0
  #5
Oct 22nd, 2004
Originally Posted by Dave Sinkula
Would you mind telling us what this "strange thing" was?

Or might you be marvelling at the command shell in which your code is running? (F7 is really cool, too!)

i am running my code in msvc++ 6.0

ya.the thing i wondered was.. it happens even after giving ignore() and clear() commnds.So it doe not matters with the buffer.So tell me how the previous input stays in that keys particularly?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Tell Me Why It Happens!!!!!!!

 
0
  #6
Oct 22nd, 2004
Again, are you investigating your program or the shell in which it is running?

I am guessing that you are in Windows. I think you are confused by Doskey.

[edit]Go to "Command Prompt" and enter "help doskey". Is this the same behavior you see?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Tell Me Why It Happens!!!!!!!

 
0
  #7
Oct 22nd, 2004
MSVC6 just got it wrong.
Strange, but i m using MS Visual C++ 6.0, too.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Tell Me Why It Happens!!!!!!!

 
0
  #8
Oct 22nd, 2004
Originally Posted by Asif_NSU
Strange, but i m using MS Visual C++ 6.0, too.
I am at a loss as to whether you are asking a question in regard to the "strange" behavior, or if you are trying to ask a question about the fact the MSVC6 messed up the scoping rules, or whether there is any question in that at all.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Tell Me Why It Happens!!!!!!!

 
0
  #9
Oct 22nd, 2004
To dave:
I was wondering why my MS visual c++ 6.0 is not pointing that out (the thing with i's scopes) as an error, but ur MSVC6 is. My friend's MSVC6 doesnt produce an error either. Since i do not expect u to know what is wrong with my particular compiler without seeing it for urself, i said "Strange, but i m using MS Visual C++ 6.0, too." Got it big bro?
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Tell Me Why It Happens!!!!!!!

 
0
  #10
Oct 22nd, 2004
Originally Posted by Asif_NSU
To dave:
I was wondering why my MS visual c++ 6.0 is not pointing that out (the thing with i's scopes) as an error, but ur MSVC6 is. My friend's MSVC6 doesnt produce an error either.
My MSVC6 isn't. My Borland 5.5 is. And I happen to be aware of the issue with MSVC6. They got it wrong (among other things). Borland 5.5 didn't.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC