I am making a little game, in which I would like to make string comparison, to compare 2 strings, and if they are equal I would like to print it out.:
I included stdio.h and string.h I use codeblocks.
I tried to run the code, but codeblocks says
code.exe has stopped working
Could someone help me?
The code looks like this:
int main(void)
{
typedef struct {
char * name;
} player;
int player_number;
player players[100];
printf("What's your name?\n");
scanf("%s",&players[0].name);
printf("How many players are playing the game?\n");
scanf("%d", &player_number);
printf("Can you name your friends?\n");
int d;
for(d=1;d<player_number;d++)
{
scanf("%s", &players[d].name);
}
printf("Who is starting the game?\n");
char * starter;
scanf("%s",&starter);
int e;
for(e=0;e<player_number;e++)
{
if(strncmp(players[e].name, starter, 10)==0)
{
printf("%s is starting the game",starter);
break;
}
}
return 0;
}
You need to have some memory for char *starter to point at.
Here's your mistake.
char * starter;
scanf("%s",&starter);
also here;
typedef struct {
char * name;
}player;
Instead try this;
typedef struct {
char name[100];
}player;
char starter[100];
scanf("%s",starter);
I don't know C that much but shouldn't there be a .name in the first input?
It should look like I suppose:
printf("What's your name?\n");
scanf("%s",&players[0].name);
Related
#include <stdio.h>
int j=0;
struct student
{
int CNE;
char Nom[20];
char Prenom[20];
char Ville[20];
float Note[3];
float Moyenne;
};
void read_struct(struct student stu)
{
stu.Moyenne=0;
printf("Nom de l'etudiant:\t ");
scanf(" %s",stu.Nom);
printf("Prenom de l'etudiant:\t ");
scanf(" %s",stu.Prenom);
printf("CNE de l'etudiant:\t ");
scanf("%d",&stu.CNE);
}
int main()
{
struct student stu[10];
read_struct(stu[0]);
read_struct(stu[1]);
printf("%s \n %s \n",stu[0].Nom,stu[1].Nom);
printf("%d \n %d",stu[0].CNE,stu[1].CNE);
}
I m getting some weird output after compiling, the input from users are not saved in struct after calling them back.( sorry for my english)
Look at how this function is defined:
void read_struct(struct student stu) {
...
}
When you call this function, it passes in a copy of the struct student, so the function does its work to fill in the copy rather than the original.
You may want to have this function take in a pointer to the struct student:
void read_struct(struct student* stu) {
/* You'll need to change things here */
}
read_student(&stu[0]);
read_student(&stu[1]);
Hope this helps!
I have the following struct:
struct visitordata {
char name[25];
char email[25];
int id;
char reg_time[9];
};
I want to write data from these visitors into a file. I'm reading this data from the CLI:
int main(int argc, char** argv)
{
struct visitordata mydata;
char name[25], email[25], eventid[10], c;
printf("Enter name: ");
scanf("%s", name);
strcpy(mydata.name, name);
//etc.
}
Now when I try to write to the file:
lseek(handle, -sizeof(mydata.name), SEEK_END);
fputs(handle, &mydata.name);
This writes the input the user gave alright, but also some gibberish, since I allocated char name[25]
How can I allocate the char array sizes after reading from the command line? I'm pretty new to this, please don't be harsh. Thank you!
struct visitordata mydata;
memset(mydata, 0, sizeof mydata);
if (scanf("%24s %24s", mydata.name, mydata.email) == 2) {
...
}
The memset fills mydata with 00 bytes, so that your file doesn't contain random data.
When passing an array to a function, only a pointer to its first element is really passed, and this is exactly what scanf expects. Note that for reading an int, you would have to write:
if (scanf("%d", &mydata.id) == 1) {
...
}
Use strlen(name) to get how many character are in the name.
I have a question about passing function to another function which both have structure as arguments. First I created two structures:
typedef struct
{
char name[25],surname[25];int number;
}PLAYER;
typedef struct
{
char nameofteam[25];int numberofplayers;char *players;
}TEAM;
Then I defined a function to read elements of one player:
void readplayer(PLAYER *);
void readplayer(PLAYER *pi)
{
printf("name:");scanf("%s",pi->name);
printf("surname:");scanf("%s",pi->surname);
printf("number of player:");scanf("%d",&pi->number);
}
My question is how to create function which prototype is void readteam(TEAM*) which will read data for one team, but using function readplayer and call it in main()? Here is what I have tried:
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char name[25],surname[25];int number;
}PLAYER;
typedef struct
{
char nameofteam[25];int numberofplayers;char *players;
}TEAM;
void readplayer(PLAYER *pi)
{
printf("name:");scanf("%s",pi->name);
printf("surname:");scanf("%s",pi->surname);
printf("number of player:");scanf("%d",&pi->number);
}
void readteam(TEAM *pt)
{
char players[101];int i;
printf("name of team:");scanf("%s",pt->nameofteam);
printf("number of players in team:");scanf("%d",&pt->numberofplayers);
printf("players:");scanf("%s",players);
pt->players=(char *)calloc(length(players)+1,sizeof(char));
copy(pt->players,players);
for(i=0;i<pt->numberofplayers;i++)
{
printf("%d.",i+1);
readplayer(pt+i);
}
}
void erase(TEAM *);
void erase(TEAM *pt)
{
free(pt->players);
}
int length(char *s)
{
int d=-1;
while(s[++d]);
return d;
}
void copy(char *s1,char *s2)
{
while(*s1++ = *s2++);
}
int main()
{
int i,n;
TEAM *p;
do
{
printf("n=");scanf("%d",&n);
}
while(n<1);
p=(TEAM *)malloc(n * sizeof(TEAM));
for(i=0;i<n;i++)
{
printf("%d.",i+1);readteam(p+i);
}
free(p);
}
This gives me an error at the last input (in compiling, not debugging). Must be because of inappropriate use of dynamic allocation. I didn't use <string.h library. Obviously, only the readteam function has to be in main().
Thanks for the answers.
You are confused on how to store the playsrs. You have created a PLAYER struct, but you never use it. Instead, you insist that players must be a single string.
But it should work like this: You have n teams. Ecah team has m players. All team info is stored in your ´TEAMstruct. All player info is stored in yourPLAYERstruct. Because a team is made up of players, there should be aPLAYER` entry in your struct:
typedef struct {
char name[25];
char surname[25];
int number;
} PLAYER;
typedef struct {
char nameofteam[25];
int numberofplayers;
PLAYER *players;
} TEAM;
Then, when you read players, you read the bare team info in readteam. But you don't read anything about individual players there, because you delegate that to readplayer. Of course, the pointer you pass to that function must be that for a player, not one for a team:
void readplayer(PLAYER * pi)
{
printf("name:");
scanf("%s", pi->name);
printf("surname:");
scanf("%s", pi->surname);
printf("number of player:");
scanf("%d", &pi->number);
}
void readteam(TEAM * pt)
{
int i;
printf("name of team:");
scanf("%s", pt->nameofteam);
printf("number of players in team:");
scanf("%d", &pt->numberofplayers);
pt->players = calloc(pt->numberofplayers, sizeof(*pt->players));
for (i = 0; i < pt->numberofplayers; i++) {
printf("Player %d:\n", i + 1);
readplayer(pt->players + i);
}
}
Your cast to (char *) hides the warning about incompatible types. You should cast only when you know what you're doing. In this simple program, you don't need casts.
In your original code, there are warnings about "implicit declarations". These concern your copy and length functions. (By the way, what's wrong with strlen and strcpy?) You should move these functions to the top so that they are declared before they are called. ALternatively, provide prototypes at the beginning of your code or in a header file, which you #include at the top. (But now that you read into PLAYER structs, these functions are no longer needed.)
Trying to pass a struct to a function and browse through it. Is this the correct way to pass a struct to a function? The for loop in the function view() doesn't seem to work. Any help with that too would be appreciated.
My structs:
typedef struct {
char name[20];
employee *list_employees;
int empl_count;
} agency;
typedef struct {
char name[30];
int age;
} employee;
Important pars of the code:
int main()
{
//...
int nmbr_agencies;
agency *list_agencies = malloc(sizeof(agency) * nmbr_agencies);
view(&list_agencies, &nmbr_agencies);
}
void view(agence *ListA[], int *nmbr)
{
int i=0;
for (i = 0; i < *nmbr; i++){
printf("name of agency: %s\n", ListeA[i]->name);
printf("number of employees\n, ListeA[i]->empl_count);
}
}
No.
You should just pass a single array if that is what you have, not pretend (in the call) that you have an array of pointers which you don't have.
Make the function:
void view(const agency *list, size_t number);
and call it like so:
view(list_agencies, nmbr_agencies);
Then inside the function, do direct accesses:
printf("name of agency: %s\n", list[i].name);
since you don't have an array of pointers.
I'm making a simple enrollment system that maintains a database of a collection of computer science students. Each student record contains
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char name[300];
int age;
char course_1[40];
char course_2[40];
char *remarks;
};
struct course
{
char course_title[200];
int cse_num[100];
char instructor[200];
char date[50];
char start_time[50];
char end_time[50];
char location[50];
};
main()
{
int i;
struct course data[11];
FILE *f;
char title[100];
int num[100];
char instructor[100];
char date[100];
char start_time[100];
char end_time[100];
char location[100];
char line[300];
f = fopen("course.dat", "r");
i=0;
while(*fgets(line, 300, f) != '\n')
{
sscanf(line, "%99[^\n]", num);
sscanf(line, "%99[^\n]", title);
sscanf(line, "%99[^\n]", instructor);
sscanf(line, "%99[^\n]", date);
sscanf(line, "%99[^\n]", start_time);
sscanf(line, "%99[^\n]", end_time);
sscanf(line, "%99[^\n]", location);
data[i].cse_num = num // doesn't work
strcpy(data[i].course_title, title);
strcpy(data[i].instructor, instructor);
strcpy(data[i].date, date);
strcpy(data[i].start_time, start_time);
strcpy(data[i].end_time, end_time);
strcpy(data[i].location, location);
i++;
}
fclose(f);
}
My question is how to take in the input from the file since it is 7 lines until a new line is considered. I tried my best to explain this, thanks if you can try to help me guys!! I really focused on this just couldn't figure it out to be honest. This is the file:
Example input:
CSE1001
Research Directions in Computing
Wildes, Richard
W
16:30
17:30
VC 135
Do not forget, you must also strcpy(data[i].course_title, title);
and that goes for all strings.
You are currently doing this: data[i].course_title = title;
Consider the use of scanf. It's a general use function for parsing input from the terminal or from a file with the fscanf variant. It's already a part of the libraries you'd be including, and is similar in formatting to printf which you'll use lots for program output.
You've misdeclared main():
struct course
{
char course_title[200];
int cse_num[100];
char instructor[200];
char date[50];
char start_time[50];
char end_time[50];
char location[50];
}
main()
{
This says that main() returns a struct course. That's wrong.
Add a semi-colon after the structure.
Always provide an explicit type for every function. C99 requires it; it is good practice for C89.
The code should start:
struct course
{
char course_title[200];
int cse_num[100];
char instructor[200];
char date[50];
char start_time[50];
char end_time[50];
char location[50];
};
int main(void)
{
You should be getting warnings from your C compiler about this mistake. If you weren't, you either need to turn warnings on or you need to get a better compiler.
This may or may not be directly related to the other problems you face, but it should be fixed.