take full name and birthday as input to struct in C - c

I have to write program that takes from the user, his full name, number, and his birthday and put them in structure.
then I have to sort the inputs by birthday and display them to the screen.
the problem is when I enter the first Full Name the program crash and doesn't get any full name further.
this what I have approach to so far...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ZNAK{
char full_Name[100];
int number;
int bday[3];
};
int Compare(const void *s1, const void *s2){
struct ZNAK *e1 = (struct ZNAK *)s1;
struct ZNAK *e2 = (struct ZNAK *)s2;
int res = memcmp(e1->bday, e2->bday, 3);
if(res < 1){
return e1->bday - e2->bday;
}
}
int main(){
struct ZNAK arr[4];
for(int i=0; i<4; i++){
printf("\nenter Full Name: ");
fgets(arr[i].full_Name, 100, stdin);
printf("\nEnter Phone Number: ");
scanf("%d", &arr[i].number);
printf("Enter Birth Day: ");
scanf("%d %d %d", &arr[i].bday[0], &arr[i].bday[1], &arr[i].bday[2]);
}
qsort(arr, 4, sizeof(struct ZNAK), Compare);
char last_name[50];
printf("enter the last name: ");
scanf("%s", last_name);
for (int x=0; x<4; x++){
printf("First Name: %s\n", arr[x].full_Name);
printf("Phone Number: %d\n", arr[x].number);
printf("Birth Day: %d %d %d\n", arr[x].bday[0],arr[x].bday[1],arr[x].bday[2] );
}
}

Related

Structure Array Size in C

