Re-allocating array of structure with realloc? - c

I'm practicing on structures, dynamic memory and file I/O but I can't understand what is wrong with this code. I suspect that the error is with realloc function. When I run the program after the file opening, the program crash.
I check a lot of times and I suppose that is not a syntax related problem(especially with realloc).
Structure declaration:
#define MAXLENPATH 250
#define MAXSTRING 25
typedef struct
{
int ID;
char Name[MAXSTRING];
char Surname[MAXSTRING];
char code[MAXSTRING];
int age;
}person;
Function to get record info:
#include <stdio.h>
#include <stdlib.h>
#include "Data.h"
#include "DataBase.h"
void initRecord(person *Person)
{
printf("Insert ID:\n");
scanf("%d", &Person->ID);
printf("Insert Name:\n");
scanf("%s", Person->Name);
printf("Insert Surname:\n");
scanf("%s", Person->Surname);
printf("Insert Age:\n");
scanf("%d", &Person->age);
printf("Insert code:\n");
scanf("%s", Person->code);
}
Main function:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Data.h"
#include "DataBase.h"
int main(void) {
FILE *ts;
char *fpath=NULL;
int pCount=1; //variable to hold database records count
int i=0;
int toAdd=0;
person *personDB=NULL;
fpath="C:\\Users\\Pio\\Desktop\\FILEOP\\MYTXT.txt";
personDB = malloc(pCount*sizeof(person));
//fill the database(in memory)
i=0;
while( i < pCount)
{
printf("Record %d\n",i+1);
initRecord(&personDB[i]); //fill i-th database record
printf("Record %d ID:%d Name:%s Surname:%s Age:%d Code:%s \n", i+1, personDB[i].ID, personDB[i].Name, personDB[i].Surname, personDB[i].age, personDB[i].code );
i++;
if(i == pCount) // check if all records are filled, then ask to user how many records wants and realloc new memory
{
printf("Insert element to add to database records:");
scanf("%d", &toAdd);
personDB=realloc(personDB, toAdd*(sizeof(person)) );
pCount += toAdd;
}
}
//Print structure on file
if ((ts=fopen(fpath, "w")) != NULL)
{
puts("FILE OPENED");
fprintf(ts,"************INFO****************\n");
i=0;
while (i < pCount)
{
fprintf(ts, "Record %d ID:%d Name:%s Surname:%s Age:%d Code:%s \n", i+1, personDB[i].ID, personDB[i].Name, personDB[i].Surname, personDB[i].age, personDB[i].code );
i++;
}
fclose(ts);
}
else
perror("ERROR:");
system("pause");
return EXIT_SUCCESS;
}

Related

How can i automatically increase memory allocation for a struct in C?

