HELP with some code needed ...

N

Nikola

Compiler prompts a dosen errors
Proffessionals, HELP!
I's not english, try anyway ...
thanks

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct osoba
{
char ime[10],prez[10],br_tel[10];
};

int unos(struct osoba x[],int br_el)
{
if(br_el>9) printf("Niz je pun");
else
{
printf("Unesite ime, prezime i broj telefona:\n");
scanf("%s %s %d",x[br_el].ime,x[br_el].prez,&x[br_el].br_tel);
br_el++;
}
return br_el;
}

void ispis(struct osoba x[],int br_el)
{
int i;
for(i=0;i<br_el;i++)
printf("\n%s %s\t%d",x.ime,x.prez,x.br_tel);
}

int brisi(struct osoba x[],int br_el)
{
char prezime[10];
int a,i,j;
scanf(" %s",prezime);
do
{
a=strcmp(x.prez,prezime);
i++;
}
while(a!=0 && i<br_el)
{
if(a==0)
{
for(j=i-1;j<br_el;j++)
{
strcpy(x[j].ime,x[j+1].ime);
strcpy(x[j].prez,x[j+1].prez);
strcpy(x[j].br_tel,x[j+1].br_tel);
}
br_el--;
}
else printf("Osoba ne postoji!");
}
}

int citaj(struct osoba x[],int br_el)
{
FILE *dat;
dat=fopen("imenik.txt","r");
//fali provjera
while(feof(dat)==0 && br_el<10)
{
fscanf(dat," %s %s %d",x[br_el].ime,x[br_el].prez,&x[br_el}.br_tel);
br_el++;
}
fclose(dat);
return br_el;
}

void pisi(struct osoba x[],int br_el)
{
FILE *dat;
int i;
dat=fopen("imenik.txt","w");
for(i=0;i<br_tel;i++)
fprintf(dat," %s %s\t%d\n",x.ime,x.prez,x.br_tel);
fclose(dat);
}

main()
{
struct osoba x[10];
int ponovo=0,unos,br_el=0;
while(ponovo==0)
{
printf("1. UNOS OSOBE\n2. ISPIS SVIH OSOBA\n3. BRISANJE OSOBE\n");
printf("4. ÈITANJE IZ DATOTEKE\n5. UNOS U DATOTEKU\n6. KRAJ");
scanf("%d",&unos);
switch (unos)
{
case 1:
br_el=unos(x,br_el);
break;
case 2:
ispis(x,br_el);
break;
case 3:
br_el=brisi(x,br_el);
break;
case 4:
br_el=citaj(x,br_el);
break;
case 5:
pisi(x,br_el);
break;
case 6;
ponovo=1;
break;
default:
printf("Krivi unos!");
break;
}
}
system("pause");
return (0);
}
 
N

Nick Austin

Compiler prompts a dosen errors
Proffessionals, HELP!
I's not english, try anyway ...
thanks

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct osoba
{
char ime[10],prez[10],br_tel[10];
};

int unos(struct osoba x[],int br_el)
{
if(br_el>9) printf("Niz je pun");
else
{
printf("Unesite ime, prezime i broj telefona:\n");
scanf("%s %s %d",x[br_el].ime,x[br_el].prez,&x[br_el].br_tel);

All three structure fields are defined are strings, so...

scanf("%s %s %s",x[br_el].ime,x[br_el].prez,x[br_el].br_tel);
br_el++;
}
return br_el;
}

void ispis(struct osoba x[],int br_el)
{
int i;
for(i=0;i<br_el;i++)
printf("\n%s %s\t%d",x.ime,x.prez,x.br_tel);
}

int brisi(struct osoba x[],int br_el)
{
char prezime[10];
int a,i,j;
scanf(" %s",prezime);
do
{
a=strcmp(x.prez,prezime);
i++;


'i' should be initialised before attempting to increment it.
}
while(a!=0 && i<br_el)

Is this the end of a do/while loop, or the start of a new while loop?
{
if(a==0)
{
for(j=i-1;j<br_el;j++)
{
strcpy(x[j].ime,x[j+1].ime);
strcpy(x[j].prez,x[j+1].prez);
strcpy(x[j].br_tel,x[j+1].br_tel);
}
br_el--;
}
else printf("Osoba ne postoji!");
}

return /* something */;
}

