Chinaunix首页 | 论坛 | 博客
  • 博客访问: 918213
  • 博文数量: 132
  • 博客积分: 9976
  • 博客等级: 中将
  • 技术积分: 1781
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-30 20:40
文章分类

全部博文(132)

文章存档

2013年(1)

2011年(1)

2010年(15)

2009年(77)

2008年(36)

2007年(2)

我的朋友

分类: C/C++

2010-04-17 13:16:13

expected declaration or statement at end of input


==========================================================================
from:

error: expected declaration or statement at end of input ?

[ to get rid of this advertisement]
We are trying to compile this program and we keep getting the same error, we can't find where the problem is can somenone help us please.
Thanks a lot

#include
#include
struct vino{
char nombre[30];
char tipo[15];
char nacionalidad[15];
char sabor[15];};





void leer(FILE* archivo, struct vino mibodega[7]){
int i; i=0;
while (i<7){

fscanf(archivo, "%s",mibodega[i].nombre);
fscanf(archivo, "%s",mibodega[i].tipo);
fscanf(archivo, "%s",mibodega[i].nacionalidad);
fscanf(archivo, "%s",mibodega[i].sabor);
i++;
}


int main(void){
FILE* f=fopen("vinos.txt","r");

int i,j;
struct vino mibodega[7];
leer (f,mibodega);
printf("%s", mibodega[4].sabor);
}


I believe you are missing an } before main


Quote:
Code:
#include
#include
struct vino{
char nombre[30];
char tipo[15];
char nacionalidad[15];
char sabor[15];};





void leer(FILE* archivo, struct vino mibodega[7]){
int i; i=0;
}
while (i<7){

fscanf(archivo, "%s",mibodega[i].nombre);
fscanf(archivo, "%s",mibodega[i].tipo);
fscanf(archivo, "%s",mibodega[i].nacionalidad);
fscanf(archivo, "%s",mibodega[i].sabor);
i++;
}


int main(void){
FILE* f=fopen("vinos.txt","r");

int i,j;
struct vino mibodega[7];
leer (f,mibodega);
printf("%s", mibodega[4].sabor);
}
The red brace is the one you are missing. I had this error come up in a program I was working on the other day.


Thanks a lot, we've manage to solve the problems and complete the program of the expert systems.
Thanks to all
==========================================================================
from:


linked list and read strings-problem

