I want to pass a structure as an argument in my function but having some problems in passing it.
The code without functions is :
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[100];
char roll[100];
int marks[5];
}a[3];
typedef struct student s;
void stuwise(s *a);
void subwise(s *a);
int i;
int j;
int m;
int main(int argc, char const *argv[])
{
for(i=0;i<3;i++)
{
printf("Enter Student %d Name \n",i+1);
fgets(a[i].name,100,stdin);
printf("Enter Student %d Roll Number \n",i+1);
fgets(a[i].roll,10,stdin);
for(m=0;m<5;m++)
{
printf("Enter Student %d Marks %d\n",i+1,m+1);
scanf("%d",&(a[i].marks[m]));
getchar();
}
}
printf("Student wise list :\n");
for(j=0;j<3;j++)
{
for(m=0;m<5;m++)
{
printf("Student %d Marks %d ",j+1,m+1);
printf("%d ",(a[j].marks[m]));
printf("\n");
}
printf("\n");
}
printf("Subject wise list :\n");
for(m=0;m<5;m++)
{
for(j=0;j<3;j++)
{
printf("Student %d Marks %d ",j+1,m+1);
printf("%d ",(a[j].marks[m]));
printf("\n");
}
printf("\n");
}
return 0;
}
My attempt at using functions :
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[100];
char roll[100];
int marks[5];
}a[3];
typedef struct student s;
void stuwise(s *a);
void subwise(s *a);
int i;
int j;
int m;
int main(int argc, char const *argv[])
{
for(i=0;i<3;i++)
{
printf("Enter Student %d Name \n",i+1);
fgets(a[i].name,100,stdin);
printf("Enter Student %d Roll Number \n",i+1);
fgets(a[i].roll,10,stdin);
for(m=0;m<5;m++)
{
printf("Enter Student %d Marks %d\n",i+1,m+1);
scanf("%d",&(a[i].marks[m]));
getchar();
}
}
stuwise(s a);
return 0;
}
void stuwise(s *a)
{
printf("Subject wise list :\n");
for(m=0;m<5;m++)
{
for(j=0;j<3;j++)
{
printf("Student %d Marks %d ",j+1,m+1);
printf("%d ",(a[j].marks[m]));
printf("\n");
}
printf("\n");
}
}
void subwise(s *a)
{
printf("Student wise list :\n");
for(j=0;j<3;j++)
{
for(m=0;m<5;m++)
{
printf("Student %d Marks %d ",j+1,m+1);
printf("%d ",(a[j].marks[m]));
printf("\n");
}
printf("\n");
}
}
It isn't working properly as I am getting an error " expected expression before āsā "
stuwise(s a); is the wrong way to call the function. You don't have to specify what type the variable is, since you already said so in the function definition.
Replace with:
stuwise(a);
or
stuwise(&a[0]);
Related
I am getting segmentation error?? PLS HElP
#include <stdio.h>
#include <string.h>
void display(char n2[], int x2[], char c1[], int p);
void display(char n2[], int x2[], char c1[], int p)
{
printf("Student Name : %s \n", n2[p]);
printf("Student Roll No : %d \n", x2[p]);
printf("Student Class : %s \n ", c1[p]);
}
int main()
{
char n[50], c[5], n1;
int y, x[8], x1;
int p1 = 0;
printf("Enter the number of students: \n");
scanf("%d", &y);
fflush(stdin);
for (int i = 0; i < y; i++)
{
fflush(stdin);
printf("Enter the Student Name : \n");
scanf("%s", n[i]);
fflush(stdin);
printf("Enter the Student Class : \n");
scanf(" %s", c[i]);
fflush(stdin);
printf("Enter the Student Roll No : \n");
scanf(" %d", &x[i]);
fflush(stdin);
}
fflush(stdin);
printf("Enter the Student Name and Roll Number :\n");
scanf("%s %d", &n1, &x1);
for (int i = 0; i < y; i++)
{
if ((n[i] == n1) && (x[i] == x1))
{
p1 = i;
}
else
{
printf("No Such Entry!!");
}
}
display(n, x, c, p1);
return 0;
}
The error occurs here:
scanf("%s",c[i]);
You are trying to store a string into a char (n[i]).
You should define n and the others as an array of strings instead of a string, for example:
char n[50][ 50 ], c[50][50], n1[50];
Also, you can't compare strings like you do on this line:
if ((n[i]== n1) && (x[i] == x1))
Use strcmp instead to compare strings.
I created two structs, each of which I defined as a 3rd struct variable. I'm confused about calling those variables in the scanf() function and the printf() function. This is my code so far
#include <stdio.h>
#include <string.h>
#define MAX 5
typedef struct {
char namaJalan[30];
int noRumah;
char kota[30];
char provinsi[30];
} Alamat;
typedef struct {
int tanggal;
int bulan;
int tahun;
} tanggalLahir;
typedef struct {
int noDosen;
int nidn;
char nama[30];
char tempat[30];
Alamat alamat;
tanggalLahir tglLahir;
} dataDosen;
int main() {
Alamat a;
tanggalLahir t;
dataDosen d[MAX];
for (int i = 0; i < MAX; i++) {
printf("\nData dosen ke %d\n", i+1);
printf("No. Dosen: ");
scanf("%d", &d[i].noDosen);
printf("NIDN: ");
scanf("%d", &d[i].nidn);
printf("Nama: ");
scanf("%s", d[i].nama);
printf("Alamat (Jalan - No. - Kota - Provinsi ): ");
I'm confused to define this scanf
//how to define scanf variable made of struct?
scanf("%s%d%s%s", &d[i].alamat);
printf("Tempat lahir: ");
scanf("%s", d[i].tempat);
printf("Tanggal lahir (DD/MM/YYYY): ");
scanf("%d%d%d", &d[i].tglLahir);
}
printf("================ DATA DOSEN ==================\n");
printf("\n");
printf("NO\tNIDN\tNAMA\tALAMAT\tTEMPAT LAHIR\tTANGGAL LAHIR");
And how to define printf variable made of struct?
for (int i = 0; i < MAX; i++) {
printf("%d\t%d\t%s\t%s%d%s\t%s\t%d%d%d\n",
d[i].noDosen, d[i].nidn, d[i].nama, d[i].alamat, d[i].tempat, d[i].tglLahir);
}
return 0;
}
I want to ask you guys, here my code cannot check is my value is exist or not in struct, I've input the value, but none of them enter the if else condition, can anyone help me?
#include <stdio.h>
int main(){
int a,i;
struct data {
char nim[10];
};
struct data batas[100];
printf("TEST1 : "); scanf("%[^\n]s", batas[0].nim);
printf("TEST2 : "); scanf(" %[^\n]s", batas[1].nim);
printf("TEST3 : "); scanf(" %[^\n]s", batas[3].nim);
printf("TEST : "); scanf(" %[^\n]s", batas[a].nim);
for(i=0; i<a; i++){
if (batas[a].nim == batas[i].nim) {
printf("Value exist");
} else {
printf("Value doesn't exist");
}
}
return 0;
}
You can not compare array of chars with the equal operator, instead:
if (strcmp(batas[a].nim, batas[i].nim) == 0)
or
if (!strcmp(batas[a].nim, batas[i].nim))
you need to #include <string.h>
Also, note that you are using a uninitialized.
from what you give, it still can't enter the "Value exist" value. Here are my full line of code.
#include <stdio.h>
#include <string.h>
struct data {
char nim[10];
};
struct data batas[100];
int a=0, b, c, d;
int i, j;
char x[20];
void inputdata()
{
printf("\nInput Data\n");
printf("=======================\n");
printf("NIM : "); scanf("%s", batas[a].nim);
for(i=0; i<a; i++){
if (!strcmp(batas[a].nim, batas[i].nim)) {
strcpy(x, "FLAG");
} else {
strcpy(x, "FLAGX");
}
}
printf("%s", x);
a++;
}
void showdata()
{
j=0;
for(i=0; i<a; i++){
j = j + 1;
printf("\nData-%i", j);
printf("\nNIM : %s", batas[i].nim);
}
}
int main() {
int menu;
do {
printf("\nChoose input = "); scanf("%d", &menu);
switch(menu)
{
case 1 : inputdata(); break;
case 2 : showdata(); break;
}
}while (menu != 3);
return 0;
}
I have to sort this strucure in descending order, based on the "med" variable stored in this strucure, so if students 1 med > students 2 med, then the struct shall be printed as : * student 2 info * then * student 1 info *
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct om
{
char name[20];
int n1,n2,n3;
float med;
} a[3];
int main()
{
int i;
printf("Student's data");
for (i=0; i<3; i++)
{
printf("\nName:");
scanf("%s", a[i].name);
printf("\nGrade at maths:");
scanf("%d", &a[i].n1);
printf("\nGrade at programming:");
scanf("%d", &a[i].n2);
printf("\nGrade at english:");
scanf("%d", &a[i].n3);
a[i].med=(a[i].n1+a[i].n2+a[i].n3)/3;
}
}
Here is an example of how to do this, try to understand how the sorting function works based on some example inputs:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_STUDENTS 3
struct student
{
char name[20];
int math, prog, engl;
double avg;
} students[NUM_STUDENTS];
static int compare_students(const void *s1, const void *s2)
{
double avg1 = ((struct student *)s1)->avg;
double avg2 = ((struct student *)s2)->avg;
return (avg1 > avg2) - (avg1 < avg2);
}
int main()
{
printf("Student data:\n");
for (int i=0; i<NUM_STUDENTS; i++)
{
printf("Name:");
scanf("%s", students[i].name);
printf("Grade at maths:");
scanf("%d", &students[i].math);
printf("Grade at programming:");
scanf("%d", &students[i].prog);
printf("Grade at english:");
scanf("%d", &students[i].engl);
students[i].avg = (double)students[i].math + students[i].prog + students[i].engl) / NUM_STUDENTS;
}
qsort(students, NUM_STUDENTS, sizeof(struct student), compare_students);
for (int i=0; i<NUM_STUDENTS; i++)
{
printf("Student %d: %s\n", i, students[i].name);
}
return 0;
}
I have also tried to make some general improvements, like adding a #define for the number of students, using more readable variable names and using double instead of float variables (using the latter almost never makes sense in practice). Also note that your average calculation was wrong before because the result was truncated.
man_d_sort(struct om a[])
{
if (a[0].med >= a[1].med)
{
if (a[0].med >= a[2].med)
printf("%.2lf", a[0].med);
else
printf("%.2lf", a[2].med);
}
else
{
if (a[1].med >= a[2].med)
printf("%.2lf ", a[1].med);
else
printf("%.2lf ", a[2].med);
}
}
#include <stdio.h>
#include <stdlib.h>
struct fighter
{
char *name, *category;
int weight, age;
};
void enter_data(struct fighter *f, int n);
void display(struct fighter *f, int n);
int main(int argc, char const *argv[])
{
int n;
struct fighter *f;
printf("Enter no. of fighter: "); scanf("%d",&n);
f = (struct fighter *)malloc(n*sizeof(struct fighter));
enter_data(f, n);
display(f, n);
return 0;
for(int i=0; i<n; i++){
free((f+i)->name);
free((f+i)->category);
}
free(f);
}
void enter_data(struct fighter *f, int n){
for(int i=0; i<n; i++){
(f+i)->name=(char *)malloc(40*sizeof(char));
(f+i)->category=(char *)malloc(40*sizeof(char));
fflush(stdin);
printf("Enter name of fighter: ");
scanf("%s[^\n]",(f+i)->name);
//its not working full name, when I'm trying to give full name with space (ie. John Trump) its not working but when I'm giving only first name (ie. John) its working fine...
fflush(stdin);
printf("Enter age of fighter: ");
scanf("%d",&((f+i)->age));
printf("Enter weight of fighter: ");
scanf("%d",&(f+i)->weight);
printf("Enter category of fighter: ");
scanf("%s[^\n]",(f+i)->category);
}
}
void display(struct fighter *f, int n){
for(int i=0; i<n; i++){
printf("\n\n");
printf(" Name of fighter: ");
puts((f+i)->name);
printf(" Age of fighter: ");
printf("%d \n",(f+i)->age);
printf(" Weight of fighter: ");
printf("%d \n",(f+i)->weight);
printf(" Category of fighter: ");
puts((f+i)->category);
}
}
See in enter_data() function in there I have commented my issue...
When I'm giving full name with spaces its not taking rest of the
inputs and directly jump to next iteration of loop. But when I'm
giving first name or name with no spaces then its working fine.
This imgae is a output sceenshot when I'm giving full name.
This imgae is a output sceenshot when I'm giving first name only or name with no space.
See How do you allow spaces to be entered using scanf?
Also, your return 0 should be after loops for freeing resources.