I am building a small program that takes name and age as input (stored in a struct) and spits out the output. One of the problems that I am facing is I have to enter the number of people I am going to store, something that I am sure I can solve with realloc() it's just not working. Here is what I got so far.
#include <stdio.h>
#include<stdlib.h>
struct info
{
int age;
char name[30];
};
int main()
{
struct info *Ptr;
int i, num;
printf("Enter number of people");
scanf("%d", &num);
// Allocates the memory for num structures with pointer Ptr pointing to the base address.
Ptr = (struct info*)malloc(num * sizeof(struct info));
for(i = 0; i < num; ++i)
{
printf("Enter name and age:\n");
scanf("%s %d", &(Ptr+i)->name, &(Ptr+i)->age);
}
for(i = 0; i < num ; ++i)
printf("Name = %s, Age = %d\n", (Ptr+i)->name, (Ptr+i)->age);
return 0;
}
I have tried to realloc inside the first for loop, but it wasn't working even if it makes sense to have it there. Have also tried to convert the loop to a while loop like this:
while(input != "stop)
{
allocate more memory
}
How can I use realloc to in order to skip having to enter the persons number before entering them?
realloc is the correct way. Just start with Ptr = NULL and num = 0 and on each input increase the number of elements by one.
Remember to limit the number of characters scanf can read, otherwise you may buffer overrun.
Also I find Ptr[i] way easier then (Ptr+i)->.
Also compare strings with strcmp not using !=. The != will compare pointers to strings, not strings themselves.
As I like reading the whole line, then scanning the line, I would do it like this:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
struct info
{
int age;
char name[30];
};
int main()
{
struct info *ptr = 0;
size_t num = 0;
for (;;) {
printf("Enter name and age. If you want to stop, type only 'stop'.\n");
char line[256];
if (fgets(line, sizeof(line), stdin) == NULL) {
fprintf(stderr, "fgets error");
exit(-1);
}
if (!strcmp("stop\n", line)) {
break;
}
struct info tmp;
if (sscanf(line, "%29s %d\n", tmp.name, &tmp.age) != 2) {
fprintf(stderr, "error parsing line\n");
exit(-1);
}
ptr = realloc(ptr, (num + 1) * sizeof(*ptr));
if (ptr == NULL) {
fprintf(stderr, "error allocating memory!\n");
exit(-1);
}
ptr[num] = tmp;
++num;
}
for (size_t i = 0; i < num ; ++i) {
printf("Name = %s, Age = %d\n", ptr[i].name, ptr[i].age);
}
free(ptr);
return 0;
}
If you are not sure of the no.of.elements you want to allocate and do it based on the users choice, then you can follow the below approach.
It starts with one element and the memory is reallocated as when the user wants to add new element.
#include <stdio.h>
#include<stdlib.h>
struct info
{
int age;
char name[30];
};
int main()
{
struct info *Ptr=NULL;
int i=0, num;
char c='Y';
while(c=='Y'||c=='y') {
Ptr=realloc(Ptr,(i+1)*sizeof(struct info));
if(Ptr==NULL)
break;
printf("Enter name and age:\n");
scanf("%s %d",&Ptr[i].name,&Ptr[i].age);
printf("Do you want to cont?\n");
scanf(" %c",&c);
i++;
}
num=i;
for(i = 0; i < num ; ++i)
printf("Name = %s, Age = %d\n", (Ptr+i)->name, (Ptr+i)->age);
free(Ptr);
return 0;
}
To answer exactly, you can first read the input to temp variables and check if you need to stop: Break the loop if so. Or continue and reallocate the 'storage' array by increasing it's size by one and copying the values you just read to the 'storage' array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct info
{
int age;
char name[30];
};
int main()
{
struct info * infos = 0;
int num = 0;
char input_name[30];
int input_age;
while (1)
{
printf("Enter name and age:\n");
int r = scanf("%29s", input_name);
if (r == EOF || strcmp(input_name, "stop") == 0)
break;
scanf(" %d", &input_age);
infos = realloc(infos, sizeof(struct info) * (num + 1));
infos[num].age = input_age;
memcpy(infos[num].name, input_name, sizeof(char) * 30);
num++;
}
for(int i = 0; i < num ; ++i)
printf("Name = %s, Age = %d\n", infos[i].name, infos[i].age);
return 0;
}
You should use a data struct like vector.
vector_init init vector.
vector_push push val to vector, if necessary, will realloc memory.
vector_output output the vector.
The following code could work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INIT_SIZE 1
struct info
{
int age;
char name[30];
};
struct vector {
struct info* p;
int n;
int index;
};
void vector_init(struct vector* ve) {
ve->n = INIT_SIZE;
ve->index = 0;
ve->p = malloc(sizeof(struct info) * ve->n);
}
void vector_push(struct vector* ve, struct info* tmp) {
if (ve->n == ve->index) {
ve->n *= 2;
ve->p = realloc(ve->p, sizeof(struct info) * ve->n);
}
ve->p[ve->index++] = *tmp;
}
void vector_output(const struct vector* ve) {
for (int i = 0; i < ve->index; ++i)
printf("Name = %s, Age = %d\n", ve->p[i].name, ve->p[i].age);
}
int main()
{
struct vector ve;
vector_init(&ve);
for (;;) {
struct info tmp;
printf("Enter name and age:\n");
scanf("%29s", tmp.name);
if (strcmp(tmp.name, "stop") == 0)
break;
scanf("%d", &tmp.age);
vector_push(&ve, &tmp);
}
vector_output(&ve);
return 0;
}

C language - printing some sentences to file

I want to read strings and write them as full lines into a file, but I can't read more words into a buffer as a complete string.
Current problematic code:
printf("\nEnter how many sentences do you want to read: ");
scanf("%d", &n);
tab = (char**)malloc(n * sizeof(char*));
for (int i = 0; i < n; i++) {
printf("\nEnter sentence: ");
scanf("%s", val);
tab[i] = _strdup(val);
}
for (i = 0; i < n; i++)
fprintf(f, "%s ", tab[i]);
free(tab);
Previously I tried this: (problem is this only assigns one string)
printf("\nEnter how many sentences do you want to read: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter sentence: ");
scanf("%s", val);
fprintf(f, "\%s ", val);
}
Almost there, now i have sentences but i got one empty line as first line of file.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string>
#define SIZE 30
void creare(char t[30]);
void main(void)
{
FILE* f2;
char name[30];
printf("\nEnter name of file to work with: ");
scanf("%s", name);
creare(name);
f2 = fopen(name, "r");
if (f2 == NULL)
{
printf("\nOpen error!!");
exit(0);
}
fclose(f2);
printf("\n");
_getch();
}
void creare(char t[30])
{
FILE* f;
int n,i;
char val[30];
f = fopen(t, "w");
if (f == NULL)
{
printf("\nOpen error!!");
exit(0);
}
printf("\nEnter how many sentences do you want to read: ");
scanf("%d", &n);
for (i = 0; i <= n; i++)
{
fgets(val, sizeof(val), stdin);
fprintf(f, "% s", val);
}
fclose(f);
}
I have used fgets(val, sizeof val, stdin) to read the string, because to read string with spaces.
The reason why that blank line comes in file is due to the fact that you are reading "\n" that is inputted after scanf("%d", &n);The keystroke "\n" will be read into val for the first time so it simply prints that "\n" to the file.
In order to read that "\n" use a character to read that "\n". Below is the complete program.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define SIZE 30
void creare(char t[30]);
int main(void)
{
FILE* f2;
char name[30];
printf("\nEnter name of file to work with: ");
scanf("%s", name);
creare(name);
f2 = fopen(name, "r");
if (f2 == NULL)
{
printf("\nOpen error!!");
exit(0);
}
fclose(f2);
printf("\n");
}
void creare(char t[30])
{
FILE* f;
int n,i;
char val[30],g;
f = fopen(t, "w");
if (f == NULL)
{
printf("\nOpen error!!");
exit(0);
}
printf("\nEnter how many sentences do you want to read: ");
scanf("%d", &n);
scanf("%c",&g);
for (i = 0; i <n; i++)
{
fgets(val, sizeof(val), stdin);
fprintf(f, "%s", val);
}
fclose(f);
}

