Error : Expected expression before 'struct' - c

I cant figure out what I am doing wrong here / how do I fix it. It is giving me an indication of expecting an expression before struct on line 90. Can somebody help me out with this problem?
I want to print a structure that is in a function, but because of the 2 arguments behind it I cant find a proper way to code it. The other functions do work fine, but when I add this one it all goes wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct geboorteDatum
{
int geboortedag;
int geboortemaand;
int geboortejaar;
};
struct Persoon
{
struct geboorteDatum;
char naam[20];
};
void printPersonen(struct Persoon *pt, int persoonCount);
void scanPersonen(int persoonCount);
int startMenu(void);
int main()
{
startMenu();
}
void printPersonen(struct Persoon *pt, int persoonCount)
{
printf("Gegevens persoon 1 : ");
printf("\n%s",pt[persoonCount].naam);
printf("\n%d",pt[persoonCount].geboortedag);
printf("\n%d",pt[persoonCount].geboortemaand);
printf("\n%d",pt[persoonCount].geboortejaar);
printf("\n");
}
void scanPersonen(int persoonCount)
{
struct Persoon pt[100];
printf("Voer Gegevens persoon . : \n");
scanf("%s", pt[persoonCount].naam);
scanf("%d", &pt[persoonCount].geboortedag);
scanf("%d", &pt[persoonCount].geboortemaand);
scanf("%d", &pt[persoonCount].geboortejaar);
}
int startMenu(void)
{
int keuze = 0;
int persoonCount = 0;
int * p1 = &persoonCount;
do
{
printf("MENU \n");
printf("1 : Voer een persoon in \n");
printf("2 : Toon alle geboortedatums \n");
printf("3 : Toon de jongste persoon \n");
printf("4 : Toon verjaardagen in een maand \n");
printf("0 : Einde \n\n");
printf("Maak een keus : ");
scanf("%d", &keuze);
printf("\n");
if(keuze == 1)
{
persoonCount++;
scanPersonen(persoonCount);
}
else if(keuze == 2)
{
printPersonen(struct Persoon *pt, int persoonCount);
}
else if(keuze == 3)
{
printf("jo3");
}
else if(keuze == 4)
{
printf("%d",persoonCount);
}
else
{
printf("Deze keus is niet mogelijk, kies opnieuw \n\n");
return startMenu();
}
} while (keuze > 0);
return persoonCount;
}

Well, in:
struct Persoon
{
struct geboorteDatum;
char naam[20];
};
that struct geboorteDatum specifies a type, but no member name! You can fix that by adding one struct geboorteDatum d;
Then in your print and scan functions replace
printf("\n%d",pt[persoonCount].geboortedag);
with
printf("\n%d",pt[persoonCount].d.geboortedag);
But even better would be to change the struct geboorteDatum to
struct Date {
int day;
int month;
int year;
}
as a birthday is just a date. you do not need a special type to represent birthdays.
Then struct Persoon becomes:
struct Person {
struct Data birthday;
char name[20];
};
And the print statements become
printf("\n%d", pt[persoonCount].birthday.day);
(And translate all code to English for easier sharing. I could understand the dutch part, but most people cannot.)

Cough
well....
printPersonen(struct Persoon *pt, int persoonCount);
means you are declaring a prototype, and in that context is incorrect. May be it should be:
printPersonen(pt, persoonCount);

Related

Using Struct in C to create a library for items and giving location feedback based upon user entry. I just cant seem to find my error

