View previous topic :: View next topic |
Author |
Message |
kianab1985
Joined: 20 Jun 2008 Posts: 2
|
For Statement |
Posted: Fri Jun 20, 2008 12:14 pm |
|
|
Code: | while(true)
for (i=1 ;i>0 ;++i)
{
if (input(PIN_A2))
break;
else
{
output_high(PIN_C6);
delay_ms(200);
output_low(PIN_C6);
delay_ms(200);
}
} |
for a simple FOR statement
3 errors appear
Error 51 and stated that a numeric expression must appear here
this is just a simple code i modify from the help index
can anyone tell me whats the problem?? |
|
|
kianab1985
Joined: 20 Jun 2008 Posts: 2
|
|
Posted: Fri Jun 20, 2008 12:24 pm |
|
|
define i as
#define i int
i just want the program to loop
until there is an input at pin A2 |
|
|
milkman41
Joined: 16 Jun 2008 Posts: 30
|
|
Posted: Fri Jun 20, 2008 12:37 pm |
|
|
if you just want your program to loop forever, as your for statement is doing (i will always be greater than 0), you can just have a while(TRUE) or while(1) loop, as you have further up. Also, I'm fairly certain you can't define i that way, you have to define it within your main program. But you don't even need i if you just do an endless while loop. Otherwise, if you want to stick to your for loop, your code would look like this:
Code: |
void main() {
int i;
while(TRUE) {
for(i = 1; i>0; ++i) {
if (input(PIN_A2))
break;
else
{
output_high(PIN_C6);
delay_ms(200);
output_low(PIN_C6);
delay_ms(200);
}
}
}
}
|
Hope that helps,
-Nick |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Jun 20, 2008 1:10 pm |
|
|
kianab1985 wrote: | define i as
#define i int
|
You want to declare the variable i not define it. Use:
to tell the compiler the data type of i; Your statement told the compiler the string "i" is equivalent to the string "int". _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 20, 2008 1:42 pm |
|
|
kianab1985,
Please take a class in the C language, or read a textbook about it.
The purpose of this forum is not to teach the C language. |
|
|
newberries Guest
|
For Loop won't compile |
Posted: Fri Jun 20, 2008 3:23 pm |
|
|
I am having exactly the same problem. This is frustrating as I have been programming C for 20+ years (on the PC, embedded on 80c51) so can't understand why it can't compile. I am building a game for the local school to use at their summer fair (in 10 days time!), and thought using the PIC would be an ideal micro to use.
this code fails with A numerical expression must appear here!
for (int i=0; i<=10; ++i)
{
}
this code also fails with the same error message
int i;
for (i=0;i<=10;++i)
{
}
Does anyone know what is going on? I have reinstalled the compiler as I thought it could be that causing the problem.
Cheers
James |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 20, 2008 3:27 pm |
|
|
This program compiles with no errors with vs. 4.074:
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
//====================================
main()
{
int i;
for(i=0;i<=10;++i)
{
}
while(1);
} |
If it doesn't compile for you, then post your compiler version. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Jun 20, 2008 4:22 pm |
|
|
Odds are the error is actually above where the error was indicated by the compiler. Take a look above to see if you've omitted any brace or bracket that is needed.
You could also try commenting out the entire for loop and see if the error moves down to the next section. This would indicate the error is in the code above the for loop.
Ronald |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Jun 23, 2008 2:33 am |
|
|
In C declarations have to be at the start of a block {}. And the compiler will usually fail if you try and declare a variable imediately after a function or instructrion.
Code: |
main { // Start of block
int i;
// Code
}
or
main {
while(1) { // Start of block
int i;
// code
}
}
or
main {
//code
{ // Start of block
int i;
//code
}
}
|
The ability to define vars at anypoint in the code and within statements such as for (int i=0;...) was introduced in C++ |
|
|
|