I am quite new to programming. Memory allocation also, still confuses me. And our professor asked us to make an array of structures wherein users will input the array size. This is to know how many entries will the users enter in the telephone directory.
Here is it so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct TelDirectory {
char name[50];
char address[100];
char tel[20];
};
int main() {
int num, add, del, prev = 0;
char ask;
*//asking for the number of entries*
printf("Number of entries: ");
scanf(" %i", &num);
struct TelDirectory *entry[num];
Input(entry, prev, num);
Display(entry, num);
//Input and Displaying
void Input(struct TelDirectory *entry[], int prev, int num) {
int i;
for (i = prev; i < num; i++) {
entry[i] = (struct TelDirectory *)malloc(sizeof(struct TelDirectory) * num);
printf("\nEnter name (last, first, middle): ");
scanf(" %[^\n]", entry[i]->name);
printf("Enter address: ");
scanf(" %[^\n]", entry[i]->address);
printf("Enter telephone number: ");
scanf(" %[^\n]", entry[i]->tel);
}
void Display(struct TelDirectory *entry[], int num) {
int i, j;
printf("%i\n", num);
struct TelDirectory *temp;
temp = (struct TelDirectory *) malloc(sizeof(struct TelDirectory) * num);
for (i = 0; i < num; i++) {
for (j = i+1; j < num; j++) {
if (strcasecmp(entry[i]->name, entry[j]->name) > 0) {
temp = entry[i];
entry[i] = entry[j];
entry[j] = temp;
}
}
}
printf("\n\t\t\t\t\tInformation\n");
printf("------------------------------------------------------------------------------------------------\n");
printf("Name\t\t\t\t\tAddress\t\t\t\t\tTelephone Number\n");
printf("------------------------------------------------------------------------------------------------\n");
for (i = 0; i < num; i++) {
printf("%-30s\t\t%-30s\t\t%-30s\n", entry[i]->name, entry[i]->address, entry[i]->tel);
}
printf("------------------------------------------------------------------------------------------------\n");
}
And, we will also be asking the users if they will update the directory through either INSERTING more entries or DELETING entries. Is it possible to change the structure array size on run time?

Structures and arrays in C

I must create a program focusing on the use of structures.
The program will ask the user to enter the number of students for whom he will later add specific information (name, surname, average grade).
For some reason, when I start the the program, I enter the number of students i want, after that I enter the info of the first student and then the program terminates.
Here's what I've tried.
#include <stdio.h>
#include <stdlib.h>
struct student
{
char *name;
char *surname;
float *average;
};
void inputs(struct student *a, int size);
int main()
{
int size;
printf("Enter the number of students: ");
scanf("%d", &size);
struct student *data;
data = (struct student *)malloc(size*sizeof(struct student));
if(data==NULL) {
printf("Cannot allocate memory. The program will now terminate.");
return -1;
}
inputs (data, size);
return 0;
}
void inputs(struct student *a, int size)
{
int j=0;
int i;
for(i=0; i<size; i++) {
printf("Enter the name of the student number %d: ", j+1);
scanf("%s", a->name);
printf("Enter the surname of the student number %d: ", j+1);
scanf("%s", a->surname);
printf("Enter the average grade of the student number %d: ", j+1);
scanf("%f", a->average);
j++;
a++;
}
}
This code works:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char name[30];
char surname[30];
float average;
};
void inputs(struct student *a, int size);
int main()
{
int size;
int i=0; // we need i
char string[100];
printf("Enter the number of students: ");
// scanf can be tricky lets use fgets
fgets( string , 99 , stdin );
size= atoi(string);
struct student **data; // array of struct
// get memory for the pointers to pointer
data=(struct student **) calloc( size , sizeof(char *));
// now each struct need space
for( i=0 ; i<size ; i++)
{
data[i] = malloc(sizeof(struct student));
if(data[i]==NULL) {
printf("Cannot allocate memory. The program will now terminate.");
return -1;
}// end if
}// end for loop
// input each student
for( i=0 ; i<size ; i++)
inputs (data[i], i );
return 0;
}
void inputs(struct student *a, int num)
{
int j=0;
char string[100];
// scanf can be tricky lets use fgets
printf("Enter the name of the student number %d: ", num+1);
fgets(a->name, 29, stdin);
printf("Enter the surname of the student number %d: ", num+1);
fgets( a->surname , 29, stdin);
printf("Enter the average grade of the student number %d: ", num+1);
fgets( string , 99, stdin);
a->average=atof(string);
}

I'm trying to create a program with a loop that prompts the user to enter data in the array elements

I'm trying to create a program with a loop that prompts the user to enter data in the array elements. And when the user no longer can enter data, print to screen the data entered in a last in, first out order.
And this is my attempt...
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
struct Name_Age
{
char Name[10];
int Age;
};
void printMe(struct Name_Age info)
{
printf("Name: %s\n", info.Name);
printf("Age: %d\n", info.Age);
}
int main()
{
int size = 0, i = 0, j = 0;
struct Name_Age * array_ptr = (struct Name_Age*)malloc((size + 1)* sizeof(struct Name_Age));
struct Name_Age myInfo = *array_ptr;
printf("Enter size of array: ");
scanf("%d\n", size);
for (i = 0; i < size; ++i)
{
printf("Enter Name: \n");
scanf("%s\n", myInfo.Name);
printf("Enter Age: \n");
scanf("%d\n", myInfo.Age);
}
printMe(myInfo);
return 0;
};
First, scanf("%d", &size) replace scanf("%d\n", size), put &size instead of size as argument(You need an address), and put the malloc things after this line of code, because you need an exact size value before malloc. Same thing with all the scanf stuffs.
As you want to print out all your input names and ages in order, I changed your code like this:
#include <stdio.h>
#include <stdlib.h>
struct Name_Age
{
char Name[10];
int Age;
};
void printMe(struct Name_Age *infoList, int size)
{
int i;
for (i = size-1; i >= 0; --i)
{
printf("Name: %s\n", infoList[i].Name);
printf("Age: %d\n", infoList[i].Age);
printf("\n");
}
}
int main()
{
int size, i;
printf("Enter size of array: ");
scanf("%d", &size);
struct Name_Age * array_ptr = (struct Name_Age*)malloc(size* sizeof(struct Name_Age));
for (i = 0; i < size; ++i)
{
printf("Enter Name: \n");
scanf("%s", array_ptr[i].Name);
printf("Enter Age: \n");
scanf("%d", &array_ptr[i].Age);
}
printMe(array_ptr, size);
return 0;
}
Try to test and compare with your code, questions are welcome.
You can use fgets to read strings and cast the string to digits with atoi. The storage is not complete, but with my changes you can read to structs and specify the size of the loop (TODO: Save a list of structs so that you actually can print a list of structs that you have specified.)
#include <stdio.h>
#include <stdlib.h>
struct Name_Age {
char Name[10];
int Age;
};
void printMe(struct Name_Age info) {
printf("Name: %s\n", info.Name);
printf("Age: %d\n", info.Age);
}
int main() {
int size = 0, i = 0, j = 0;
struct Name_Age *array_ptr = malloc((size + 1) * sizeof(struct Name_Age));
struct Name_Age myInfo = *array_ptr;
printf("Enter size of array: ");
char tmp[10];
fgets(tmp, 10,stdin);
size = atoi(tmp);
for (i = 0; i < size; ++i) {
printf("Enter Name: ");
fgets(myInfo.Name, 10,stdin);
printf("Enter Age: ");
fgets(tmp, 10,stdin);
myInfo.Age = atoi(tmp);
}
printMe(myInfo);
return 0;
};
Test
$ ./a.out
Enter size of array: 2
Enter Name: Superman
Enter Age: 25
Enter Name: Batman
Enter Age: 27
Name: Batman
Age: 27
See also this question about fgets fgets how to read int
Obviously using fgets() is a much better approach, but making the fewest amount of changes as possible to your code and still achieving your result is the following:
#include <stdio.h>
#include <stdlib.h>
struct Name_Age
{
char Name[10];
int Age;
};
void printMe(struct Name_Age *info);
int main(void)
{
int size, i;
struct Name_Age *array_ptr;
printf("Enter size of array: ");
scanf("%d", &size);
array_ptr = malloc(size * sizeof *array_ptr);
for (i = 0; i < size; ++i)
{
printf("Enter Name: ");
scanf(" %s", array_ptr[i].Name);
printf("Enter Age: ");
scanf("%d", &array_ptr[i].Age);
printf("\n");
}
for (i = 0; i < size; ++i)
printMe(&array_ptr[i]);
return 0;
}
void printMe(struct Name_Age *info)
{
printf("Name: %s\n", info->Name);
printf("Age: %d\n", info->Age);
}
Note that passing the struct by pointer to the function should be faster, note that you don't need the myInfo struct; you can just directly modify the elements of the array. Note the space before %s in the scanf() line, this is to discard any whitespace (including \n), this is done automatically for %d so it is only necessary for your strings (technically not the first iteration of the loop, but it doesn't fail if no whitespace is found). If you have any questions about why I made the changes I did, please feel free to comment on this answer!
example of fix
//struct Name_Age myInfo = *array_ptr;//Not necessary
printf("Enter size of array: ");
scanf("%d", &size);//remove `\n`, add `&`
//To ensure after the size has been determined
struct Name_Age * array_ptr = (struct Name_Age*)malloc(size * sizeof(struct Name_Age));//Cast is not necessary in C
for (i = 0; i < size; ++i)
{
printf("Enter Name: \n");
scanf("%s", array_ptr[i].Name);//remove `\n`
printf("Enter Age: \n");
scanf("%d", &array_ptr[i].Age);//need `&`
}
for (i = 0; i < size; ++i)
printMe(array_ptr[i]);

