I tried to make a small registration programm dividing it in a header, a source code containing the registration function definition and the main code.
However, i get this error message:
error: expected expression before 'struct'.
What did I do wrong? Please help I'm a beginner. Here's my main code.
#include <stdio.h>
#include <stdlib.h>
#include "registration.h"
int main()
{
int choice;
puts("Press 1 to register");
scanf("%d", &choice);
if(choice==1){
registerUser(struct generalUser user);
}
}
This my header code.
struct generalUser{
char fName[15];
char lName[20];
int id;
int bDay;
int bMonth;
int bYear;
};
struct generalUser user;
void registerUser(struct generalUser user);
Here's my code for defining the function.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void registerUser(struct generalUser user){
srand(time(NULL));
puts("Insert name");
scanf("%s", user.fName);
puts("Insert last name");
scanf("%s", user.lName);
user.id=1 + rand()%9999;
printf("You ID code is %d", &user.id);
printf("Insert date of birth in the following format: dd/mm/yy");
scanf("%d/%d/%d", &user.bDay, &user.bMonth, &user.bYear);
printf("%s %s, id %d, %d/%d/%d", user.fName, user.lName, user.id, user.bDay, user.bMonth, user.bYear);
}
There are two bugs in your source:
printf("You ID code is %d", &user.id); : please remove & because what your are printing is its value not ref
In main registerUser(struct generalUser user);, please remove struct generalUser. You are calling a function having user already declared global.
Related
I want to take a number input from the user. I want it to work as a user defined array of structure.
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main(void)
{
struct Data
{
char Name[10];
int Age;
char Gender;
};
int i, n;
struct Data D[n];
printf(" \n enter the number of user : ");
scanf("%d",&n);
printf("enter the User details :");
for(i=0;i<n;i++)
scanf("%s" "%d" "%c",D[i].Name,&D[i].Age,&D[i].Gender);
return 0;
}
Try using this instead , if you want to declare an array with unknown number of parts , you must read the n value from the user first , and then procced in your programm , quick note : " The struct declaration is an alternatif you can use but the one you used is also correct " , hope I helped you ;)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char Name[10];
int Age;
char Gender;
}Data;
int main()
{
int i, n;
printf(" \n enter the number of users : ");
scanf("%d",&n);
Data D[n];
printf("enter the users details ....\n\n");
for(i=0;i<n;i++){
printf("Enter the user number %d informations : \n",i+1);
scanf("%s %d %c",D[i].Name,&D[i].Age,&D[i].Gender);
}
return 0;
}
This is the output I need. This is what I've got so far. I've only been able to figure out how to add 1 subject, but I need to add 2 more like the picture. I tried to save directly into the array but it didn't work. So i used strcpy.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char subjectID[10];
char subjectName[10];
float marks;
}Subject;
typedef struct {
char Stu_ID[10];
char Stu_Name[30];
Subject subjects;
}Student;
int main()
{
Student a[3];
Student b;
int i;
strcpy(b.Stu_ID, "S100011");
strcpy(b.Stu_Name, "James");
strcpy(b.subjects.subjectID, "TOS10005");
strcpy(b.subjects.subjectName, "Programming");
b.subjects.marks=50;
printf("Student details \n");
printf("ID : %s\t Name : %s\n\n", b.Stu_ID, b.Stu_Name);
printf("Subject ID\t Name\t\t Marks\n");
printf("%s\t %s\t\t %.2f\t \n", b.subjects.subjectID, b.subjects.subjectName, b.subjects.marks);
}
I am having issues in collecting and validating a user's integer input for a grade in my program. the grade is stored in a structure along with its accompanying course name in courseRecord. there should be 2 of these structures created and stored as an array of size 2 within the studentRecord structure, where the data should be stored.
I know this is not my only problem, as I can see by the watches on the debug menu that whilst there is a substructure of a kind in studentRecord, there are not 2 as I intended. for the moment I am only working with the 1 available, and then I intend to adapt the code to include a second.
the specific error of "program recieved signal SIGSEGV, Segmentation fault", which points to when the user first enters any numerical grade leads me to believe that there is a memory allocation error somewhere. I have attempted to use the malloc function, but the fact that it is still not working means I have obviously not used it correctly. I am using the current GNU GCC compiler/linker as is available in the code:blocks software. Here is the code:
main.c
#include <stdlib.h>
#include "defs.h"
int main()
{
getGradeAverage();
return EXIT_SUCCESS;
}
defs.h
#include <stdio.h>
#include <math.h>
#define MAX_LENGTH 40
typedef struct courseRecord
{
char courseName [MAX_LENGTH+1];
int grade;
}courseRecord[2];
typedef struct studentRecord
{
char studentName [MAX_LENGTH+1];
struct courseRecord;
}studentRecord;
void getUserName ();
void getCourse (index);
void GPAPrint ();
void getGradeAverage ();
lib.c
#include "defs.h"
#include <stdbool.h>
#include <string.h>
//Gets username, assigns it to struct studentRecord.
//Known working.
void getUserName ()
{
studentRecord sr;
printf ("please enter the your name:");
scanf("%40[^\n]%*c",sr.studentName);
}
//Gets course name and grade, assigns to array of size 2 of struct courseRecord in studentRecord
void getCourse ()
{
int i;
studentRecord sr;
printf("please enter the name of course 1:");
scanf("%40[^\n]%*c",sr.courseName);
malloc(sizeof(sr.grade));
do{
printf("please enter the grade for course 1:");
scanf("%i",sr.grade);
if ((scanf("%i",sr.grade))!=1)
{
printf("sorry, that grade is in letter form. Please try again.\n");
fflush(stdin);
continue;
}
else if (sr.grade!=-2||0||2||4||6||8||10||12);
{
printf("sorry, that grade is not on the scale. Please try again.\n");
fflush(stdin);
continue;
}
} while(true);
}
void GPAPrint ()
{
int GPA;
studentRecord sr;
/*
GPA=(sr.grade[1]+sr.grade[2])/2
printf("Student name: %s\n",&sr.studentName\n\n);
printf("Course: %s\nGrade:%i\n"sr.coursename[1], sr.grade[1])
printf("Course: %s\nGrade:%i\n"sr.coursename[2], sr.grade[2])
printf("total GPA is %i",GPA)
*/
}
void getGradeAverage ()
{
getUserName();
getCourse();
GPAPrint();
}
thanks for your help in advance!
I'm new to C and I'm developing this basic project while I study it, to help me fixate things... so please, bear with me if my code still looks kind of stupid.
That being said, I'm having trouble with a function to print struct members.
I've created a function to register book details and a separate one for printing said details.
If I print the details within the registerBook function, they are printed correctly.
However, when I call the method printBook, all I get is "garbage". And it's always the same characters,
The code is as follows:
#include <stdio.h>
#include <stdlib.h>
struct Books {
char title[30];
char author[20];
int book_id[10];
char subject[50];
} Books;
int main() {
struct Books Book1;
struct Books Book2;
registerBook(Book1);
printBook(Book1);
registerBook(Book2);
printBook(Book2);
int exit = 0;
while(exit == 0) {
scanf("%p", exit);
}
return 0;
}
void printBook(struct Books a){
printf("\nTitle: %s", a.title);
printf("\nAuthor: %s", a.author);
printf("\nISBN: %d", a.book_id);
printf("\nSubject: %s", a.subject);
}
void registerBook(struct Books a){
printf("\nTitle?");
scanf("%s", &a.title);
printf("\nAuthor?");
scanf("%s", &a.author);
printf("\nISBN?");
scanf("%d", &a.book_id);
printf("\nSubject?");
scanf("%s", &a.subject);
}
All I get is:
Title?one
Author?two
ISBN?3
Subject?four
Title: ç Author: ` ISBN: 6356340 Subject: Ç# Title?five
Author?six
ISBN?7
Subject?eight
Title: &Ý=w¬8wÝ=wÃÊpï Author: ISBN: 6356340 Subject:
Could someone please advise?
In the registerBook function you should pass the parameters by reference and not by value so you can keep the changes after the function ends.
#include <stdio.h>
#include <stdlib.h>
struct Books {
char title[30];
char author[20];
int book_id;
char subject[50];
} Books;
void printBook(struct Books a){
printf("\nTitle: %s", a.title);
printf("\nAuthor: %s", a.author);
printf("\nISBN: %d", a.book_id);
printf("\nSubject: %s", a.subject);
}
void registerBook(struct Books* a){
printf("\nTitle?");
scanf(" %s", a->title);
printf("\nAuthor?");
scanf(" %s", a->author);
printf("\nISBN?");
scanf(" %d", &a->book_id);
printf("\nSubject?");
scanf(" %s", a->subject);
}
int main() {
struct Books Book1;
struct Books Book2;
registerBook(&Book1);
printBook(Book1);
registerBook(&Book2);
printBook(Book2);
return 0;
}
I didn't include your exit loop as it is something that has nothing to do with your problem here.
I've defined a header file with a struct and function prototypes that take a pointer to a struct as a parameter. The code compilation goes fine except that struct instantiated in the main do not seem to retain numerical data.
This is the header file:
#ifndef _GETDATA
#define _GETDATA
#include <stdio.h>
struct PERSONDATA{
char name[20];
double age,mass;
};
typedef struct PERSONDATA person;
extern void getData(person *);
extern void getName(char *,int);
#endif
This is the getData.c file:
#include <stdio.h>
#include "GETDATA.h"
void getData(person *ptr)
{
printf("Enter name: ");
getName(ptr->name,sizeof(ptr->name));
printf("enter age: ");
scanf("%f",&(ptr->age));
printf("enter mass: ");
scanf("%f",&(ptr->mass));
}
and this is the getName.c file:
#include <stdio.h>
#include "GETDATA.h"
void getName(char *ptrName, int varSize)
{
int i=0;
do
{
*(ptrName++) = getchar();
++i;
if(i==varSize) printf("array full, EXITING!\n");
}while(*(ptrName-1)!='\n' && i<varSize);
*(ptrName-1) = '\0';
}
The main function is as follows:
#include <stdio.h>
#include "GETDATA.h"
int main(int argc, char **argv)
{
person human1;
printf("hello, world!\n\n");
getData(&human1);
printf("\nData entered: \n");
printf("\tname = %s\n",human1.name);
printf("\tMass = %f\n",&(human1.mass));
printf("\tAge = %f\n",&(human1.age));
return 0;
}
This is the console output when the code is run:
What could be going wrong here?
Your values are doubles, not floats. You need to use %lf with scanf():
printf("enter age: ");
scanf("%lf",&(ptr->age));
printf("enter mass: ");
scanf("%lf",&(ptr->mass));
Also, your prints are wrong. You are passing a pointer. It should be
printf("\tMass = %f\n",human1.mass);
printf("\tAge = %f\n",human1.age);