|
-
Feb 11th, 2001, 03:49 PM
#1
Thread Starter
Registered User
Hello everyone, i don't understand whats wrong with this example....
Code:
#include <iostream.h>
void main(){
double a[4];
cout << "enter 4 real numbers: \n";
for (int i = 1; i <=4; i++) {
cout << i << ": ";
cin >> a[i-1];
}
cout << "Here they are in reverse order:\n";
for (i = 3; i >= 0; i--)
cout << "\ta[" << i << "] = " << a[i] << endl;
}
I get the following errors:
c:\my documents\c++programs\programming in c++\array's\printing array numbers.cpp: In function `int main(...)':
c:\my documents\c++programs\programming in c++\array's\printing array numbers.cpp:13: name lookup of `i' changed for new ANSI `for' scoping
c:\my documents\c++programs\programming in c++\array's\printing array numbers.cpp:7: using obsolete binding at `i'
c:\my documents\c++programs\programming in c++\array's\printing array numbers.cpp: In function `int main(...)':
c:\my documents\c++programs\programming in c++\array's\printing array numbers.cpp:16: redefinition of `int main(...)'
c:\my documents\c++programs\programming in c++\array's\printing array numbers.cpp:4: `int main(...)' previously defined here
c:\my documents\c++programs\programming in c++\array's\printing array numbers.cpp: In function `int main(...)':
c:\my documents\c++programs\programming in c++\array's\printing array numbers.cpp:25: name lookup of `i' changed for new ANSI `for' scoping
c:\my documents\c++programs\programming in c++\array's\printing array numbers.cpp:19: using obsolete binding at `i'
thanks for listenign
-
Feb 11th, 2001, 04:09 PM
#2
Monday Morning Lunatic
Under VC++ (just tested it) it's fine.
Using Borland C++ 5.5.1 it gives me an "undefined symbol 'i' in function main()" error.
Changing it to this worked on Borland:
Code:
#include <iostream.h>
void main(){
double a[4];
int i;
cout << "enter 4 real numbers: \n";
for (i = 1; i <= 4; i++) {
cout << i << ": ";
cin >> a[i-1];
}
cout << "Here they are in reverse order:\n";
for (i = 3; i >= 0; i--)
cout << "\ta[" << i << "] = " << a[i] << endl;
}
This is because under ANSI rules, variable declaration does not move out a for block, and must either be declared twice or once globally.
Turning off MS extensions in VC++ also produced this error.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 11th, 2001, 04:19 PM
#3
Thread Starter
Registered User
thanks !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|