Storing .txt file into structures

I'm trying to store the .txt file into struct, not being successful.
I dont need the first line stored, but I do need the rest.
Thanks guys !! =)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
struct candidates
{
char name[25];
char gender[5];
int height;
int weight;
};
struct candidates values[25];
FILE *fp;
if ((fp=fopen("candidatesdata.txt","r"))==NULL){printf("Unable to open file\n");}
int x = 0;
int iterations = 0;
while((fscanf(fp,"%s,%s,%d,%d",&values[x].name, &values[x].gender, &values[x].height, &values[x].weight))!=EOF)
{
x++;
iterations +=1;
}
fclose(fp);
//values[15].weight = 300;
printf("\n%d\t%d",iterations,values[1].height);
return 0;
}
Text file looks like the following:
Name,Gender,Height,Weight
Tanner,M,71.8,180.25
John,M,70.75,185.3
Parker,F,65.25,120.3
Meeks,M,57.25,210.2
struct candidates
{
char name[25];
char gender[5]; // can be char gender too !!
int height; // should be changed to float height
int weight; // should be changed to float height
};
If you actually have :
Name,Gender,Height,Weight
in your file, you might do his before you go to the while loop :
char somebigstring[100];
.
.
.
if(fscanf(fp,"%s",somebigstring) == EOF) //wasting the first line
exit(-1); // exiting here if there is no first line
fix like this:
#include <stdio.h>
#include <stdlib.h>
struct candidates{
char name[25];
char gender[7];//Female+1
float height;
float weight;
};
int main(void) {
struct candidates values[25];
FILE *fp;
if ((fp=fopen("candidatesdata.txt","r"))==NULL){
fprintf(stderr, "Unable to open file\n");
exit(EXIT_FAILURE);
}
char line[64];
int x = 0;
int iterations = 0;
while(fgets(line, sizeof line, fp)){
iterations += 1;
if(x < 25 && sscanf(line, "%24[^,],%6[^,],%f,%f", values[x].name, values[x].gender, &values[x].height, &values[x].weight)==4){
x++;
} else if(iterations != 1){
fprintf(stderr, "invalid data in %d line: %s", iterations, line);
}
}
fclose(fp);
for(int i = 0; i < x; ++i){
printf("%s,%c,%.2f,%.2f\n", values[i].name, *values[i].gender, values[i].height, values[i].weight);
}
return 0;
}
This code does two following things; first is writes data into .txt file and the second it reads data from .txt file and stores in struct data type which is what you are trying achieve but only for single entity. Next you should implement this as for entities.
#include<stdio.h>
#include<stdlib.h>
typedef struct{
char name[30];
//other fields
}Candidate;
typedef struct{
Candidate c;
char city[30];
int vote;
}CityVotes;
//for .bin operations
void write_to_file(FILE *f)
{
Candidate c;
CityVotes cv;
printf("Enter candidate name : ");
scanf("%s",&c.name);
cv.c = c;
printf("Enter the city : ");
scanf("%s",&cv.city);
printf("Enter the vote : ");
scanf("%d",&cv.vote);
fwrite(&cv, sizeof(cv), 1, f);
}
//for .txt operations
void write_to_file1(FILE *f)
{
Candidate c;
CityVotes cv;
printf("Enter candidate name : ");
scanf("%s",c.name);
cv.c = c;
printf("Enter the city : ");
scanf("%s",&cv.city);
printf("Enter the vote : ");
scanf("%d",&cv.vote);
fprintf(f,"%s ", cv.c.name);
fprintf(f,"%s ", cv.city);
fprintf(f,"%d ", cv.vote);
}
//for .bin operations
void read_file(FILE *f)
{
CityVotes cv;
while(fread(&cv, sizeof(cv), 1, f)){
printf("Candidate : %s\t",cv.c.name);
printf("City.: %s\t",cv.city);
printf("Vote.: %d\n",cv.vote);
}
}
//for .txt operations
void read_file1(FILE *f){
CityVotes cv;
fscanf(f," %s", &cv.c.name);
fscanf(f," %s", &cv.city);
fscanf(f," %d", &cv.vote);
printf("Candidate : %s\t",cv.c.name);
printf("City.: %s\t",cv.city);
printf("Vote.: %d\n",cv.vote);
}
int main(void)
{
FILE *f;
f=fopen("candidates.txt","w");
write_to_file1(f);
fclose(f);
f=fopen("candidates.txt","r");
read_file1(f);
return 0;
}
Be aware, the code just is example , you should check for nulls.

