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.
Related
/* problem of qsorting
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT "doc.txt"
#define OUTPUT "output.txt"
#define MAX_SIZE 500
typedef struct student // here I declare stuctures
{
char name[20];
int index;
int code;
int grade;
char surname[20];
} student;
int cmpfunc (const void * a, const void * b) // initial part of qsort
{
return ( *(char*)a - *(int*)b );
}
int main()
{
int i=0, m=0;
int number=0;
FILE *fo;
FILE *fi;
char file[]={INPUT};
char file1[]={OUTPUT};
fo = fopen(file, "r"); // open the file
fi = fopen(file1, "w"); // open the file
student *studentPtr;
studentPtr = (student*) malloc(MAX_SIZE*sizeof(student));
// here I use mallocation
if(fo == NULL || studentPtr == NULL || fi == NULL)
{
perror("Error");
exit(1);
}
while (!feof(fo)) // reading of file
{
fscanf(fo,"%d %s %s %d %d",
&studentPtr[i].index,
studentPtr[i].name,
studentPtr[i].surname,
&studentPtr[i].code,
&studentPtr[i].grade);
i++;
}
i=m;
printf("please insert a number upto 7:\n");
// because there are only 7 raws in the file
scanf("%d",&number);
m=number;
if (number > 7)
{
printf("try again");
}
else
{
for(i=0;i<m;i++)
{
qsort(studentPtr[i].surname, 1, sizeof(char), cmpfunc); // ?
printf("%d %s %s %d %d\n", // printing out
studentPtr[i].index,
studentPtr[i].name,
studentPtr[i].surname,
studentPtr[i].code,
studentPtr[i].grade);
fprintf(fi,"%d %s %s %d %d\n", // writing in the file
studentPtr[i].index,
studentPtr[i].name,
studentPtr[i].surname,
studentPtr[i].code,
studentPtr[i].grade);
}
}
free(studentPtr);
fclose(fo);
fclose(fi);
return 0;
}
I am just beginner and sorry for obvious mistakes.
I cannot do qsorting properly.
i will attach txt file as a comment of this post.
could you guys tell me how to change first part of qsort not to get silly things, after that i will try to do that by myself
why when attempt to use function N I get a Segmentation fault 11 .. im new to malloc so i dont exactly know where to look for my error .. im trying to read from a file and put certain line through fscanf to my double array ..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
void V(int *id,int i,int *praca, double *plat,int *datum, FILE **f)
{
char meno[2000],priezvisko[2000];
if ((*f = fopen("zam.txt", "r")) == NULL)
{
printf("Neda sa otvorit");
// koniec programu
return ;
}
while(//vypis informacii zamestnancov
fscanf(*f,"%d %s %s %d %lf %d",&id[i],&meno[i],&priezvisko[i],&praca[i],&plat[i],&datum[i])>0)
{
printf(" osobne cislo zamestnanca:%d\n meno priezvisko:%s %s\n administrativa/vyrobny pracovnik:%d\n plat:%g\n datum:%08d\n\n",id[i],&meno[i],&priezvisko[i],praca[i],plat[i],datum[i]);
i++;
}
}
void N(int i,double **platy, FILE *f)
{
rewind(f);
int j=0,k=0;
char line[1800];
int suma = 0;
int count = 0;
int lineNumber = 2;
double val;
double d;
while(fgets(line, sizeof(line), f)!= NULL)
{
if (count == lineNumber)
{
suma++; //pocet platov
count++;
lineNumber+=6;
*platy=malloc(suma*sizeof(double));
fscanf (f, "%lf", platy[i]);
i++;
}
else
{
count++;
}
}
printf("SUMA: %d",suma);
}
int main() //MAIN
{
int i=0,id[1800],praca[1800],datum[1800];
char *meno[1800],*priezvisko[1800],z;
double plat[1800],*platy;
FILE *f;
while((z = getchar())!='K')
{
if(z == 'V')
V(id,i,praca,plat,datum,&f);
if(z == 'P')
P(i,praca,plat,datum,f);
if(z == 'N')
N(i,&platy,f);
}
/*
for (j=0 ; j<suma; j++) {
printf("%g\n",*platy[j]);
}
*/
return 0;
}
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.
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;
}
I get a bad access error in the middle of the for loop, always when i=4. Does anybody know the reason for this? It works until i=4, but I don't see why I wouldn't get the bad access error in any other part of the for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXF 51
#define MAXFILE 200
int recommend(int fid, char *funcs[]){
int i;
for(i=0; i<fid; i++)
*funcs++;
printf("\nRecommended Function: %s\n", *funcs);
return 0;
}
int overlap(char *list[], char name[], int n){
int over=0, fid=202, i, j, k, m;
for(i=0; i<n; i++){
m=strlen(*list);
int lap=0;
for(j=0; j<(strlen(name)-1); j++){
for(k=0; k<m; k++)
if(list[i][k]==name[j]){
lap+=1;
break;
}
}
if(over<lap){
over=lap;
fid=i;
}
*list++;
}
return fid;
}
int readfile(char *flist[], FILE *fptr){
char a[MAXF];
int size=0;
while(fscanf(fptr, "%s\n", a) != EOF){
flist[size]=malloc(sizeof(char)*(1+strlen(a)));
strcpy(flist[size++],a);
}
return size;
}
int main () {
int n, id;
char fnname[MAXF], filename[MAXF], *flist[MAXFILE];
FILE *fp;
printf("Name of network file: ");
gets(filename);
printf("\nFunction Name: ");
gets(fnname);
fp=fopen(filename, "r");
if(fp==NULL)
printf("\nCould not open file.\n");
else {
n=readfile(flist, fp);
id=overlap(flist, fnname, n);
recommend(id, flist);
}
return 0;
}
It looks to me as if this:
m=strlen(*list);
should be:
m=strlen(list[i]);
And this:
*list++;
should not be there at all.