int citaj(struct osoba x[],int br_el)
{
FILE *dat;
dat=fopen("imenik.txt","r");
//fali provjera
while(feof(dat)==0 && br_el<10)

See:
http://www.eskimo.com/~scs/C-faq/q12.2.html
{
fscanf(dat," %s %s %d",x[br_el].ime,x[br_el].prez,&x[br_el}.br_tel);

All three structure fields are defined are strings, so...

fscanf(dat," %s %s %s",x[br_el].ime,x[br_el].prez,x[br_el].br_tel);
br_el++;
}
fclose(dat);
return br_el;
}

void pisi(struct osoba x[],int br_el)

typo:
void pisi(struct osoba x[],int br_tel)
{
FILE *dat;
int i;
dat=fopen("imenik.txt","w");
for(i=0;i<br_tel;i++)
fprintf(dat," %s %s\t%d\n",x.ime,x.prez,x.br_tel);
fclose(dat);
}

main()


int main(void)
{
struct osoba x[10];
int ponovo=0,unos,br_el=0;
while(ponovo==0)
{
printf("1. UNOS OSOBE\n2. ISPIS SVIH OSOBA\n3. BRISANJE OSOBE\n");
printf("4. ÈITANJE IZ DATOTEKE\n5. UNOS U DATOTEKU\n6. KRAJ");
scanf("%d",&unos);
switch (unos)
{
case 1:
br_el=unos(x,br_el);
break;
case 2:
ispis(x,br_el);
break;
case 3:
br_el=brisi(x,br_el);
break;
case 4:
br_el=citaj(x,br_el);
break;
case 5:
pisi(x,br_el);
break;
case 6;

typo:
case 6:
ponovo=1;
break;
default:
printf("Krivi unos!");
break;
}
}
system("pause");
return (0);
}

Nick.
 
M

Martin Ambuhl

Nikola said:
Compiler prompts a dosen errors
Proffessionals, HELP!
I's not english, try anyway ...
thanks

The right way to debug your code is to pay attention to the diagnostics
and fix the errors, one by one. Posting to a newsgroup is a poor
substitute for learning this fundamental skill.

I am not at all happy leaving all your calls to scanf, all your magic
numbers (10 is a tiny size to declare for a char array, by the way).
Nevertheless, here is a version of your code, with minimal changes, that
should at least compile:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct osoba
{
char ime[10], prez[10], br_tel[10];
};

int unos(struct osoba x[], int br_el)
{
if (br_el > 9)
printf("Niz je pun");
else {
printf("Unesite ime, prezime i broj telefona:\n");
/* mha: &x[br_el].br_tel is the address of a char array, not an
int, so either the specifier "%d" is wrong or the type of the
member br_tel is wrong. I have changed the specifier and
removed the '&'. */
scanf("%s %s %s", x[br_el].ime, x[br_el].prez, x[br_el].br_tel);
br_el++;
}
return br_el;
}

void ispis(struct osoba x[], int br_el)
{
int i;
for (i = 0; i < br_el; i++)
/* mha: x[br_el].br_tel is a char array, not an int, so either
the specifier "%d" is wrong or the type of the member br_tel
is wrong. I have changed the specifier. */
printf("\n%s %s\t%s", x.ime, x.prez, x.br_tel);
}

/* mha: you have declared brisi to return an int. It returns no value.
I have it to return a value. */
int brisi(struct osoba x[], int br_el)
{
char prezime[10];
int a, i, j;
scanf(" %s", prezime);
do {
a = strcmp(x.prez, prezime);
i++;
}
/* mha: the 'while' below is the bottom of the 'do' loop and should
be terminated with ';', not '{'. I have made this change. */
while (a != 0 && i < br_el);
if (a == 0) {
for (j = i - 1; j < br_el; j++) {
strcpy(x[j].ime, x[j + 1].ime);
strcpy(x[j].prez, x[j + 1].prez);
strcpy(x[j].br_tel, x[j + 1].br_tel);
}
br_el--;
}
else
/* mha: the else condition leads to a single statement. Either
surround it with '{' and '}' or leave out the brackets
entirely. I have added an opening brace below: */
{ /* mha: added */
printf("Osoba ne postoji!");
}
return br_el; /* mha: who knows what you want here? */
}


int citaj(struct osoba x[], int br_el)
{
FILE *dat;
dat = fopen("imenik.txt", "r");
/* fali provjera */
while (feof(dat) == 0 && br_el < 10) {
/* mha: I have fixed the '}' used for ']' in the line below.

mha: &x[br_el].br_tel is the address of a char array, not an
int, so either the specifier "%d" is wrong or the type of the
member br_tel is wrong. I have changed the specifier and
removed the '&'. */
fscanf(dat, " %s %s %s", x[br_el].ime, x[br_el].prez,
x[br_el].br_tel);
br_el++;
}

fclose(dat);
return br_el;
}

