This code has errors

#include <iostream> 
void main()
{
int var [10];
for (I=0; I<5; I++)
     cout << var(i);

}

Now what I have come up with is this to correct the errors that I see.

#include <iostream> 
using namespace std;
void main()
{
int var [10];
for (i=0; i<5; i++)
 {
    cout << var(i)<<endl;
}
}

I think I fixed all the mistakes but can't get my compiler to run at the moment. I was wondering if there is anything else that I miss correcting the first program. Everything looks right to me but my gut says differently.

please let me know if I have another mistake or if I found them all.

I'm a beginner with C++ but I'll attempt to help.

Line 3: Your main() function is void. The Main() function must always return an int. So change void to int.
Line 5: I don't believe you'll need this. Replace with a declaration of the variable "i"
Line 6: The variable "i" is never declared.
Line 8: You're trying to call "var" and providing the argument "i". Var is not a function and cannot be called. Replace var(i) with just i.

Finally at the end of the function add return 0;
This tells the operating system that everything went ok.

Edited 6 Years Ago by WildBamaBoy: Sounded stupid :P

You can have a void and still return a value I thought.
Oh forgot to put the var (i) as var


#include <iostream>
using namespace std;
void main()
{
int var [10];
for (i=0; i<5; i++)
{
cout << var[i]<<endl;
}
}

No. Void states that the function will not return anything. Int says that it will return an integer. Main must return an integer.

You still have not declared the variable "i". Add "int i;"(no quotes) after you declare "var".

I still don't think you need the variable "var" or "var" at all. It can be replaced with just "i". Otherwise you get some pretty strange outputs like 2009147472. I doubt that's what you want.

Edited 6 Years Ago by WildBamaBoy: n/a

Next time when you post about problem, please include the detail of error which the compiler is complaining...

I cannot remember which C++ compiler requires main() to return int. I used to code them both ways... But on a safe side, declare it as int...

Anyway, you are attempting to use 'i' while you have not declare it first. Change for (i=0; i<5; i++) to for (int i=0; i<5; i++) and see what happen.

Oh, and you have not initiate any of value in 'var'. This is not Java that will initiate value for primitive type. I am not sure what you would get in the display... Could be garbage...

I think I there is one error remaining in this program variable i should be declare before using it.


<<snip>>

Edited 6 Years Ago by Nick Evan: Removed code (keep it legal)

This article has been dead for over six months. Start a new discussion instead.