Compile Error

B

Bill

Can someone help me with this error:

error C2143: syntax error : missing ';' before 'type'

Here is the line it's complaining about:

for(int i=0;i<4;i++){
printf("%d",x);
}

Any help would be appreciated.
 
C

Christopher Benson-Manica

Bill said:
Here is the line it's complaining about:
for(int i=0;i<4;i++){
printf("%d",x);
}


That isn't valid C89. (I don't recall whether it's valid C99.) Try

int i;
for( i=0; i < 4; i++ ) {
printf( "%d", x );
}

Whether that's correct depends on the definition of x, which you
didn't include in your post.
 
B

Bill

Bill said:
Here is the line it's complaining about:
for(int i=0;i<4;i++){
printf("%d",x);
}


That isn't valid C89. (I don't recall whether it's valid C99.) Try

int i;
for( i=0; i < 4; i++ ) {
printf( "%d", x );

}

Whether that's correct depends on the definition of x, which you
didn't include in your post.


Sorry, : int x[4]
 
B

Bill

Bill said:
Here is the line it's complaining about:
for(int i=0;i<4;i++){
printf("%d",x);
}


That isn't valid C89. (I don't recall whether it's valid C99.) Try

int i;
for( i=0; i < 4; i++ ) {
printf( "%d", x );

}

Whether that's correct depends on the definition of x, which you
didn't include in your post.


I tried this and I get the same error message. Any ideas?
 
C

Chris Dollin

Bill said:
Bill said:
Here is the line it's complaining about:
for(int i=0;i<4;i++){
printf("%d",x);
}


That isn't valid C89. (I don't recall whether it's valid C99.) Try

int i;
for( i=0; i < 4; i++ ) {
printf( "%d", x );

}

Whether that's correct depends on the definition of x, which you
didn't include in your post.


I tried this and I get the same error message. Any ideas?


Show us the code /in context/. Cutnpaste.

If you're using C90 and that `int i;` declaration is preceeded by
a statement then you'll get a syntax error, which you have to fix by
pushing the declaration up to another declaration or start-of-block,
whichever comes first.

PS I believe it's usual to not quote signatures.
 
C

Christopher Benson-Manica

Bill said:
int i;
for( i=0; i < 4; i++ ) {
printf( "%d", x );
}

I tried this and I get the same error message. Any ideas?

You'll have to post a complete snippet (preferably a whole program),
as there's nothing further to be guessed from the fragment you posted.

#include <stdio.h>

int main( void ) {
int i;
int x[4] = { 0, 1, 2, 3 };
for( i=0; i < 4; i++ ) {
printf( "%d", x );
}
printf( "\n" );
return 0;
}
 
B

Bill

Bill said:
Here is the line it's complaining about:
for(int i=0;i<4;i++){
printf("%d",x);
}
That isn't valid C89. (I don't recall whether it's valid C99.) Try
int i;
for( i=0; i < 4; i++ ) {
printf( "%d", x );
}
Whether that's correct depends on the definition of x, which you
didn't include in your post.

I tried this and I get the same error message. Any ideas?

Show us the code /in context/. Cutnpaste.

If you're using C90 and that `int i;` declaration is preceeded by
a statement then you'll get a syntax error, which you have to fix by
pushing the declaration up to another declaration or start-of-block,
whichever comes first.

PS I believe it's usual to not quote signatures.

--
Chris "electric hedgehog" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/- Hide quoted text -

- Show quoted text -


Here you go!

#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<process.h>