void pisi(struct osoba x[], int br_el)
{
FILE *dat;
int i;
dat = fopen("imenik.txt", "w");
/* mha: you have misspelt either 'br_el' or 'br_tel'. I have changed
the following line to have 'br_el'. */
for (i = 0; i < br_el; i++)
/* mha: x[br_el].br_tel is a char array, not an int, so either
the specifier "%d" is wrong or the type of the member br_tel
is wrong. I have changed the specifier. */
fprintf(dat, " %s %s\t%s\n", x.ime, x.prez, x.br_tel);
fclose(dat);
}

/* mha: main returns an int, and you should say so. I have made this
change below. */

int main(void)
{
struct osoba x[10];
/* mha: the line below had 'unos' declared as an int, masking the
function named 'unos' above. I have changed the variable 'unos'
to 'vnos' here and in the scanf & switch statements */
int ponovo = 0, vnos, br_el = 0;
while (ponovo == 0) {
printf
("1. UNOS OSOBE\n2. ISPIS SVIH OSOBA\n3. BRISANJE OSOBE\n");
printf("4. ÈITANJE IZ DATOTEKE\n5. UNOS U DATOTEKU\n6. KRAJ");
scanf("%d", &vnos);
switch (vnos) {
case 1:
br_el = unos(x, br_el);
break;
case 2:
ispis(x, br_el);
break;
case 3:
br_el = brisi(x, br_el);
break;
case 4:
br_el = citaj(x, br_el);
break;
case 5:
pisi(x, br_el);
break;
/* mha: I have fixed the ';' instead of ':' below. */
case 6:
ponovo = 1;
break;
default:
printf("Krivi unos!");
break;
}
}
/* mha: a simple call to getchar() is a better choice than relying
on the non-portable assumption that "pause" is meaningful. */
system("pause");
return 0;
}
 
M

Mark McIntyre

Compiler prompts a dosen errors

Well, six anyway.
First tip: READ THE COMPILER MESSAGES AND EXAMINE THE LINES FOR ERRORS.
Most of the problems with this code are typing mistakes.
scanf(" %s",prezime);
do
{
a=strcmp(x.prez,prezime);
i++;
}
while(a!=0 && i<br_el)


either this while belongs with the do, in which case a semicolon is missing
at the end of the line, or else it belongs with the next block, in which
case the "do" is missing a while statement.
fscanf(dat," %s %s %d",x[br_el].ime,x[br_el].prez,&x[br_el}.br_tel);

Is this a typing mistake when you wrote the usenet post? Don't retype your
code, copy/paste it.
Othewise, change the } to a ]
a=strcmp(x.prez,prezime);


my compiler points out to me that you did not initialise "i". Before you
can use a variable it must have a value. In this cae, you're using a random
number to index into an array. That will crash your program, if you're
lucky, and produce random garbage if you're not.
for(i=0;i<br_tel;i++)

br_tel is undefined. I think you meant br_el....
This may be another typing mistake in your usenet post.

the correct definition of main is
int main()
or
int main(void)
or
int main(int argc, char** argv)

the return type of int is mandatory.
scanf("%d",&unos);

DO NOT USE scanf until you are an expert at C. Its a very dangerous
function and is also not good at handling error conditions eg the user
types in something unexpected such as "one" instead of "1".
br_el=unos(x,br_el);

you have two objects called "unos". You can't do that - all objects must
have distinct names. In this case the variable "unos" hides the function
"unos", so you cannot call the function. Name your variables more sensibly.


another typing mistake - semicolon instead of colon
 
N

Nikola

First tip: READ THE COMPILER MESSAGES AND EXAMINE THE LINES FOR ERRORS.

Yes I know but Dev-C++ I have doesn't pop out the meningfull messages:

-parse error before
-confused by earlier errors, bailing out
and so on ..... :-(
 
C

Chris Dollin

Nikola said:
Yes I know but Dev-C++ I have doesn't pop out the meningfull messages:

-parse error before
-confused by earlier errors, bailing out
and so on ..... :-(

Look at the FIRST error message. Look for the error "just" before
the message. Fix it.

"Parse error before" is usually followed by some token; you've
most likely made a mistake just before that token.

[Really, if you don't understand the messages, why not post
the relevant slice of the program?]
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top