Putting data from a variable into an array

Hello good people of Stack Overflow. Need some help with my code. I am basically trying to put the data that is stored in the variables itemCode, itemName, pricepu, and stock into array and I have NO IDEA how to do so..
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define totalitems 20
#define maxitems 300
void mainmenu(void);
void alt_menu(void);
void purchase(void);
void edit_item(void);
void update_item(void);
void del_item(void);
void shw_item(void);
void invent_menu(void);
void alt_invent_menu(void);
void daily_trans(void);
void exit_escape(void);
void back_inventmenu(void);
void back_menu(void);
void gst_array(void);
char invent_back, back; //global declarations
char input, newinput;
char *barcode;
char taxitems[20][20];
char ntaxitems[20][20];
char line[2048];
char *itemCode[20][20];
char itemName[20][20];
unsigned char pricepu[10][20];
char stock[10][20];
int i, ret, quantpurchase;
double total;
void purchase() {
char exitcode[] = "-1";
barcode = (char*) malloc(5);
printf(" Please key in the barcode number of the product: ");
scanf(" %s", barcode);
while(strcmp(exitcode, barcode) != '\0') {
double priceint;
FILE *gst = fopen("gst.txt", "r");
FILE *ngst = fopen("ngst.txt", "r");
printf("\n");
printf("\n Item Code Entered: %s\n", barcode);
while(fgets(line, sizeof(line), gst) != NULL) { //while fgets does not fail to scan a line
if(sscanf(line, "%[^;];%[^;];%[^;];%[^;]", itemCode, itemName, pricepu, stock) != 4) {
//If sscanf failed to scan everything from the scanned line
//%[^;] scans everything until a ';'
printf("Bad line detected\n");
exit(-1); //Exit the program
}
for (i=0; i < totalitems; i++) {
if (strcmp(itemCode[i], barcode) == '\0') {
printf("\n");
printf(" Item Found.\n");
printf("\n");
printf("===========================================================================\n");
printf(" Item code\t Item name \t\t\tPrice\t Stock Available \n");
printf("===========================================================================\n");
printf(" %-10s\t %-16s\t\t%s\t %s\n", itemCode, itemName, pricepu, stock);
printf("\n");
printf("\n");
priceint = strtod(pricepu, (char **)NULL);
printf(" How many would you like: ");
scanf("%d", &quantpurchase);
total = priceint * quantpurchase;
printf(" %.2lf\n", total);
for (i=0; i<maxitems; i++) {
taxitems[0][i] = itemCode[i];
printf(" %s", taxitems[0][0]); //HERE'S WHERE I NEED HELP
}
purchase();
}
}
}
}
}
It's not giving me my desired result. Basically say "AG001" is currently stored under the variable "itemCode", I want to move that into an array. Appreciate all the help I can get. Thanks.
taxitems[0][i] = itemCode[i];
You can't copy char arrays with simple assignment. You need to use the strcpy() function instead.
strcpy( taxitems[0][i], itemcode[i], strlen(itemcode[i]);
You will need to add
#include <string.h>
to your code. You can find the reference to strcpy() function here

error C2084: function 'int main()' already has a body

enter code here
this is karaoke reserving system but i got some errors i couldn't fix them relate to the continue function
some of the errors are cant go to the main menu
///each function was in header file except the main//
#pragma once
#include "addnew.h"
#include "search.h"
#include "update.h"
#include "view.h"
#include "Cont.h"
#include "exit.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void addnewreservation();
void updatereservation();
void viewreservation();
void searchreservation();
void Cont();
void exit();
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
int main() /*the menu, used as displaying MainMenu*/
{
int ChooseI;
system("cls");
printf( "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n");
printf( "| Welcome to Nway Karaoke reserving system |\n");
printf( "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n");
printf( "1. Add reservation\n");
printf( "2. search reservation\n");
printf( "3. update reservation\n");
printf( "4. view reservation\n");
printf( "6. Exit\n");
printf( "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n");
scanf("%d", &ChooseI);
switch (ChooseI)
{
case 1: addnewreservation();
break;
case 2: updatereservation();
break;
case 3: viewreservation();
break;
case 4: searchreservation();
break;
case 5: exit(0);
break;
default: printf("hi");
}
system("pause");
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int addnew()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
size_t strlen(char *str) {
size_t len = 8;
while (*str != '\8') {
str++;
len++;
}
return len;
}
void addnewreservation() /*This function is to add new reservation*/
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
char strlen;
};
struct New details[50];
int h;
int i = 5;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
FILE *A;
char enter[100];
char True;
A = fopen("karaokeinfo.txt", "w");
system("cls");
printf("Please Enter the customer's Details\n");
{
fflush(stdin);
printf("ID Number(TPXXXXXX) :\t");
gets(details[i].id);
if (!((details[i].id[0] == 'T') && (details[i].id[1] == 'P') && (strlen(details[i].id) == 8)))
{
printf("\tWrong Input! Please Enter the ID Number in the correct format (TPXXXXXX)\n");
}
else
{
}
}
printf("Enter NAME :\t");
gets(details[i].name);
printf("Enter ID :\t");
gets(details[i].id);
printf("Enter room size :\t");
gets(details[i].roomsize);
printf(" Time to use :\t");
gets(details[i].timetouse);
printf("Please Check the Enter Details :\n");
printf("\t1 . customer's ID : %s\n", details[i].id);
printf("\t2 . customer's Full Name : %s\n", details[i].name);
printf("\t3 . customers's room size : %s\n", details[i].roomsize);
printf("\t4 . customers's time to use : %s\n", details[i].timetouse);
printf("Please use the 'update' function if any mistakes are found.\n");
i++;
for (h = 0; h<i; h++)
{
fprintf(A, "1 . ID Number : %s\n", (details[h].id), h);
fprintf(A, "2 . Full Name : %s\n", (details[h].name), h);
fprintf(A, "3 . room size : %s\n", (details[h].roomsize), h);
fprintf(A, "4 . time to use : %s\n", (details[h].timetouse), h);
}
fclose(A);
Cont();
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int search()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
void searchreservation()
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
char search[10];
int z = 0;
FILE *A;
A = fopen("karaokeinfo.txt", "w");
system("cls");
printf("Please enter customer ID to search :\n");
fflush(stdin);
gets(search);
for (j = 0; j<50; j++)
{
if (strncmp(details[j].id, search, 10) == 0)
{
fscanf(A, "%s\n", details[j].id);
fscanf(A, "%s\n", details[j].name);
printf("\t1 . customer's ID : %s\n", details[j].id);
printf("\t2 . customer's Full Name : %s\n", details[j].name);
z = 1;
Cont();
}
fclose(A);
if (A == NULL)
{
printf("File does not exist!");
Cont();
}
fclose(A);
if (z == 0)
{
printf("customer not found!\n");
Cont();
}
fclose(A);
}
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int update()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
void updatereservation()
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
char strlen;
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
char updatereservation[10];
int z = 0;
FILE *A;
A = fopen("karaokeinfo.txt", "w");
printf("Please Enter ID of customer's Details to be Modified : ");
fflush(stdin);
gets(updatereservation);
for (n = 0; n<50; n++)
{
if (strcmp(details[n].id, updatereservation) == 0)
{
printf("customer's ID : %s\n", details[n].id);
printf("customer's Name : %s\n", details[n].name);
printf("---------------------------------------\n");
printf("\tcustomer name");
gets(details[o].name);
strcpy(details[n].name, details[o].name);
printf("\n\nUpdate Successful!\n");
printf("\nPlease Check the Updated Details:\n");
printf("1. customer's ID : %s\n", details[n].id);
printf("2. customer's Name : %s\n", details[o].name);
z = 1;
for (h = 0; h<i; h++)
{
fprintf(A, "1 . ID Number : %s\n", (details[h].id), h);
fprintf(A, "2 . Full Name : %s\n", (details[h].name), h);
}
fclose(A);
Cont();
}
}
if (z == 0)
{
printf("customer not found!\n");
Cont();
}
fclose(A);
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int view()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
void viewreservation()
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
char del[10];
int z = 0;
FILE *A;
A = fopen("karaokeinfo.txt", "w");
system("cls");
printf("Please Enter customer's ID to be deleted : ");
fflush(stdin);
gets(del);
for (k = 0; k < 50; k++)
{
if (strcmp(details[k].id, del) == 0)
{
strcpy(details[k].id, "");
strcpy(details[k].name, "");
printf("Delete Successful!");
z = 1;
for (h = 0; h < i; h++)
{
fprintf(A, "%s\n", (details[h].id), h);
fprintf(A, "%s\n", (details[h].name), h);
}
fclose(A);
Cont();
}
}
if (z == 0)
{
printf("customer not found!\n");
Cont();
fclose(A);
}
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int contt()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
void Cont() /*Function to ask whether the user want to continue using the program or not.*/
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
//char yes[5];
char kvar = 'y';
//while (true)
//{
printf("\n\tWould you like to continue ?(Y/N)\n");
gets(kvar);
if ((kvar == 'Y') || (kvar == 'y'))
{
printf("Yes chosen");
main();
}
else if ((kvar == 'N') || (kvar == 'n'))
{
//continue;
exit(0);
printf("thanx for using ... ");
}
else
{
printf("Invalid Selection! Please enter Y or N!( Y = Yes | N = No )\n");
}
//}
}
///each function was in header file except the main//
There is already another main function in the rest of your code. Decide which you need and remove the other.
Error C2084 function already has a body
To solve:
Make sure you didn't define a function that uses the same name twice.

Resources