/* It is not entering data into the third scanf() statement .*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void)
{
struct book
{
char name;
int pages;
float price;
};
struct book a1,a2,a3,a4;
printf("Enter data into 3 books\n");
scanf("%c %d %f",&a1.name,&a1.pages,&a1.price);
scanf("%c %d %f",&a2.name,&a2.pages,&a2.price);
scanf("%c %d %f",&a3.name,&a3.pages,&a3.price);
printf(" you entered:\n");
printf("\n%c %d %f",a1.name,a1.pages,a1.price);
printf("\n%c %d %f",a2.name,a2.pages,a2.price);
printf("\n%c %d %f",a3.name,a3.pages,a3.price);
getch();
}
You want to use strings, not single characters:
int main(void)
{
struct book
{
char name[100];
int pages;
float price;
};
struct book a1,a2,a3,a4;
printf("Enter data into 3 books\n");
scanf("%s %d %f",&a1.name,&a1.pages,&a1.price);
scanf("%s %d %f",&a2.name,&a2.pages,&a2.price);
scanf("%s %d %f",&a3.name,&a3.pages,&a3.price);
printf(" you entered:\n");
printf("%s %d %f\n",a1.name,a1.pages,a1.price);
printf("%s %d %f\n",a2.name,a2.pages,a2.price);
printf("%s %d %f\n",a3.name,a3.pages,a3.price);
return 0;
}
But note this is prone to buffer overflows, and won't deal correctly with book names that contain spaces.
you are wanting a string as a name, while you are giving a %c specifier for the input which expects a character.
so either use %s for a string input.
or better use some string function like gets()
gets (a1.name);
scanf ( %d %f",&a1.pages,&a1.price);
And again to remind that you must be careful with size of string(char array) to avoid stack overflows.
Thanks
Alok.Kr.
Related
I start to study C language, and after a short break, I start to study it again and I'm in stuck with the new updates. This code doesn't works. I can't understand how to do now. If I change from scanf to scanf_s it's doesn't work anyway. I also try to change a declaration of the type. Is someone here who can explain how can I change this code and what to use now, scanf or scanf_s, and on what occasions to use them. Thanks in advance.
#include <stdio.h>
#define N 256
typedef struct
{
char name[N];
char surname[N];
char street[N];
char city[N];
char district[3];
int n_house;
int day, month, year;
} person;
main()
{
person s;
printf("ask name\n");
scanf("%s", s.name);
printf("ask sur\n");
scanf("%s", s.surname);
printf("ask wh h lives\n");
scanf("%s %s %s %d", s.street, s.city, s.district, &s.n_house);
printf("ask bd\n");
scanf("%d/%d/%d", &s.day, &s.month, &s.year);
printf("personal data of the person : \n");
printf("%s %s\n Nato il %d %d %d \n Vive in %s %s %s %d", s.name, s.surname, s.day, s.month, s.year, s.street, s.city, s.district, s.n_house);
}
The error is C4996 scanf. This function or variable may be unsafe. Consider using scanf_s instead. To disable depreciation, use _CTR_SECURE_NO_WARNINGS.
I compiled your program with some little modifications:
#include <stdio.h>
#define N 256
typedef struct
{
char name[N];
char surname[N];
char street[N];
char city[N];
char district[3];
int n_house;
int day, month, year;
} person;
void main(void)
{
person s;
printf("ask name\n");
scanf("%s", s.name);
printf("ask sur\n");
scanf("%s", s.surname);
printf("ask wh h lives\n");
scanf("%s %s %s %d", s.street, s.city, s.district, &s.n_house);
printf("ask bd\n");
scanf("%d/%d/%d", &s.day, &s.month, &s.year);
printf("personal data of the person : \n");
printf("%s %s\n Nato il %d %d %d\nVive in %s %s %s %d\n",
s.name, s.surname,
s.day, s.month, s.year,
s.street, s.city, s.district, s.n_house);
return;
}
And works with the input:
ask name
george
ask sur
mac
ask wh h lives
barcelona sskk ksl 123
ask bd
12/13/1111
personal data of the person :
george mac
Nato il 12 13 1111
Vive in barcelona sskk ksl 123
Is this the kind of input that you are trying ?
I want to sort employee data based on names. The sorting function works but provides strange characters in the output??
The last printf statement is the culprit I guess (bottom of the code)
If someone could help, that would be appreciated.
Thanks
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char name[25];
char firstname[25];
char job;
float hrs;
float rate;
} employee;
int main()
{
FILE *fp = fopen("employee.dat", "r");
employee staff[30];
int i = 0;
if (fp == NULL){
printf("not working\n");
exit(1);
}
fscanf(fp, "%s %s %c %f %f", staff[i].name, staff[i].firstname, &staff[i].job, &staff[i].hrs, &staff[i].rate);
while(!feof(fp))
{
printf("%s %s %c %4.1f %4.1f \n", staff[i].name, staff[i].firstname, staff[i].job, staff[i].hrs, staff[i].rate);
i++;
fscanf(fp, "%s %s %c %f %f", staff[i].name, staff[i].firstname, &staff[i].job, &staff[i].hrs, &staff[i].rate);
}
fclose(fp);
// qsort struct function for comparing names
int struct_cmp_by_name(const void *a, const void *b)
{
employee *ia = (employee *)a;
employee *ib = (employee *)b;
return strcmp(ia->name, ib->name);
}
int structs_len;
structs_len = sizeof(staff) / sizeof(employee);
// sort on names
qsort(staff, structs_len, sizeof(employee), struct_cmp_by_name);
//output with strange charaters???
for(i=0; i<structs_len; i++){
printf("%s %s %c %4.1f %4.1f \n", staff[i].name, staff[i].firstname, staff[i].job, staff[i].hrs, staff[i].rate);
}
return(0);
}
I am expecting a regular output of the printf statement.
The first printf works fine but the one after the qsort provides strange characters instead??
The most likely culprit of your problem is that you sort the whole array, even when maybe not all elements are initialized.
If the file contains less than the 30 elements you have for the array, parts of the array will be uninitialized with indeterminate contents (which may sometimes seem random or like "garbage"). You should not use those when sorting, only sort the data you actually have read from the file.
You have the number of valid and initialized elements in the array in the variable i which you should use instead:
qsort(staff, i, sizeof(employee), struct_cmp_by_name);
You have the same problem when printing the data: You print the whole array, including the uninitialized parts.
I suggest you create a new variable for the number of valid elements, suitable named, instead of the generic i that you now use.
This question already has answers here:
How do I return an array of struct from a function?
(3 answers)
Closed 5 years ago.
i'm trying to pass an array of stract to a function, but i just gave up. i´m trying to store data inside the structure function "fun()", so i can later pull up the data in function lo(); when ever i need too.
#include<stdio.h>
#include<string.h>
struct Operador
{
char nome[32];
char telefone[15];
char idade[3];
};
struct Operador* fun( ) {// im using this function to store the data
struct Operador* pItems = malloc(3 * sizeof(struct Operador));//is it necessary to use malloc
int n;
printf(" give nome: ");
scanf("%s", pItems->nome);
printf(" give telefone: ");
scanf("%s", pItems->telefone);
printf(" give age: ");
scanf("%s", pItems->idade);
return pItems;
}
//*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-
void lo(struct Operador pItems)//and this function to display the data
{
struct Operador Items = pItems;
int j;
printf("\n\n");
printf("Name is: %s \n", Items->nome);
printf("telefone is: %s \n", Items->telefone);
printf("age is: %s \n", Items->idade);
printf("\n\n");
return pItems;
}
main()
{
fun(); //here i call out the function for the user to type in information
printf("\n\n click any key to see data");
system("pause");
lo(); // and this function is supposed to display information
}
You probably want this:
#include<stdio.h>
#include<string.h>
struct Operador
{
char nome[32];
char telefone[15];
char idade[3];
};
struct Operador *fun()
{
struct Operador* pItems = malloc(sizeof(struct Operador)); // allocate space for ONE structure
printf(" give nome: ");
scanf("%s", pItems->nome);
printf(" give telefone: ");
scanf("%s", pItems->telefone);
printf(" give age: ");
scanf("%s", pItems->idade);
return pItems;
}
void lo(struct Operador *pItems)
{
printf("\n\n");
printf("Name is: %s \n", pItems->nome);
printf("telefone is: %s \n", pItems->telefone);
printf("age is: %s \n", pItems->idade);
printf("\n\n");
}
int main()
{
struct Operador *op = fun(); // op points to new structure filled in by user
lo(op); // display structure
free(op); // free structure
system("pause");
}
This code is still bad (no error checking, usage of %s format specifier without length limitation, poor choice of names, age field as a string instead of an int and probably a few more), but it works as expected.
This error message is shown in every software.
it has stop working check online solution and close the program
There is no error in code but when I run this program I got this error and I found that most of the program where user input is required it stops working. I have used code blocks, C free, dev C++
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[10];
} stu1 = {100, "ram"};
main()
{
struct student stu2;
printf("2nd student name is: %s \n",stu1.name);
printf("second student roll no: %s \n ",stu1.roll);
printf("enter second student data ");
scanf("%d", &stu2.roll);
printf("enter second student name ");
scanf("%s",&stu2.name);
printf("2nd student name is: %s \n",stu2.name);
printf("second student roll no: %s \n ",stu2.roll);
getch();
}
Image with error message: https://i.stack.imgur.com/ZZsAU.png
#include<stdio.h>
struct student
{
int roll;
char name[10];
}stu1 = {100, "ram"};
int main()
{
struct student stu2;
printf("2nd student name is: %s \n",stu1.name);
printf("second student roll no: %d \n ",stu1.roll); // line 1
printf("enter second student data ");
scanf("%d", &stu2.roll);
printf("enter second student name ");
scanf("%s",stu2.name); // line 2
printf("2nd student name is: %s \n",stu2.name);
printf("second student roll no: %d \n ",stu2.roll); // line 3
return 0;
}
I have corrected the code like this. Lines 1, 2 and 3 was the problem I think. While changing those lines as shown above code no longer runs to segmentation fault.
NB : Don't use scanf for user input. If you use scanf at all, always check its return value. scanf %s is a bug: It can't prevent buffer overflows from longer input.
This could be another approach:
#include<stdio.h>
#include<conio.h>
typedef struct
{
int roll;
char *name;
} studentT;
int main()
{
studentT stu1, stu2;
stu1.roll = 100;
stu1.name = "ram";
printf("2nd student name is: %s \n",stu1.name);
printf("second student roll no: %d \n ",stu1.roll);
printf("enter second student data ");
scanf("%d", &stu2.roll);
printf("enter second student name ");
scanf("%s",&stu2.name);
printf("2nd student name is: %s \n",stu2.name);
printf("second student roll no: %d \n ",stu2.roll);
getch();
}
I'm trying to make a structure with the three variables int age, int siblings, and char[] hometown but it's not letting me insert the hometown string when the program is run. The integers work properly but it'll just skip right over the array and leave it blank. I've tried using gets and fgets but nothing seems to be working.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
struct person{
int age;
int s;
char hometown[20];
}p;
printf("Age: ");
scanf("%d",&p.age);
printf("Siblings: ");
scanf("%d",&p.s);
printf("Hometown: \n");
fgets(p.hometown, 20, stdin);
printf("Age \t Siblings \t Hometown\n");
printf("%d \t %d \t %s\n",p.age,p.s,p.hometown);
}
The local variable might already contains garbage.
Try to memset before you use for string,
So that proper null will be terminated.
Try to acquire your input with following scan(%s, p.hometown);
For strings no need of & for collecting the string.
If you still face the issue, please let me know.
This also works for town names that contains a space and is protected against buffer overflow too.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define HOMETOWN_SIZE 20
int main(){
struct person {
int age;
int s;
char hometown[HOMETOWN_SIZE + 1]; //+ 1 for terminating null character
} p;
printf("Age: ");
scanf("%d", &p.age);
printf("Siblings: ");
scanf("%d", &p.s);
printf("Hometown: \n");
getchar(); //just for consume new line from previous scanf
fgets(p.hometown, HOMETOWN_SIZE + 1, stdin); //fgets reads n-1 characters
//don't want new line in hometown name
if (p.hometown[strlen(p.hometown) - 1] == '\n')
p.hometown[strlen(p.hometown) - 1] = '\0';
printf("Age \t Siblings \t Hometown\n");
printf("%d \t %d \t %s\n", p.age, p.s, p.hometown);
return 0;
}
Try flushing the Buffer memory before taking character array input
like this
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
struct person{
int age;
int s;
char hometown[20];
}p;
printf("Age: ");
scanf("%d",&p.age);
printf("Siblings: ");
scanf("%d",&p.s);
printf("Hometown: \n");
fflush(stdin);
fgets(p.hometown, 20, stdin);
printf("Age \t Siblings \t Hometown\n");
printf("%d \t %d \t %s\n",p.age,p.s,p.hometown);
}