GO TO POST 6(is my new problem)
{
this solved
I am getting this message when i am trying to compile the code
series1.c:44: σφάλμα: expected declaration or statement at end of input
series1.c:44: σφάλμα: expected declaration or statement at end of input
where is the mistake?(maybe any missing { or })


Code:
#include 
#include

struct node{
char data[64];
struct node *next;
};
struct node *firsta, *currenta, *newa;

int main(void)
{
int k;
scanf("%d", &k);

struct node *head=NULL;

do{
switch(k){
case 1: addnew();
break;

}while(k!=0);

void addnew(void)
{
struct node *newnode=malloc(sizeof(struct node));

if (head=NULL)
firsta=newa=currenta;
else
{
currenta=firsta;
while(currenta->next!=NULL)
currenta=currenta->next;

currenta->next=newa;
currenta=newa;
}

gets(currenta->data);

currenta->next=NULL;
}
}SOLVED

Last edited by alzar; 09-20-2007 at 12:23 PM.
alzar is offline   Reply
 With Quote
Old 09-20-2007, 09:20 AM   #
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
If you indent your code correctly, you'll notice that you are missing two curly braces...

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply
 With Quote
Old 09-20-2007, 09:20 AM   #
and the hat of Jobseeking
 
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,810
Count the number of closing braces which your main() seems to have.

Read the FAQ on why you shouldn't use gets()
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Salem is offline   Reply
 With Quote
Old 09-20-2007, 09:52 AM   #
Registered User
 
Join Date: Aug 2007
Posts: 63
Quote:
Originally Posted by alzar View Post
I am getting this message when i am trying to compile the code
series1.c:44: σφάλμα: expected declaration or statement at end of input
series1.c:44: σφάλμα: expected declaration or statement at end of input
where is the mistake?(maybe any missing { or })


Code:
#include 
#include

struct node{
char data[64];
struct node *next;
};
struct node *firsta, *currenta, *newa;

int main(void)
{
int k;
scanf("%d", &k);

struct node *head=NULL;

do{
switch(k){
case 1: addnew();
break;
}
}while(k!=0);

return 0;
}
void addnew(void)
{
struct node *newnode=malloc(sizeof(struct node));

if (head=NULL)
firsta=newa=currenta;
else
{
currenta=firsta;
while(currenta->next!=NULL)
currenta=currenta->next;

currenta->next=newa;
currenta=newa;
}

gets(currenta->data);

currenta->next=NULL;
}
This code was a part of an exercise that i have, so i forget to close switch and main.Sorry
alzar is offline   Reply
 With Quote
Old 09-20-2007, 11:10 AM   #
Registered User
 
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,933
Code:
int main(void)
{
int k;
scanf("%d", &k);

struct node *head=NULL;

do {
switch(k) {
case 1:
addnew();
break;
}
} while(k!=0);
This is an infinite loop if k is anything other than 0. There is no modification of k beyond the initial scanf call.



Code:
void addnew(void)
{
struct node *newnode=malloc(sizeof(struct node));

if (head=NULL)
firsta=newa=currenta;
= is for assignment, == is to test for equality. Second, head is not in scope here. Third, currenta hasn't yet been initialized the first time the function is called, head is equal to NULL and in fact does not change it's value so it will always be NULL and the rest of the code (the else part) will never get executed.


Code:
    else
{
currenta=firsta;
while(currenta->next!=NULL)
currenta=currenta->next;

currenta->next=newa;
currenta=newa;
}

gets(currenta->data);

currenta->next=NULL;
}
newa never gets initialized to anything valid (in the above if test it is effectively set to the same random address as currenta). Same thing with firsta and since currenta points to this random memory address I certainly wouldn't want to try and access its next pointer member. I think newa is really supposed to be newnode here in this function and firsta is maybe supposed to be set to head which gets passed in as a parameter?


The whole function should either be returning a pointer to a new node, or setting an argument passed into the function to the newly allocated memory, or something like that. Bottom line is there's a lot wrong with this code that needs fixing.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply
 With Quote
Old 09-20-2007, 12:10 PM   #
Registered User
 
Join Date: Aug 2007
Posts: 63
@hk_mp5kpdw
i make some changes. What's wrong?
Code:
#include 
#include

struct node{
char data[64];
struct node *next;
};
struct node *firsta, *currenta, *newa;
void addnew(void);

int main(void)
{
int k;

firsta=NULL;

do{
fflush(stdin);
printf("enter a choice\n");
scanf("%d", &k);
switch(k){
case 1:
printf("enter string");
fflush(stdin);
addnew();
break;}

}while(k!=0);
return 0;
}
void addnew(void)
{
newa=(struct node *)malloc(sizeof(struct node));

if (firsta==NULL)
firsta=currenta=newa;
else
{
currenta=firsta;
while(currenta->next!=NULL)
currenta=currenta->next;

currenta->next=newa;
currenta=newa;
}

gets(currenta->data);

currenta->next=NULL;
}
I am getting something like this
enter a choice
1
enter stringenter a choice
1
enter stringenter a choice
asd
enter stringenter a choice
0
owner@owner-laptop

Last edited by alzar; 09-20-2007 at 12:18 PM.
alzar is offline   Reply
 With Quote
Old 09-20-2007, 01:11 PM   #
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Code:
fflush(stdin);
Not what you wnat to do. Check the FAQ for "How do I clear the input buffer" or some such.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply
 With Quote
Old 09-20-2007, 01:18 PM   #
Registered User
 
Join Date: Aug 2007
Posts: 63
i found only this

should i replace fflush with something?if yes with what?
or just remove it from the code?
alzar is offline   Reply
 With Quote
Old 09-20-2007, 01:22 PM   #
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by alzar View Post
i found only this

should i replace fflush with something?if yes with what?
or just remove it from the code?
No, another bit further down:


--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply
 With Quote
Old 09-20-2007, 09:25 PM   #
CSharpener
 
 
Join Date: Oct 2006
Posts: 5,524
1. you should not cast malloc (see FAQ)
2. you HAVE TO check the return value of malloc
3. you SHOULD NOT use gets (see FAQ)
4. Why do you need currenta and newa as globals? Make the local for addnew
5. better to make *firsta local for main and pass it as a parameter to addnew (pointer to it actually to be able to modify it)
6. do not forget to free the list before you exit the program
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply
 With Quote
Old 09-21-2007, 01:22 AM   #
Registered User
 
Join Date: Aug 2007
Posts: 63
@matsp
Thank you so much.I didn't know that fflush is so dangerous.I replace the fflush with
while ((ch = getchar()) != '\n' && ch != EOF);
but now i have another problem look:
PHP Code:
owner@owner-laptop:~$ ./series1
                        
/*the first time i must enter in order to print me the"enter choice"*/
enter a choice
1
enter string
hello there

enter a choice

Is there a way not to have this problem?(look in my edit down here in this post)

ps:in the link you gave me the BUFSIZ where is? In stdio.h?

@vart
thank you.I know all of them but this not the finally code.I will make some of the changes you tell me in the end and also the addnew function must have no arguments.




edit:i delete the first while and i haven't this problem,but maybe is necessary ?i don't know.In the book where i saw the code it cleans the input also before enter choice

note:there is also other cases after, such as delete an element of the list or print an element

Last edited by alzar; 09-21-2007 at 01:29 AM.
alzar is offline   Reply
 With Quote
Old 09-21-2007, 03:21 AM   #
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
You can't just "flush" the input buffer if there's nothing there. You'll need to try to get something first, then flush if you think the user input more than you wanted. [But better make SURE that this is the case, otherwise it will be the same problem].

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply
 With Quote
Old 09-22-2007, 03:15 AM   #
Registered User
 
Join Date: Aug 2007
Posts: 63
i update my post and still have some problems with what it prints me
case 1 is to add a newnode
case 2 to print the list
case 3 to print one element(asks to give an index) of the list and delete it
case 4 to find the length of a string that i gave his index


it prints me
enter a choice
1
enter string
hello
enter a choice
1
enter string
there
enter a choice
you
enter string

so in enter choice i write "you" and then it didn't tell me again enter choice but enter string.why?
the hole code(a little big)
Code:
#include 
#include
#include
#include

struct node{
char data[64];
struct node *next;
};
struct node *firsta, *currenta, *newa;

void addnew(void);
void printall(struct node *head);
void printanddelete( int i);
int findlen(int j);
int slen(char *p);

int main(void)
{
int k,i,j,m;
char ch;
firsta=NULL;

do{
printf("enter a choice\n");
scanf("%d", &k);
switch(k){
case 1:
printf("enter string\n");
while ((ch = getchar()) != '\n' && ch != EOF);
addnew();
break;

case 2:
printall(firsta);
break;

case 3:
printf("enter a number");
scanf("%d", &i);
printanddelete(i);
break;
case 4:
printf("enter index to find length");
scanf("%d", &j);
m=findlen(j);
printf("%d", m);}
}while(k!=0);
return 0;
}
void addnew(void)
{
newa=(struct node *)malloc(sizeof(struct node));

if (firsta==NULL)
firsta=currenta=newa;
else
{
currenta=firsta;
while(currenta->next!=NULL)
currenta=currenta->next;

currenta->next=newa;
currenta=newa;
}

gets(currenta->data);

currenta->next=NULL;
}

void printall(struct node *head)
{
struct node *current=head;

if(head==NULL)
return;

while(current!=NULL)
{printf(current->data);
printf("\n");
current=current->next;

}
printf("~");
}


void printanddelete( int i)
{
int index=1;
struct node *current=firsta;
struct node *prev;

while (current!=NULL){
if(i==index)
{printf(current->data);
if(current==firsta)
firsta=current->next;
else
prev=current->next;

free(current);
current=prev;
break;}
else{
index++;
current=current->next;}
}
}

int findlen(int j)
{
struct node *current=firsta;
int index=1;
int l;

while (current!=NULL){
if (j==index)
return(slen(current->data));
else
index++;
current=current->next;
}
}


int slen(char *p)
{
if (*p=='\0')
return 0;
else
return(slen(++p) +1);
}

Last edited by alzar; 09-22-2007 at 03:21 AM.
alzar is offline   Reply
 With Quote
Old 09-22-2007, 12:07 PM   #
Registered User
 
Join Date: Aug 2007
Posts: 63
anyone please?


==========================================================================


==========================================================================


==========================================================================


==========================================================================


==========================================================================


==========================================================================


阅读(8280) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~