//make a location marker for key items
#include <stdio.h>
#include <string.h>
struct find {int index; char name[50]; char location[50]; };
int main() {
char locate_item[50];
//using struct to add items
struct find item1;
item1.index = 1;
strcpy(item1.name, "guitar");
strcpy(item1.location, "Usually near the table in the living room area.\n");
struct find item2;
item2.index = 2;
strcpy(item2.name, "ipad");
strcpy(item2.location, "Usually on the table or charging on the bed.\n");
//using while and if statements to get user feedback and display the appropriate location
while (locate_item != item1.name || locate_item != item2.name) {
printf("what is the item you want to find? \n");
scanf("%s", locate_item);
printf("You entered %s\n", locate_item);
if (locate_item == item1.name) {
printf("%s", item1.location);
} else if (locate_item == item2.name) {
printf("%s", item2.location);
} else {
printf("Incorrect entry. Please try again.\n");
}
}
return 0;
}
locate_item != item1.name || locate_item != item2.name compares pointer, however, you want to use strcmp() to compare two strings by value. strcmp() returns 0 if the two strings are the same.
In either case, your program doesn't make a lot of sense. You probably want to find locate_item in the an array of items (renamed from find) along these lines:
#include <stdio.h>
#include <string.h>
struct item {
int index;
char name[50];
char location[50];
};
int main() {
struct item items[] = {
{ 1, "guitar", "Usually near the table in the living room area." },
{ 2, "ipad", "Usually on the table or charging on the bed." },
{ 0 }
};
for(;;) {
printf("what is the item you want to find? \n");
char locate_item[50];
scanf("%s", locate_item);
//printf("You entered %s\n", locate_item);
for(int i = 0; items[i].name[0]; i++) {
if(!strcmp(items[i].name, locate_item)) {
printf("%s\n", items[i].location);
goto done;
}
}
printf("Incorrect entry. Please try again.\n");
}
done:
return 0;
}

Initialization from incompatible pointer type warning

I am trying to make an array of pointer pointers to point in an stuct.
until now I have the following code done
#include <stdlib.h>
typedef struct ID{
char firstName[21];
char lastName[21];
char phoneNumber[11];
}ID;
int main(int argc, char *argv[]) {
int answere,i=0;
printf("******* WELCOME TO PHONEBOOK **********\n\n\n");
printf("***************************************\n");
printf("* MENU *\n");
printf("* *\n");
printf("* 1.Add New 2.Print list 3.Exit *\n\n");
printf("***************************************\n\n");
ID* ptr=(int*)malloc(21*sizeof(ID));
do
{
printf("Please select (1, 2 or 3): ");
scanf("%d",&answere);
if(answere!=1 && answere!=2 && answere!=3)
{
printf("//...Error...\\ please give me a correct answere: ");
}
if(answere == 1)
{
i++;
addNew(&ptr,i);
}
else if(answere==2)
{
PrintList(&ptr,i);
}
}while(answere!=3);
return 0;
}
so the ptr array gonna be each time a new user and I want it to max out at 3 users the code for adding a new user is the following
ID addNew(ID *ptr,int i){
char fname,lname,phone;
if(i<3)
{
int l;
for (l=1;l<=3;l++)
{
ID user;
ptr[l] = user;
printf("enter the first name: ");
scanf("%s",&fname);
*ptr->firstName= fname;
printf("enter the last name: ");
scanf("%s",&lname);
*ptr->lastName = lname;
printf("enter the phone number: ");
scanf("%s",&phone);
*ptr->phoneNumber = phone;
}
}
else
{
printf("sorry but you have reach max capacity\n");
}
}
the problem is that when I am calling the first fuction after the assignment of values the programm crashes. What is wrong?
You function prototype ID addNew(ID *ptr,int i), so you must call it with addNew(ptr,i), not using &ptr because you've already declared ptr as ID*.
I hope it helps.
you scan a string into a single character. which probably results in memory overrung and thus the behavior of your program is UNDEFINED
scanf("%s",&fname); // here fname is only 1 char long, probably a mistake

Why does this code still print 1, even though I already add item?