Properly buffering and validating string

I am getting a bit of an error in my program, it is somewhat basic but here is my error: I am trying to properly input a series of strings into a structure and it seems like user input is ending at the wrong spot.
#include <stdio.h>
#include <string.h>
#define buffer 256
struct Fisher
{
char SSN[8]; //9 digit max
char First_Name[12];
char Last_Name[15];
char Phone[10];
char Email[35];
}typedef Fisher;
void getFisher(Fisher* pfisher, int i);
void dispFisher(Fisher* pfisher, int i);
void Fisherman_Menu(Fisher* pfisher, int i);
int main()
{
int i = 0;
Fisher fisherarray[3];
Fisher* pfisher = &fisherarray[0];
Fisherman_Menu(pfisher, i);
return 0;
}
void Fisherman_Menu(Fisher* pfisher, int i)
{
for(;;)
{
fflush(stdin);
printf("-1-Register Fisherman\n");
printf("-2-Search Fisherman\n");
printf("-3-Go back to Main Menu\n");
fflush(stdin);
int choice=0;
scanf(" %d", &choice);
if (choice == 1)
{
fflush(stdin);
getFisher(pfisher, i);
i++;
}
if (choice == 2)
{
fflush(stdin);
dispFisher(pfisher, i);
}
else if (choice == 3)
{
break; /* Break out of loop */
}
else
printf("Anything else?\n");
}
/* When this function returns, you get back to the main menu */
}
void getFisher(Fisher* pfisher, int i)
{
char input[buffer];
char* pinput = NULL;
//===============================
printf("Enter Social Security Number: ");
pinput = fgets(input, buffer, stdin);//validate
strcpy((pfisher+i)->SSN, pinput);
//(pfisher+i)->SSN = atoi(pinput);
//==============================
printf("Enter first name: ");
pinput = fgets(input, buffer, stdin);//validates name
strcpy((pfisher+i)->First_Name, pinput);
//==============================
printf("Enter last name: ");
pinput = fgets(input, buffer, stdin);//validates name
strcpy((pfisher+i)->Last_Name, pinput);
//==============================
printf("Enter phone number as a 10 digit number (without any
dashes or spaces): ");
pinput = fgets(input, buffer, stdin);//validate phone
strcpy((pfisher+i)->Phone, pinput);
//(pfisher+i)->Phone = atoi(pinput);
//===============================
printf("Enter email address: ");
pinput = fgets(input, 20, stdin);//validates name
strcpy((pfisher+i)->Email, pinput);
}
void dispFisher(Fisher* pfisher, int i)
{
int len;
int pen;
printf("ssn is equal to: %s\n", ((pfisher+0)->SSN));
printf("First Name is equal to: %s\n", ((pfisher+0)->First_Name));
printf("ssn is equal to: %s\n", ((pfisher+1)->SSN));
printf("First Name is equal to: %s\n", ((pfisher+1)->First_Name));
len = strlen(((pfisher+0)->SSN));
printf("string length = %d\n", len);
pen = strlen(((pfisher+1)->SSN));
printf("string length = %d\n", len);
/*
int arr = 0;
char searchSSN[9];
printf("Enter SSN: ");
scanf("%s", &searchSSN);
for(arr = 0; arr < i; arr++)
{
if(strcmp(&searchSSN, (pfisher+arr)->SSN) == 0)
{
//printf("I is equal to: %s\n", (pfisher+i)->SSN);
printf("Fisher\n");
printf("-------------------------------------------\n");
printf("%d \n %s \n %s \n %s \n %s ", (pfisher + arr)->SSN, ((pfisher+arr)->First_Name), ((pfisher+arr)->Last_Name), ((pfisher+arr)->Phone), (pfisher+arr)->Email);
printf("-------------------------------------------\n");
}
}
*/
}
My output for SSN is getting messed up and becoming "123456789(FirstName string)
example: 123456789TurboTurkey
How can I clean up the length of the string and get in the SSN properly
There is no enough space to store 9 digit in SSN. If you want to store 9 digit in it, its size should be 10 (9 digit + NULL). If there is no NULL charactor, it will read near by locations too..

