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);
Related
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.
I'm having trouble with char pointers. I'm trying to get the value from the store_stuff function and print it into the main function. How can I do this?
#include <stdio.h>
#include <string.h>
#include <assert.h>
void store_stuff(char *name, int *age);
int main(void) {
char *name;
int age;
store_stuff(&name, &age); // I'm having trouble here
printf("Name: %s\n", name);
printf("Age: %d\n", age);
}
void store_stuff(char **name, int *age) {
*name = "John";
*age = 31;
}
https://ideone.com/5HOPGQ
#include <stdio.h>
#include <string.h>
#include <assert.h>
char *store_stuff(char **name, int *age);
int main(void) {
char *name;
int age;
store_stuff(&name, &age); // I'm having trouble here
printf("Name: %s\n", name);
printf("Age: %d\n", age);
}
char *store_stuff(char **name, int *age) {
*name = "John";
*age = 31;
return *name;
}
Make it
void store_stuff(char** name, int* age)
{
*name = "John";
*age = 31;
}
You need that pointer to a pointer for name assignment to make its way out of store_stuff
Also you need to change your definition of
char name
to
char* name
Since name itself wants to be a pointer to interact as a string
EDIT: And of course, be sure to make your prototype consistent at the top with void store_stuff(char** name, int* age);
So, I'm new in this, and trying structures out... Error C2073 appeares..can someone help and give some advice?
I tried with FOR in main function to call functions "ispis" which is for printf only, and function "unos" which is for scanf so many times, how big is int "broj_knjiga". I tried to work with -> instead of . but I simply can't solve this problem(which is simple). Someone help?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void ispis(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\ID knjige: %d", &pt[i].ID_knjige);
printf("\Autor knjige: %s", &pt[i].autor);
printf("\Naslov knjige: %d", &pt[i].naslov);
}
}
void unos(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\nUnesite ID knjige: ");
scanf("%d", &pt[i].ID_knjige);
printf("\nUnesite autora knjige: ");
scanf("%d", &pt[i].autor);
printf("\nUnesite naslov knjige: ");
scanf("%d", &pt[i].naslov);
}
}
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
int main()
{
struct knjiga *pt;
int broj_knjiga;
printf("Unesite koliko knjiga unosite: ");
scanf("%d", &broj_knjiga);
pt=(struct knjiga*)malloc(sizeof(struct knjiga)*broj_knjiga);
unos(pt, broj_knjiga);
ispis(pt, broj_knjiga);
return 0;
}
C is compiled from the top-down. Structs only exist below their declarations. Moving your struct definition above the rest of your code will fix your problem.
knjiga myStruct1; // invalid
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
knjiga myStruct2; // valid
I'm trying to display the values of my structure array, the compiler throws out the following error:
athletes.c:17: error: expected ')' before '*' token
Could someone help me out with a solution and if possible an explanation as to what I did wrong.
#include <stdio.h>
#include <string.h>
struct athlete {
char last_name[25];
char first_name [20];
int rank;
float salary;
};
int main (void) {
struct athlete players[] = {{"Lebron", "James",1,25}, {"Durant", "Kevin",3,20},{"Duncan","Tim",2,12}};
display_jock(players);
}
void display_jock(athlete *p) {
printf ("%10c%10c%10d%10.1f \n", p[0].last_name,p[0].first_name,p[0].rank, p[0].salary);
printf ("%10c%10c%10d%10.1f \n", p[1].last_name,p[1].first_name,p[1].rank, p[1].salary);
printf ("%10c%10c%10d%10.1f \n", p[2].last_name,p[2].first_name,p[2].rank, p[2].salary);
}
there are several small errors:
your code does not know the type "athlete", so your class display_jock should be defined with argument struct athlete: void display_jock(struct athlete *p)
you should forward declare that function: otherwise main doesn't know it (edit:you could also just move the display_jock function at the top of the main function)
when printing a char array, you should use %s instead of %c when using printf.
your main function should return an int (since it is declared as int main...)
this is your code fixed up:
#include <stdio.h>
#include <string.h>
struct athlete {
char last_name[25];
char first_name [20];
int rank;
float salary;
};
void display_jock(struct athlete *p);
int main (void) {
struct athlete players[] = {{"Lebron", "James",1,25}, {"Durant", "Kevin",3,20},{"Duncan","Tim",2,12}};
display_jock(players);
return 0;
}
void display_jock(struct athlete *p) {
printf ("%s%s%10d%10.1f \n", p[0].last_name,p[0].first_name,p[0].rank, p[0].salary);
printf ("%s%s%10d%10.1f \n", p[1].last_name,p[1].first_name,p[1].rank, p[1].salary);
printf ("%s%s%10d%10.1f \n", p[2].last_name,p[2].first_name,p[2].rank, p[2].salary);
}
I'm trying to call a structure to a function but I'm getting the error: 'course' undeclared
The error is in the hw3func program. I typedef'd my structure and malloc'd space for it inside of main, but I don't know what I need to make my function prototype or my function call to recognize the struct? Thank you for the help in advance!!
hw3.c:
#include <stdio.h>
#include <stdlib.h>
#include "hw3func.c"
int main(void)
{
coursestruct *course = malloc(1*sizeof(coursestruct));
studentstruct *student = malloc(1*sizeof(studentstruct));
display();
menu();
return 0;
}
hw3.h:
#include <stdio.h>
#include <stdlib.h>
/*Start of prototypes*/
void display(void);
void menu(void);
void newcourse(int coursetotal, coursestruct *course);
void newstudent(int studenttotal);
/*End of prototypes*/
/*Start of initial struct declares*/
// coursestruct *course = malloc(1 * sizeof(coursestruct));
// studentstruct *student = malloc(1 * sizeof(studentstruct));
/*End of initial struct declares*/
/*Start of variables*/
int coursetotal=0;
int studenttotal=0;
/*End of variables*/
hw3struct.h:
typedef struct{
char name[50];
int id[4];
}coursestruct;
typedef struct{
char name[20];
int id[8];
}studentstruct;
hw3func.c
#include <stdio.h>
#include <stdlib.h>
#include "hw3struct.h"
#include "hw3.h"
void display(void)
{}
void menu(void)
{
int loop=0; /*Loop for the menu*/
while(loop==0)
{
int option;
printf("\n\n\nWelcome to the grade book! Select your option below.\n");
printf("1= Add a new course.\n");
printf("2= Add a new student.\n");
printf("3= Add a student to a course.\n");
printf("4= Add grades for a student in a course.\n");
printf("5= Print a list of all grades for a student in a course.\n");
printf("6= Print a list of all students in a course.\n");
printf("7= Compute the average for a student in a course.\n");
printf("8= Print a list of all courses.\n");
printf("9= Print a list of all students.\n");
printf("10= Compute the average for a course.\n");
printf("11= Store grade book to a disk file.\n");
printf("12= Load grade book from a disk file.\n");
printf("13= Exit grade book.\n");
scanf("%d",&option);
printf("\n\n\n");
if(((option>0) && (option<14)))
{
switch(option)
{
case 1:
newcourse(coursetotal,course);
break;
case 13:
loop=1;
break;
default:
break;
}
}
else
{
printf("Please input a number between 1-13.\n");
}
}
}
void newcourse(int coursetotal, coursestruct *course)
{
}
You declare coursestruct *course inside main, so it is not directly accessible from other functions (like your menu() function).
You should do some reading on variable scope in C.
In this case, the proper fix is most likely to change your function declarations to something like
void menu (coursestruct *course);
Then call it from main as menu(course);. This allows the menu function access to the variable declared inside main.