Currently I'm making a storage program, but it still print 1, when I already add an item. Can someone tell me what's wrong with this code?
Direction: Input Storage (1-10) enter 1 -> Add Item (enter anything) -> Back to main menu -> Show Item
#include <stdio.h>
#include <stdlib.h>
struct info1
{
int quantity2;
char name[60];
};
int main(void)
{
start: ;
struct info1 item1;
struct info1 item2;
struct info1 item3;
struct info1 item4;
struct info1 item5;
struct info1 item6;
struct info1 item7;
struct info1 item8;
struct info1 item9;
struct info1 item10;
int quantity1, mainmenu;
item1.quantity2 = 0;
item2.quantity2 = 0;
item3.quantity2 = 0;
item4.quantity2 = 0;
item5.quantity2 = 0;
item6.quantity2 = 0;
item7.quantity2 = 0;
item8.quantity2 = 0;
item9.quantity2 = 0;
item10.quantity2 = 0;
printf("\n==Storage==");
printf("\n\nInput Storage (1-10) : ");
scanf("%d", &quantity1);
printf("\nMain Menu\n1.Add Item\n2.Show Item\n3.Search Item\n4.Exit\n");
scanf("%d", &mainmenu);
if (quantity1 == 1)
{
if (mainmenu == 1)
{
printf("\nItem Name : ");
scanf("%s", item1.name);
fflush(stdin);
printf("\nItem Quantity : ");
scanf("%d", &item1.quantity2);
}
else if (mainmenu == 2)
{
printf("\n==Item List==");
if (item1.quantity2 == 0)
{
printf("\n1. - ");
}
else if (item1.quantity2 > 0)
{
printf("\n1. %s %d pcs", item1.name, item1.quantity2);
}
}
}
goto start;
}
In essence your program has this structure
int main(void)
{
start: //doesn't need ';'
struct info1 item1; //declared something
int quantity1, mainmenu;
//... and other setup
//then some stuff you want to do over and over again
goto start;
}
However, you want to keep using the values so don't want to go back to start.
What you really want to do is go back to the menu (and then think about making exit work later).
So this structure:
int main(void)
{
struct info1 item1; //declared something
int quantity1, mainmenu;
//... and other setup
start: //MOVED
//then some stuff you want to do over and over again
goto start;//like where we set stuff up we don't actually want to re-setup - oops!
}
i.e. in your code
int main(void)
{
struct info1 item1;
//... other stuff...
printf("\n==Storage==");
printf("\n\nInput Storage (1-10) : ");
scanf("%d", &quantity1);
start: //<------- more a loop than a start
printf("\nMain Menu\n1.Add Item\n2.Show Item\n3.Search Item\n4.Exit\n");
scanf("%d", &mainmenu);
//... menu code
//...
goto start;
}
There are those who say using goto is a bad idea.
We could use a loop instead:
int main(void)
{
struct info1 item1;
// etc
int quantity1=0, mainmenu=0; //,- initialise!
item1.quantity2 = 0;
// etc
printf("\n==Storage==");
printf("\n\nInput Storage (1-10) : ");
scanf("%d", &quantity1);
while (mainmenu != 4) //<- stop when the user says 4 for exit
{
printf("\nMain Menu\n1.Add Item\n2.Show Item\n3.Search Item\n4.Exit\n");
scanf("%d", &mainmenu);
if (quantity1 == 1)
{
//etc
}
}
}
It prints 1 over and over again because goto makes it run from start (that is at the beginning of the program) so you are doing the same thing every time. There is no loop counter, you always work on the same item1 object.
Looks like you need to store a number of some values - use array instead of manually declaring 10 items. Also you need to loop over that array then.
I recommend you to read some decent book or take a look at some tutorials.

Dynamic memory in C with struct