int no=0;
int client[4];
void calno(int);
void main(){


// clrscr();
int x[4],check,k,the_eater,the_eaten,score=1000;
char ch;


//Random no generation************************
randomize();
while(1){
x[0]=random(9);
if(x[0]!=0)
break;
}
while(1){
x[1]=random(9);
if(x[0]!=x[1])
break;
}
while(1){
x[2]=random(9);
if(x[2]!=x[1]&&x[2]!=x[0])
break;
}
while(1){
x[3]=random(9);
if(x[3]!=x[2]&&x[3]!=x[1]&&x[3]!=x[0])
break;
}
//**********************************************

int i;
for( i=0; i < 4; i++ ) {
printf( "%d", x );
}

printf(" ----- This is a guessing game ------");
printf("________________________________________");
printf("***First read these instructions***");
printf("1.The number should be a 4 digit number without 0 in the
first place");
printf("2. The digit shouldn't be repeated");
printf("Want to play? (Y/N): ");
scanf("%c",&ch);
if(ch=='n'||ch=='N')
exit(0);
printf("-------------------------------------------------------");
printf("^^^^^^^^^Guess the number^^^^^^^^:");

//starting of guessing******************

for(i=1;i<=10;i++){
the_eater=0;
the_eaten=0;
printf("Enter the %d guess: ",i);
scanf("%d",&check);
calno(check);
for(int count=0;count<4;count++){
if(x[count]==client[count])
the_eater=the_eater+1;
}
printf("
You have got %d the_eater",the_eater);
if(the_eater==4)
break;
//---------------check for the_eaten----------------------//
for(int m=0;m<4;m++){
for(int n=0;n<4;n++){
if(client[m]!=x[m]){
if(client[m]==x[n])
the_eaten=the_eaten+1;
}
}
}
//-------------------------------------------//
printf(" and %d the_eaten",the_eaten);
score=score-100;

}
if(the_eater<4){
printf("-----Game Over-----");
printf("the number is ");
for(int i=0;i<4;i++){
printf("%d",x);
}
}

else
printf("-----You have got the no in %d guess and you score is
%d----",i,score);
getch();
}



void calno(int no){
int a=no,x,y;
client[3]=a%10;
x=a/10;
client[2]=x%10;
y=a/100;
client[1]=y%10;
client[0]=a/1000;
}
 
C

Christopher Benson-Manica

Here you go!

(snip code destroyed by Google)

Criminy. That's rather a mess, but I suppose we asked for it. I
don't have any more time to clean it up for other posters, but I've
done what I could. You still have a lot of problems that have nothing
to do with the error you posted. Think about these things, and note
that I changed main to int main(void) - your version was wrong.

test2.c: In function `main':
test2.c:11: warning: implicit declaration of function `randomize'
test2.c:13: error: too many arguments to function `random'
test2.c:18: error: too many arguments to function `random'
test2.c:23: error: too many arguments to function `random'
test2.c:28: error: too many arguments to function `random'
test2.c:33: warning: ISO C89 forbids mixed declarations and code
test2.c:56: error: `for' loop initial declaration used outside C99 mode
test2.c:63: error: `for' loop initial declaration used outside C99 mode
test2.c:64: error: `for' loop initial declaration used outside C99 mode
test2.c:78: error: `for' loop initial declaration used outside C99 mode
test2.c:84: warning: too many arguments for format
test2.c:85: warning: implicit declaration of function `getch'
test2.c:8: warning: unused variable `k'

#include<stdlib.h>
#include<stdio.h>

int no=0;
int client[4];
void calno(int);
int main(void) {
int x[4],check,k,the_eater,the_eaten,score=1000;
char ch;

randomize();
while(1){
x[0]=random(9);
if(x[0]!=0)
break;
}
while(1){
x[1]=random(9);
if(x[0]!=x[1])
break;
}
while(1){
x[2]=random(9);
if(x[2]!=x[1]&&x[2]!=x[0])
break;
}
while(1){
x[3]=random(9);
if(x[3]!=x[2]&&x[3]!=x[1]&&x[3]!=x[0])
break;
}

int i;
for( i=0; i < 4; i++ ) {
printf( "%d", x );
}

printf(" ----- This is a guessing game ------");
printf("________________________________________");
printf("***First read these instructions***");
printf("1.The number should be a 4 digit number without 0 in the first place");
printf("2. The digit shouldn't be repeated");
printf("Want to play? (Y/N): ");
scanf("%c",&ch);
if(ch=='n'||ch=='N')
exit(0);
printf("-------------------------------------------------------");
printf("^^^^^^^^^Guess the number^^^^^^^^:");

for(i=1;i<=10;i++){
the_eater=0;
the_eaten=0;
printf("Enter the %d guess: ",i);
scanf("%d",&check);
calno(check);
for(int count=0;count<4;count++){
if(x[count]==client[count])
the_eater=the_eater+1;
}
printf(" You have got %d the_eater",the_eater);
if(the_eater==4)
break;
for(int m=0;m<4;m++){
for(int n=0;n<4;n++){
if(client[m]!=x[m]){
if(client[m]==x[n])
the_eaten=the_eaten+1;
}
}
}

printf(" and %d the_eaten",the_eaten);
score=score-100;
}
if(the_eater<4){
printf("-----Game Over-----");
printf("the number is ");
for(int i=0;i<4;i++){
printf("%d",x);
}
}

else
printf("-----You have got the no in %d guess and you score is d----",i,score);
getch();
}



void calno(int no){
int a=no,x,y;
client[3]=a%10;
x=a/10;
client[2]=x%10;
y=a/100;
client[1]=y%10;
client[0]=a/1000;
}
 
B

Barry Schwarz

On 12 Feb 2007 11:14:43 -0800, "Bill" <[email protected]>
wrote:

snip irrelevant previous messages
#include<conio.h>

A non-standard header you probably don't need.
#include<stdlib.h>
#include<stdio.h>
#include<process.h>

Another non-standard header.
int no=0;
int client[4];
void calno(int);
void main(){

Please
int main(void){
// clrscr();
int x[4],check,k,the_eater,the_eaten,score=1000;

C89 does not support declaration that follow statements.
char ch;


//Random no generation************************
randomize();
while(1){
x[0]=random(9);

Learn to indent your loops and if ranges. The return on investment is
phenomenal.
if(x[0]!=0)
break;
}
while(1){
x[1]=random(9);
if(x[0]!=x[1])
break;
}
while(1){
x[2]=random(9);
if(x[2]!=x[1]&&x[2]!=x[0])
break;
}
while(1){
x[3]=random(9);
if(x[3]!=x[2]&&x[3]!=x[1]&&x[3]!=x[0])
break;
}
//**********************************************

int i;

Another invalid declaration. This probably the source of the error
you ask about in the original post.
for( i=0; i < 4; i++ ) {

snip rest of code


Remove del for email
 
R

Richard Heathfield

Bill said:
On Feb 12, 9:48 am, Chris Dollin <[email protected]> wrote:
Here you go!

me@here> make
foo.c:1: conio.h: No such file or directory
foo.c:4: process.h: No such file or directory
foo.c:39: unterminated comment

me@here> indent foo.c
indent: foo.c:49: Warning:Unterminated string constant
indent: foo.c:50: Warning:Unterminated string constant
indent: foo.c:71: Warning:Unterminated string constant
indent: foo.c:72: Warning:Unterminated string constant
indent: foo.c:97: Error:Unmatched 'else'
indent: foo.c:98: Warning:Unterminated string constant
indent: foo.c:99: Warning:Unterminated string constant

I removed conio.h and process.h, changed void main to int main(void) and
removed the remainder of any line beginning //. I spliced broken string
literals. I changed the randomize() call to srand(0), and I provided a
random() function:

int random(int max)
{
return max * (rand() / (RAND_MAX + 1.0));
}

I moved int i; from where it is now to the definitions section at the
top of main, to join x[4], check, etc. I removed int from for(int count
and instead defined count alongside i at the top of the function. Same
for the m-loop and n-loop a few lines further on. And since we already
had an i, I simply dropped int from int i in a loop a few lines further
on still.

I removed the call to getch, since there is no such function. I removed
k's definition, since it is never used. I added the statement:

return 0;

to the end of main. Since the file scope object named 'no' was never
used, I removed it. That's a poor name for an object, by the way. Think
about how easy it would be to misunderstand what it means.


With these fairly minimal changes, I compiled the program, and ran it.
Here is the output I got:

me@here> ./foo
7381 ----- This is a guessing game
------________________________________________***First read these
instructions***1.The number should be a 4 digit number without 0 in the
first place 2. The digit shouldn't be repeatedWant to play? (Y/N):

As you can see, some work remains to be done, but at least it now
compiles.
 
C

Chris Dollin

Bill said:
Bill said:
On Feb 12, 9:22 am, Christopher Benson-Manica
Here is the line it's complaining about:
for(int i=0;i<4;i++){
printf("%d",x);
}

That isn't valid C89. (I don't recall whether it's valid C99.) Try
int i;
for( i=0; i < 4; i++ ) {
printf( "%d", x );

Whether that's correct depends on the definition of x, which you
didn't include in your post.

I tried this and I get the same error message. Any ideas?

Show us the code /in context/. Cutnpaste.

If you're using C90 and that `int i;` declaration is preceeded by
a statement then you'll get a syntax error, which you have to fix by
pushing the declaration up to another declaration or start-of-block,
whichever comes first.

PS I believe it's usual to not quote signatures.


I believe it's usual to not quote signatures.

(fx:snipSnipSnip)
while(1){
x[3]=random(9);
if(x[3]!=x[2]&&x[3]!=x[1]&&x[3]!=x[0])
break;
}
//**********************************************

int i;

In C90 you can't have a declaration following a statement ...
 
C

Christopher Benson-Manica

Learn to indent your loops and if ranges. The return on investment is
phenomenal.

OP's code probably was properly (or at least sanely) indented; the
pathetic inadequacies of Google Groups are no doubt the actual culprit
behind the abominable indentation of the posted code.
 
M

Mark McIntyre

You need to read a basic introduction to C, and concentrate on where
and when you are allowed to declare variables. Hint: C is different
to C++.
void main(){

main returns an int. Please do not use void main().
(code snipped)

you can't declare a variable here ( in the C version you're using. To
do it here, you need a newer compiler complying to the C99 standard.)

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
P

Peter Nilsson

Christopher Benson-Manica said:
OP's code probably was properly (or at least sanely) indented;
the pathetic inadequacies of Google Groups are no doubt the
actual culprit behind the abominable indentation of the posted
code.

In this case, I think people's dependancy on certain sized tab
settings and/or proportional fonts is more often a source of
problems than Google. The Google interface to usenet is
terrible, but it's still workable.

I generally don't have problems posting code, but then my code
is usually tab free and copy/pasted from plain text editors
(like Notepad) as opposed to word processors or flashy IDE
editors with what-you-save-is-nothing-like-what-you-see
features.
 

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

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top