Can you help me with passing elements of struct into a function (void show_info_abt_bus) for outputting my information? I don't understand how I should pass those elements.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct info_bus_{
int number;
int begin;
int end;
char* stations;
int time_working;
}info_bus;
void input_data_abt_bus(info_bus **,int);
void show_info_abt_bus(info_bus *,int);
void my_free(info_bus **,int);
void input_data_abt_bus(info_bus **b,int n){
int i;
char buffer[128];
if(!((*b)=(info_bus *)malloc(n*sizeof(info_bus)))){
printf("Error memory\n");
exit(0);
}
for(i=0;i<n;i++){
printf("Input the number of a bus: \n");
scanf("%d",&((*b)[i].number));
printf("%d)Input when it starts to work: \n",i+1);
scanf("%d",&((*b)[i].begin));
printf("%d)Input when it finishes to work: \n",i+1);
scanf("%d",&((*b)[i].end));
printf("%d)Input its stations: \n",i+1);
scanf(" %127[^\n]%*c", buffer);
(*b)[i].stations = (char*) malloc(strlen(buffer) + 1);
strcpy((*b)[i].stations, buffer);
getchar();
printf("Input time working: \n");
scanf("%d",&((*b)[i].time_working));
}
}
void my_free(info_bus **b,int n){
int i;
for (i = 0; i < n; i++) {
free((*b)[i]);
}
free(b);
}
int main()
{
int i,n;
printf("How many buses u have: \n");
scanf("%d",&n);
info_bus *b=NULL;
input_data_abt_bus(&b,n);
show_info_bus_abt_bus(b,n);
my_free(b,n);
return 0;
}
You need to pass structure object Pass by reference in function.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct info_bus_{
int number;
int begin;
int end;
char* stations;
int time_working;
}info_bus;
void input_data_abt_bus(info_bus **,int);
void show_info_abt_bus(info_bus *,int);
void show_info_bus_abt_bus(info_bus *b,int n){
int i;
for (i=0;i<n;i++){
printf("\n===============================================");
printf("\n[%d].the number of a bus: %d",i+1,b[i].number);
printf("\n[%d]. Begin at: %d am",i+1,b[i].begin);
printf("\n[%d]. Finishes at: %d pm",i+1,b[i].end);
printf("\n[%d]. Stations: %s",i+1,b[i].stations);
printf("\n[%d]. Time working: %d",i+1,b[i].time_working);
printf("\n===============================================\n");
}
}
void input_data_abt_bus(info_bus **b,int n){
int i;
char buffer[128];
(*b)=(info_bus *)malloc(n*sizeof(info_bus));
for(i=0;i<n;i++){
printf("Input the number of a bus: \n");
scanf("%d",&((*b)[i].number));
printf("%d)Input when it starts to work: \n",i+1);
scanf("%d",&((*b)[i].begin));
printf("%d)Input when it finishes to work: \n",i+1);
scanf("%d",&((*b)[i].end));
printf("%d)Input its stations: \n",i+1);
scanf(" %127[^\n]%*c", buffer);
(*b)[i].stations = (char*) malloc(strlen(buffer) + 1);
strcpy((*b)[i].stations, buffer);
getchar();
printf("Input time working: \n");
scanf("%d",&((*b)[i].time_working));
}
}
void my_free(info_bus **b,int n)
{
int i;
for (i = 0; i < n; i++) {
free((*b)[i].stations);
}
free((*b));
(*b)=NULL;
}
int main()
{
int i,n;
printf("How many buses u have: \n");
scanf("%d",&n);
info_bus *b=NULL;
input_data_abt_bus(&b,n);
show_info_bus_abt_bus(b,n);
my_free(&b,n)
return 0;
}
Related
This code has several issues that are beyond my understanding.
First, no matter how hard I try, the variable ok doesn't get saved in the file, thus ruining the display. It is strange because other variables work, it is just that one that doesn't seem to be working, even if I change the order.
Also, the delete and modify functions aren't working, as well.
I tried everything, but nothing seems to be working.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
// Structure of the employee
struct emp {
char name[50];
char ok[50];
int hr;
int age;
int id;
};
FILE *f;
emp* add(emp *e,int n){
int i;
char p;
emp *t;
int k;
t=(emp*)malloc(n*sizeof(int));
f=fopen("Data.txt","w");
for(i=0;i<n;i++){
printf("\nEnter Name : ");
scanf("%s",(t+i)->name);
printf("\nEnter Departement: ");
scanf("%s",&(t+i)->ok);
printf("\nEnter Age : ");
scanf("%d",&(t+i)->age);
printf("\nEnter avearge hour of works(per week) : ");
scanf("%d",&(t+i)->hr);
// k = ( ( (t+i)->hr ) *40 );//40 is minimum wage for each hour of work
// printf("Salary of The employe: %d\n",k);
printf("\nEnter EMP-ID : ");
scanf("%d",&(t+i)->id);
fprintf(f,"%s\n%s\n%d\n%d\n%d\n",(t+i)->name,(t+i)->ok,(t+i)->age,(t+i)->hr,(t+i)->id);
}
fclose(f);
return t;
}
void aff(emp *t,int n){
int I;
f=fopen("Data.txt","r");
for(i=0;i<n;i++){
if (f != NULL){
fscanf(f,"%s",&(t+i)->name);
fscanf(f,"%s",&(t+i)->ok);
fscanf(f,"%d",&(t+i)->age);
fscanf(f,"%d",&(t+i)->hr);
fscanf(f,"%d",&(t+i)->id);
printf("Name : %s\n",(t+i)->name);
printf("departement : %s\n",(t+i)->ok);
printf("Age : %d\n",(t+i)->age);
printf("Hours : %d\n",(t+i)->hr);
printf("ID : %d\n",(t+i)->id);
}
}
fclose(f);
}
//emp* modf(emp *t,int n){
// int i;
// int k;
// char nv[50];
// printf("id of the entry you want to be modified" );
// scanf("%d\n",&k);
// for(i=0;i<n;i++){
// if(k==(t+i)->id){
// scanf("%s",&nv);
// (t+i)->name=nv;
// }
// }
// return t;
//}
//void del(emp *t,int n){
// int i;
// emp a;
// int k;
// printf("position of the entry you want to delete?");
// scanf("%d",&a);
// for(i=0;i<n;i++){
// if (a == *(t+i)) {
// for(i=k;i<n-1;i++){
// *(t+i)=*(t+i+1);
// }
// }
// }
//}
int main(int argc, char *argv[]){
int c;
emp e;
emp *k;
k=&e;
int n;
emp *t;
t=&e;
char p;
ka:
printf("Welcome To The employe management Menu\n");
printf("1.Add employes?\n");
printf("2.show all employes?\n");
printf("3.delete an entry?\n");
scanf("%d",&c);
switch (c){
case 1:
ed:
printf("How many employes you want to add?\n");
scanf("%d",&n);
add(k,n);
if(p=='y'){
goto ed;
}
else goto ka;
break;
case 2:
aff(t,n);
break;
case 3:
del(t,n);
break;
}
return 0;
}
At least these issues:
Wrong allocation size #Weather Vane
Avoid allocation errors. Allocate to the size of the refenced object, not the type.
// t=(emp*)malloc(n*sizeof(int));
t = malloc(sizeof t[0] * n);
Easier to code right, review and maintain.
Uninitialized p #Craig Estey
f(p=='y') is undefined behavior as p is uninitialized.
This hints that OP is not compiling with all warnings enabled. Save time - enable all warnings.
emp not defined
Perhaps OP is not using a C compiler, but a C++ one?
i not defined in for (i = 0; i < n; i++) {
Is int I; the true code?
Bad "%s"
Without a width, "%s" is worse than gets(). Use a width like "%49s".
i wrote a programe to read integers from a .txt file and put them in an array, it compiles just fine ,but would ask for the number of integers then stops
#include <stdlib.h>
#include <stdio.h>
void genererN ( int n){
int i=0;
FILE* fichier = NULL;
fichier=fopen("valeurs.txt","w");
while (i<n){
fprintf(fichier,"\n%d\n",rand());
i++;
}
}
void replirtab (int **t,int n){
int i=0;int m;
FILE* fichier = NULL;
fichier=fopen("valeurs.txt","r");
char stre[999999] = "";
while(i<n){
fgets(stre, 999999, fichier);
m = atoi(stre);
*t[i]=m;
i++;
}
}
void affichertab (int *t,int n){
int i=0;
while(i<n){
printf("%d\n",t[i]);
i++;
}
}
and this is my main function where i ask for the number of randomly generated integers and use my functions
#include <stdio.h>
#include <stdlib.h>
#include "source1.h"
int main()
{
int n;
printf("donner le nombre de valeurs ");
scanf("%d",&n);
int T[n];
genererN(n);
replirtab(T,n);
affichertab(T,n);
return 0;
}
and the header
#ifndef SOURCE1_H_INCLUDED
#define SOURCE1_H_INCLUDED
void genererN ( int n);
void replirtab (int *t,int n);
void affichertab (int *t,int n);
#endif // SOURCE1_H_INCLUDED
Fix the following problems:
You're not closing the file after you write it, so the buffer isn't being flushed.
You're writing an extra newline before each number, but you don't skip it when reading.
replirtab() expects an array of pointers, but the array contains integers, not pointers. There's no need to indirect through the references. When you pass an array to a function, a pointer to the first element is passed.
#include <stdlib.h>
#include <stdio.h>
void genererN ( int n){
int i=0;
FILE* fichier = NULL;
fichier=fopen("valeurs.txt","w");
if (!fichier) {
printf("Unable to write file\n");
exit(1);
}
while (i<n){
fprintf(fichier,"%d\n",rand());
i++;
}
fclose(fichier);
}
void replirtab (int *t,int n){
int i=0;int m;
FILE* fichier = NULL;
fichier=fopen("valeurs.txt","r");
if (!fichier) {
printf("Unable to read file\n");
exit(1);
}
char stre[999999] = "";
while(i<n){
fgets(stre, 999999, fichier);
m = atoi(stre);
t[i]=m;
i++;
}
fclose(fichier);
}
void affichertab (int *t,int n){
int i=0;
while(i<n){
printf("%d\n",t[i]);
i++;
}
}
Every time I try to compile the following code I get a boatload of errors.
Does anyone see any obvious things that I overlooked with my C program. Any help would be appreciated.
The header file structures.h is also attached.
This is the structures.cc file
#include <stdio.h>
#include <string.h>
#include "structures.h"
void getOnePerson(person_t *p) {
char *name;
char *ssn;
int *year;
printf("Enter full name: ");
scanf("%s", &name);
p.name = &name;
printf("\nEnter ssn: ");
scanf("%s", &ssn);
p.ssn = &ssn;
printf("\nEnter year of birth: ");
scanf("%d", year);
p.yearOfBirth = &year;
}
void printOnePerson(person_t t1) {
char *name = t1.name;
char *ssn = t1.ssn;
int year = t1.yearOfBirth;
printf("%s", &name);
printf(":");
printf("%s", &ssn);
printf(":");
printf("%p", &year);
printf("\n");
}
void getPeople(person_t peoples[], int people) {
for (int a = 0; a < people; a++) {
getOnePerson(peoples[a]);
}
}
void printPeople(person_t peoples[], int people) {
for (int p = 0; p < people; p++) {
peoples[p];
}
}
This is the structures.h file
#include <stdio.h>
#include <string.h>
#define NAME_SIZE 80
#define SSN_SIZE 13
#define NUM_PEOPLE 10
typedef struct{
char name[NAME_SIZE];
char ssn[SSN_SIZE];
int yearOfBirth;
}person_t;
void getOnePerson(struct person_t *p);
void printOnePerson(struct person_t p);
void getPeople(struct person_t peoples[], int people);
void printPeople(struct person_t peoples[], int people);
Your function declarations in the header file are incorrect.
You make reference to one or more parameters of type struct person_t, but there is no such type. There is a type named person_t, so change the function prototypes to use that.
void getOnePerson(person_t *p);
void printOnePerson(person_t p);
void getPeople(person_t peoples[], int people);
void printPeople(person_t peoples[], int people);
Your get and print functions also have several issues. In both cases, you're using intermediate variables to read/write values which aren't necessary:
void getOnePerson(person_t *p) {
printf("Enter full name: ");
scanf("%s", p->name);
printf("\nEnter ssn: ");
scanf("%s", p->ssn);
printf("\nEnter year of birth: ");
scanf("%d", &p->yearOfBirth);
}
void printOnePerson(person_t t1) {
printf("%s", t1.name);
printf(":");
printf("%s", t1.ssn);
printf(":");
printf("%d", t1.yearOfBirth);
printf("\n");
}
I've looked around but can't seem to find how to do this. I want the value of empty lines from test2(int *f) to be passed to test1() and be printed on the screen.
Variant 1 of code:
#include <stdio.h>
#include <string.h>
void test1();
void test2(int *f);
void test1(){
int a;
test2(&a);
printf("%d \n", a);
}
void test2(int *f){
char str[80];
int lines, i, emptylines=0;
*f=emptylines;
printf("Type a program here. Ctrl+Z and enter to stop.\n");
fflush(stdin);
while(gets(str)!=NULL && strcmp(str, "qq")) {
for(i=0; i<strlen(str); i++){
if(str[i]!='\n') lines=1;
}
if(!lines) emptylines++;
lines=0;
}
}
int main() {
test1();
return 0;
}
Variant 2 of code:
#include <stdio.h>
#include <string.h>
void test1();
void test2(int *f);
void test1(){
int a;
test2(&a);
printf("%d \n", a);
}
void test2(int *f){
char str[80], *p;
int lines, emptylines=0;
*f=emptylines;
printf("Type a program here. Ctrl+Z and enter to stop.\n");
fflush(stdin);
while(gets(str)!=NULL && strcmp(str, "qq")) {
p=str;
lines=0;
while(*p!='\0') {
if(*p!=' ') {
lines=1;
}
p++;
}
if(lines==0){
emptylines++;
lines=0;
}
}
}
int main() {
test1();
return 0;
}
You are putting *f=emptylines in the beginning of the function void test2(int *f); Then you calculate emptylines but this will not affect the value pointed-to by f.
You need to move that assignment *f=emptylines to the end of the function, just before returning and after having calculated emptylines
void test2(int *f){
// stuff to calculate emptylines
....
*f=emptylines; // at the end
}
When you write
*f = emptylines;
you are copying the value of emptylines into the space pointed to by f. Then when you update emptylines later, the value pointed to by f doesn't change because you made a copy.
Rather than using another variable emptylines just use the parameter f directly to compute the value. Though I'd give it a more descriptive value than f, something like numEmptyLines.
#include <stdio.h>
#include <string.h>
void test1();
void test2(int *numEmptyLines);
void test1(){
int a;
test2(&a);
printf("%d \n", a);
}
void test2(int *numEmptyLines){
char str[80];
int lines, i;
*numEmptyLines = 0;
printf("Type a program here. Ctrl+Z and enter to stop.\n");
fflush(stdin);
while(gets(str)!=NULL && strcmp(str, "qq")) {
for(i=0; i<strlen(str); i++){
if(str[i]!='\n') lines=1;
}
if(!lines) (*numEmptyLines)++;
lines=0;
}
}
int main() {
test1();
return 0;
}
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