Can't read array of structure in C

I am trying to read members of an object like in the code below.
The issue is that the code can't read the second member (car[i].model) in the array and the third one (car[i].price), only the first one (car[i].manufacturer).
#include <stdio.h>
#include <conio.h>
struct machine
{
int price;
char manufacturer[30];
char model[30];
};
int main()
{
int i = 0, n;
printf("Introduce number of cars: ");
scanf_s("%d", &n);
struct machine car[100];
for (i = 0; i < n; i++)
{
printf("Data of the car nr. %d:\n", i+1);
printf("Manufacturer: ");
scanf_s("%s", car[i].manufacturer);
printf("Model: ");
scanf_s("%s", car[i].model); printf("\n");
printf("Price: ");
scanf_s("%d", &car[i].price); printf("\n");
}
for (i = 0; i < n; i++)
{
printf("Data of the car nr. %d:\n", i + 1);
printf("Manufacturer: %s\n", car[i].manufacturer);
printf("Manufacturer: %s\n", car[i].manufacturer);
printf("Model: %s\n", car[i].model);
printf("Price %d\n", car[i].price);
}
_getch();
}
scanf_s requires the buffer size to be specified for input parameters with format %s. The buffer size includes the terminating null. Adapt your code like this:
struct machine
{
int price;
char manufacturer[30];
char model[30];
};
struct machine car[100];
....
scanf_s("%s", car[i].manufacturer, 30 );
// ^^ buffer size
....
scanf_s("%s", car[i].model, 30 );
// ^^ buffer size
....
scanf_s("%d", &car[i].price); // no buffer size

Resources