I've got this code, but it doesnt work, what's wrong?
I try to make massive of struct with dynamic size(C language)
after the second use of add_sala(); in main function Windows close programm.
Please help to solve this problem! Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
char trash[50];
int dyn_sala_id=1;
typedef struct
{
int id;
char number[6];
int persons;
char tech_inf[256];
} sala;
sala *sala_;
int add_sala()
{
int persons;
char number[6], tech_inf[256];
sala_ = (sala*)realloc(sala_,dyn_sala_id * sizeof(sala));
printf("Wpisz numer sali(max. 5 znakow): ");
fgets(number,6,stdin);
if(strlen(number)>5)
{
printf("Numer musi byc nie wiecej, niz 5 znakow!\n");
fflush(stdin);
add_sala();
return 0;
}
printf("Wpisz ilosc osob, ktora wmiesci sie w sale(max. 1000 osob): ");
scanf("%d", &persons);
if(persons==0 || persons>1000)
{
printf("Nie wolno wprowadzic litery oraz max. ilosc osob to 1000\n");
fflush(stdin);
add_sala();
return 0;
}
printf("Wpisz info o wyposazeniu sali(max. 255 znakow): ");
fgets(trash,50,stdin);
fgets(tech_inf,256,stdin);
if(strlen(tech_inf)>255)
{
printf("Info musi byc nie wiecej, niz 255 znakow!\n");
fflush(stdin);
add_sala();
return 0;
}
sala_[dyn_sala_id].id = dyn_sala_id;
strncpy(sala_[dyn_sala_id].number, number, 6);
sala_[dyn_sala_id].persons = persons;
strncpy(sala_[dyn_sala_id].tech_inf, tech_inf, 256);
printf("\nSala zostala dodana!\n\n");
printf("%d, %d, %s, %s",dyn_sala_id, persons, number, tech_inf);
dyn_sala_id+=1;
return 0;
}
int main()
{
add_sala();
printf("%s",sala_[1].number);
add_sala();
printf("'%s'",sala_[1].number);
printf("'%s'",sala_[2].number);
return 0;
}
Arrays in C are indexed from 0, so in main() the array indexing is off by 1.
add_sala();
printf("%s",sala_[1].number);
add_sala();
printf("'%s'",sala_[1].number);
printf("'%s'",sala_[2].number);
Also in the function add_sala() it is clear that the first time it is called you have the global
int dyn_sala_id=1;
which you use to allocate memory for one record with
sala_ = (sala*)realloc(sala_,dyn_sala_id * sizeof(sala));
but a bit further down, the indexing is again off by 1, where there is plainly only one array element
sala_[dyn_sala_id].id = dyn_sala_id;
Then, in that same function (although I can't read the error messages) it seems strange that after an apparent bad input, you recurse the function. Also, you have undefined behaviour with
fflush(stdin);
and I have not looked further, because the code will not work.

Program for Typedef Structure look up Keeps Crashing

I am trying to write a console application where the user enters in a City then the program looks up the city name in a typedef structure and proceeds to display the city's latitude and longitude coordinates. I have zero errors displaying for the program, but it seems that each time I enter a city and press enter the console application will display city not found and then close out. Here is the code with a few of the cities from the typdef structure (full structure contains 200 variables).
#include <stdio.h>
#include <string.h>
#define MAX_CITY_LEN 35
typedef struct {
char name[MAX_CITY_LEN];
float latitude;
float longitude;
} PLACE_T;
PLACE_T places[] =
{{"Aberdeen,Scotland",57.15,-2.15},
{"Adelaide,Australia",-34.92,138.6},
{"Albany,NY,USA",42.67,-73.75},
{"Albuquerque,NM,USA",35.08,-106.65},
{"Algiers,Algeria",36.83,3.0},
{"Amarillo,TX,USA",35.18,-101.83},
{"Amsterdam,Netherlands",52.37,4.88},
{"Anchorage,AK,USA",61.22,-149.9},
{"Ankara,Turkey",39.92,32.92},
{"Asuncion,Paraguay",-25.25,-57.67},
{"Athens,Greece",37.97,23.72},
{"Atlanta,GA,USA",33.75,-84.38},
{"Auckland,New Zealand",-36.87,174.75},
{"",999,999}};
int main()
{
int j;
int found=0;
char city[MAX_CITY_LEN];
int citySize=(sizeof(places)/sizeof(places[0]));
printf("Please Enter the Name of a City: \n");
scanf_s("%s", &city, MAX_CITY_LEN,stdin);
city[strlen(city)-1]='\0';
for(j=0;j<citySize;j++)
{
if(strcmp(city,places[j].name)==0)
{
found=1;
printf("lat = %f, long = %f",places[j].latitude,places[j].longitude);
break;
}
}
if(!found)
printf("City not found");
return 0;
}
Thanks!
try this
int found=0;
char city[MAX_CITY_LEN];
int citySize=(sizeof(places)/sizeof(places[0]));
int j, len;
printf("Please Enter the Name of a City: \n");
scanf_s("%34s", city, MAX_CITY_LEN);
len = strlen(city);
for(j=0;j<citySize;j++){
if(strncmp(city, places[j].name, len)==0 && places[j].name[len]==','){
found=1;
printf("lat = %f, long = %f",places[j].latitude,places[j].longitude);
break;
}
}
if(!found)
printf("City not found");

Resources