#include <stdio.h>
#include <stdlib.h>
struct packets
{
int source, destination, type, port;
char data[50];
};
int addpacket(int *, struct packets *);
void listpackets(int , struct packets *);
void save(int, struct packets *);
int main()
{
struct packets *info;
char choice;
int records = 0;
info = malloc(sizeof(struct packets));
do
{
puts("\n1. Add Packets\n"); //Menu with list of options.
puts("\n2. List all saved packets\n");
puts("\n3. Save packets\n");
puts("\n4. Clear all packet information\n");
puts("\nx. Exit the programme\n");
printf("\nPlease select your option: \n");
scanf("%c", &choice); //Reading the menu option entered.
if(choice == '\n') //if the choice is the new line character read again.
scanf("%c", &choice);
switch (choice)
{
case '1': system("cls"); //Clears the screen
puts("\nYou have selected to Add packet information, follow on-screen instructions\n"); records = addpacket(&records, info); //calls function "addpacket" and sents a copy of records and pointer to struct info.
break;
case '2': system("cls"); //clears the screen
puts("\nAll packet information will now be displayed on the screen\n"); listpackets(records, info);
break;
case '3': system("cls"); //clears the screen
puts("\nAll packet information will now be saved\n"); save(records, info);
break;
case '4': system("cls"); //clears the screen
puts("\nAll packet information will now be deleted\n");
break;
case 'x': puts("\nThe proframme will now close, Goodbye!\n"); //closes the programme.
break;
default : puts("\nIncorrect option, please select from menu\n"); //used if none of the menu options have been selected.
break;
}
}while (choice != 'x'); //The programme will run until the "x" option is entered at the menu.
return 0;
}
int addpacket(int *recCount, struct packets *info)
{
int validation = 0;
int stringlength = 0;
int i = 0;
char datatest[50];
do{
printf("\nPlease enter the source address: \n");
if (scanf("%i", &info[*recCount].source) == 1)
{
validation = 1;
}
else{
validation = 0;
getchar();
puts("\nThis is not a valid source address!\n");
}
}while (validation != 1);
printf("\nPlease enter destination address: \n");
scanf("%i", &info[*recCount].destination); //pointer to the destination address.
printf("\nPlease enter type: \n");
scanf("%i", &info[*recCount].type); // pointer to the address of the type.
printf("\nPlease enter the port: \n");
scanf("%i", &info[*recCount].port); //pointer to the port address.
printf("\nPlease enter data: \n");
scanf("%s", info[*recCount].data); //pointer to the data address.
*recCount ++; //adding one to record count
return *recCount; // returning the record count which will the be copied into records in main.
}
void listpackets(int records, struct packets *info)
{
int i;
for (i=0; i<records; i++){
printf("\nSource address: %i\n", info[i].source); //Displays the source address.
printf("\nDestination address: %i\n", info[i].destination); //Displays the destination address.
printf("\nType: %i\n", info[i].type); //Displays the type.
printf("\nPort: %i\n", info[i].port); //displays the port.
printf("\nData: %s\n", info[i].data); //displays the data information.
}
}
void save(int records, struct packets *info)
{
FILE *savedfile;
char filename[30] = { '\0'}; //this is where the file name will be stored.
int i;
printf("\nPlease enter a filename: \n");
scanf("%s", filename);
if ((savedfile = fopen(filename, "w")) == NULL)
{
printf("\n%s could not be opened\n", filename);
exit(1);
}
else
{
for (i=0; i<records; i++)
fprintf(savedfile, "%i %i %i %i %s", info[i].source, info[i].destination, info[i].type, info[i].port, info[i].data);
}
fclose(savedfile);
}
My save and listpacket function crash when i call them. this was working earlier until i introduced malloc into my programme along side some basic input validation. i think this could be an issue with pointers. my programme compiles with no errors/warning so i am stuck with what the problem could be. feel free to try compiling to programme and see what i mean. any help would be greatly appreciated as i am struggling to fix this issue.
You're using malloc() to allocate space for a single struct packets. You later treat your info pointer as if it is an array of records such structures... you're going beyond the boundary of where you have allocated space.
Details on one potential way around (that will auto-grow your array):
// change the signature of addpacket():
int addpacket(int *, struct packets **);
...
// change how add packet gets called:
records = addpacket(&records, &info);
// change addpacket():
int addpacket(int *recCount, struct packets **callerinfo)
{
...
// grow the buffer by one record
*callerinfo = realloc(*callerinfo, (*recCount + 1) * sizeof (**callerinfo));
struct packets *info = *callerinfo; // to avoid changing other code
... no other changes, just the rest of your routine
}
That will come close, of not completely solve it.
The complete program, working for me:
#include <stdio.h>
#include <stdlib.h>
struct packets
{
int source, destination, type, port;
char data[50];
};
int addpacket(int *, struct packets **);
void listpackets(int , struct packets *);
void save(int, struct packets *);
int main()
{
struct packets *info;
char choice;
int records = 0;
info = malloc(sizeof(struct packets));
do
{
puts("\n1. Add Packets\n"); //Menu with list of options.
puts("\n2. List all saved packets\n");
puts("\n3. Save packets\n");
puts("\n4. Clear all packet information\n");
puts("\nx. Exit the programme\n");
printf("\nPlease select your option: \n");
scanf("%c", &choice); //Reading the menu option entered.
if(choice == '\n') //if the choice is the new line character read again.
scanf("%c", &choice);
switch (choice)
{
case '1': system("cls"); //Clears the screen
puts("\nYou have selected to Add packet information, follow on-screen instructions\n"); records = addpacket(&records, &info); //calls function "addpacket" and sents a copy of records and pointer to struct info.
break;
case '2': system("cls"); //clears the screen
puts("\nAll packet information will now be displayed on the screen\n"); listpackets(records, info);
break;
case '3': system("cls"); //clears the screen
puts("\nAll packet information will now be saved\n"); save(records, info);
break;
case '4': system("cls"); //clears the screen
puts("\nAll packet information will now be deleted\n");
break;
case 'x': puts("\nThe proframme will now close, Goodbye!\n"); //closes the programme.
break;
default : puts("\nIncorrect option, please select from menu\n"); //used if none of the menu options have been selected.
break;
}
}while (choice != 'x'); //The programme will run until the "x" option is entered at the menu.
return 0;
}
int addpacket(int *recCount, struct packets **callerinfo)
{
int validation = 0;
int stringlength = 0;
int i = 0;
char datatest[50];
*callerinfo = realloc(*callerinfo, (*recCount + 1) * sizeof (**callerinfo));
struct packets *info = *callerinfo; // to avoid changing other code
do{
printf("\nPlease enter the source address: \n");
if (scanf("%i", &info[*recCount].source) == 1)
{
validation = 1;
}
else{
validation = 0;
getchar();
puts("\nThis is not a valid source address!\n");
}
}while (validation != 1);
printf("\nPlease enter destination address: \n");
scanf("%i", &info[*recCount].destination); //pointer to the destination address.
printf("\nPlease enter type: \n");
scanf("%i", &info[*recCount].type); // pointer to the address of the type.
printf("\nPlease enter the port: \n");
scanf("%i", &info[*recCount].port); //pointer to the port address.
printf("\nPlease enter data: \n");
scanf("%s", info[*recCount].data); //pointer to the data address.
++(*recCount); //adding one to record count
return *recCount; // returning the record count which will the be copied into records in main.
}
void listpackets(int records, struct packets *info)
{
int i;
for (i=0; i<records; i++){
printf("\nSource address: %i\n", info[i].source); //Displays the source address.
printf("\nDestination address: %i\n", info[i].destination); //Displays the destination address.
printf("\nType: %i\n", info[i].type); //Displays the type.
printf("\nPort: %i\n", info[i].port); //displays the port.
printf("\nData: %s\n", info[i].data); //displays the data information.
}
}
void save(int records, struct packets *info)
{
FILE *savedfile;
char filename[30] = { '\0'}; //this is where the file name will be stored.
int i;
printf("\nPlease enter a filename: \n");
scanf("%s", filename);
if ((savedfile = fopen(filename, "w")) == NULL)
{
printf("\n%s could not be opened\n", filename);
exit(1);
}
else
{
for (i=0; i<records; i++)
fprintf(savedfile, "%i %i %i %i %s\n", info[i].source, info[i].destination, info[i].type, info[i].port, info[i].data);
}
fclose(savedfile);
}
Related
Im basically Writing a program that creates, reads, updates and
deletes records in a binary file.
Everything compiles correctly, no syntax errors, but I do have some
bugs.
KNOWN BUGS
1.) Imputing any strings does not work, using fgets
2.) Ctrl-D Does Work but outputs a 'default' error before it exits.
3.) Update does not work (Not my main issue at the moment as the others are more important for now.)
4?) I'm not sure if the menu is working how it's supposed to work. I
think the do while is correct, since in the menu if I select and hit
CTRL-D it does exit the program. Just wanna be sure.
Right now I just want to know why, It is skipping the courseName in
the inputs function.
Here is my code thus far
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
typedef struct{
char courseName [64];
char courseSched [4];
unsigned int courseHours;
unsigned int courseSize;} COURSE;
FILE *pfileCourse;
int courseNumber = 0;
//Prototypes
void inputDetails(COURSE *c);
void readCourseRecord();
void createCourseRecord();
void print_menu();
void modifyCourseInfo();
void deleteCourse();
void display(COURSE c);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char choice; // this is the choice
printf("Enter one of the following actions or press CTRL-D to exit\n");
printf("C - Create a new course record\n");
printf("R - Read an existing course record\n");
printf("U - Update an existing course record\n");
printf("D - Delete an existing course record\n");
do{
choice = getchar();
switch(choice) {
case 'c':
case 'C':
printf("YOU PICKED C for Create\n");
createCourseRecord();
break;
case 'r':
case 'R':
printf("This is Choice R\n");
readCourseRecord();
break;
case 'u':
case 'U':
printf("Here is where you update an existing course\n");
modifyCourseInfo();
break;
case 'd':
case 'D':
printf("here is where you Delete an existing course record\n");
deleteCourse();
break;
default:
printf("Wrong Choice!\n");
}
}while(choice != EOF);
return 0;
}
void createCourseRecord() {
COURSE data;
pfileCourse = fopen("courses.dat", "ab");
printf("Please Enter The Details of The Course\n");
inputDetails(&data);
fwrite(&data, sizeof(data), 1, pfileCourse);
fclose(pfileCourse);
printf("Course Has Been Created!\n");
}
void inputDetails(COURSE *c) {
printf("Enter a course number: \n");
scanf("%d", &courseNumber);
printf("Enter a Course Name: \n");
fgets(c->courseName, sizeof(courseName), stdin);
printf("Enter the course schedule (MWF or TR): \n");
fgets(c->courseSched, 4, stdin);
fflush(stdin);
printf("Enter the course credit hours: \n");
scanf("%d",&c->courseHours);
fflush(stdin);
printf("Enter Number of Students Enrolled: \n");
scanf("%d",&c->courseSize);
return;
}
void readCourseRecord(){
COURSE data;
int flag = 0;
int readCourseNumber = 0;
printf("Please Enter a Course Number to Display\n");
scanf("%d", &readCourseNumber);
fflush(stdin);
pfileCourse = fopen("courses.dat", "rb");
while((fread(&data, sizeof(data), 1, pfileCourse)) > 0) {
if(readCourseNumber == courseNumber)
{
display(data);
flag = 1;
}
}
fclose(pfileCourse);
if(flag == 0)
printf("Course not Found!\n");
}
void deleteCourse(){
int newCourseNum;
COURSE data;
FILE *file2;
printf("Please Enter The Course You Wish You Delete\n");
scanf("%d", &newCourseNum);
pfileCourse = fopen("courses.dat", "rb");
file2 = fopen("temp.dat", "wb");
rewind(pfileCourse);
while((fread(&data, sizeof(data), 1, pfileCourse)) > 0)
{
if(courseNumber != newCourseNum)
{
fwrite(&data, sizeof(data), 1, file2);
}
}
fclose(file2);
fclose(pfileCourse);
remove("courses.dat");
rename("temp.dat", "courses.dat");
printf("%d was Successfully deleted\n", newCourseNum);
}
void modifyCourseInfo()
{
COURSE data;
int newCourseNum, found = 0;
printf("Modify\n");
printf("Please Enter The Course You Wish You Modify\n");
scanf("%d", &newCourseNum);
pfileCourse = fopen("courses.dat", "rb+");
while ((fread(&data, sizeof(data), 1, pfileCourse)) > 0 && found == 0)
{
if (courseNumber == newCourseNum)
{
display(data);
printf("Please Enter New Details\n");
inputDetails(&data);
fseek(pfileCourse, - (long)sizeof(data), 1);
fwrite(&data, sizeof(data), 1, pfileCourse);
printf("Course Updated\n");
found == 1;
}
}
fclose(pfileCourse);
if(found == 0)
printf("ERROR: course not found\n");
}
void display(COURSE c){
printf("courseNumber:\t %d\n", courseNumber);
printf("courseName:\t %s\n",c.courseName);
printf("courseSched:\t %s\n",c.courseSched);
printf("courseName:\t %d\n",c.courseHours);
printf("courseSize:\t %d\n",c.courseSize);
}
It doesn't skip courseName, courseName just gets value '\n' because scanf function stops reading your input BEFORE white space. Scanf ignores any whitespace characters encountered before the next non-whitespace character. So you can just add
scanf("%d[^\n]", &courseNumber);
getchar();
after every scanf you have but I'd recommend you to use fgets function for every interactive input.
I'm making a program that sort of acts like a student records system using an array of structures within structures. The program allows adding, editing and viewing student profile and their corresponding information. I'm having trouble with my displayAll function, when checking if a structure is empty. Supposedly if no subject information has been added to a student profile yet I'm supposed to display a message saying so and display their subject they're enrolled in otherwise. But I'm quite confused how to do so. Some tips would be much appreciated.
I've omitted some parts of the code to put emphasis on the displayAll function.
Someone pointed out this thread: Checking if an array of structs is empty or not, but it doesn't really halp me fully as I am dealing with an array of structures within an array of structures.
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
struct name{
char fname[30];
char lname[20];
char mi;
};
struct local{
char address[30];
char city[20];
};
struct subjs{
char courseCode[10];
char courseDes[20];
char grade;
};
struct student{
char id[8];
struct name studName;
struct local place;
struct subjs course[4];
};
void inputInfo(struct student *studInfo);
void addSubjects(struct student *studInfo);
void displayAll(struct student info[], int limit);
int main(){
struct student info[12];
int i=0, j, courseLimit=0;
char choice;
char idVerify[8];
do{
printf("MENU");
printf("\n\n[A] Add student Information");
printf("\n[B] Add student subject");
printf("\n[C] Edit student address or city");
printf("\n[D] Edit subject grade");
printf("\n[E] View individual student info/subjects");
printf("\n[F] View all students with their corresponding subjects");
printf("\n[g] Quit");
printf("\n\nEnter choice: ");
choice=tolower(getche());
system("cls");
switch (choice){
case 'a':
inputInfo(&info[i]);
i++;
break;
case 'b':
printf("Enter you id number for verification: ");
gets(idVerify);
for(j=0; j<i; j++){
if(strcmp(idVerify, info[j].id) == 0){
addSubjects(&info[j]);
}
else
printf("ID Number not found");
}
break;
case 'c':
//codes
break;
case 'd':
//codes
break;
case 'e':
//codes
break;
case 'f':
displayAll(info, i);
break;
case 'g':
printf("This program will now close.\nPress any key to continue.");
break;
default: printf("Invalid character. Try again");
break;
}
getch();
system("cls");
}while (choice!='g');
}
void inputInfo(struct student *studInfo){
//codes
}
void addSubjects(struct student *studInfo){
//codes
}
void displayAll(struct student info[], int limit){
int i, j;
if(limit == 0){
printf("Records are empty");
}
else{
for(i=0; i<limit; i++){
printf("\nStudent Name: %s %c %s", info[i].studName.fname, info[i].studName.mi, info[i].studName.lname);
printf("\nID Number: %s", info[i].id);
printf("\nAddress and city: %s, %s", info[i].place.address, info[i].place.city);
if(info[i].course[j].courseCode == 0){
printf("\nNo enrolled subjects");
}
else{
printf("\nSubjects:");
for(j=0; j<4; j++){
if(info[i].course[j].courseCode != 0){
printf("Subject %d", j+1);
printf("\nCourse Code: %s", info[i].course[j].courseCode);
printf("\nCourse Description: %s", info[i].course[j].courseDes);
printf("\nCourse Grade: %c", info[i].course[j].grade);
printf("\n");
}
}
}
}
}
}
You can use a flag to track wether a subject has been found in the subject for loop. I would name it found and clear it before the loop. Then set it within the loop, when a subject has been found. If the flag is still cleared after the loop, then print the desired message. To print the header "Subjects", you can check within the loop if a subject has been found (and printed) before.
Example code:
int found = 0; // clear flag
for(j=0; j<=4; j++){
if(info[i].course[j].courseCode != 0){
if(!found) { // if true then this will be the first subject to print
printf("\nSubjects:");
}
found = 1; // set flag
printf("Subject %d", j);
// the other printfs
}
}
if(!found) { // check flag
printf("No enrolled subjects.\n");
}
This replaces the whole
if(info[i].course[j].courseCode == 0){
...
} else {
...
}
block within the student loop.
I'm trying to write a simple phone book program. I have completed the first function and according to I observe it works without error. However, in second function (which is ""display()"") I can't show to user after I enter person knowledge. I'm working with binary mode. What the problem is in second function I couldn't understand. If you examine and help I'll be satisfied. Thanks in advance.
#include <stdio.h>
#include <stdlib.h> // "stdlib" library contains of exit() function
#include <malloc.h> // "malloc" library contains of malloc() function
#include <Windows.h> // "Windows" library contains of Sleep() function which waits the system as you want
#include <io.h> // "io" library contains of filelength() function
struct personKnowledge
{
char name[32];
char surname[32];
char number[32];
};
FILE *ptrFILE,*ptrFILE1;
long int recordLength,totalRecordLength,location;
static int counter = 0;
int number,totalRecordNumber;
void newRecord();
void display();
void deletE();
void add();
void update();
int main()
{
int choice;
do
{
printf("\n\t\t --- Phone Book Program ---");
printf("\n\n\t\t 1) New record"); // The options are being presented to user
printf("\n\n\t\t 2) Display person knowledge");
printf("\n\n\t\t 3) Delete someone");
printf("\n\n\t\t 5) Update person knowledge");
printf("\n\n\t\t 6) Exit");
printf("\n\n\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
newRecord();
break;
}
case 2:
{
display();
break;
}
case 3:
{
break;
}
case 4:
{
break;
}
case 5:
{
break;
}
case 6:
{
printf("\nWorking has been completed.\n");
return 0;
}
default:
{
printf("\nWrong entry! The program has been terminated.\n");
break;
}
}
} while (choice >= 1 && choice <= 6);
return 0;
}
void newRecord()
{
if ((ptrFILE = fopen("Phone Book.dat", "wb")) == NULL)
{
printf("The file couldn't open\n");
exit(0);
}
system("cls"); // Screen is being cleaned
struct personKnowledge *p; // p means person
p = (struct personKnowledge *)malloc(sizeof(struct personKnowledge)); // Memory is being allocated
fflush(stdin);
recordLength = sizeof(p); // size of p
printf("|| For the %d. person ||\n", counter+1);
printf("\n\Express person name: "); // User is entering the person's knowledge and they are being saved in file
gets(p->name);
printf("Express %s's surname: ", p->name);
gets(p->surname);
printf("Express %s's number: ", p->name);
gets(p->number);
fwrite(&(*p), recordLength, 1, ptrFILE);
printf("\nPlease wait, information is saving to file..\n");
Sleep(750);
printf("*-* Saving operation has been completed succesfully. *-*\n");
free(p);
counter++;
fclose(ptrFILE);
}
void display()
{
if ((ptrFILE = fopen("Phone Book.dat", "rb")) == NULL)
{
printf("The file couldn't open\n");
exit(0);
}
system("cls"); // Screen is being cleaned
struct personKnowledge *s; // s means searching
s = (struct personKnowledge *)malloc(sizeof(struct personKnowledge));
fflush(stdin);
recordLength = sizeof(s);
totalRecordLength = filelength(fileno(ptrFILE));
totalRecordNumber = totalRecordLength / recordLength;
printf("\n\nExpress person record number which you search: ");
scanf("%d", &number);
location = (number - 1)*recordLength;
fseek(ptrFILE, location, SEEK_SET);
fread(&(*s), recordLength, 1, ptrFILE);
printf("\n*-* Person knowledge which you search *-*\n");
Sleep(750);
printf("Name: %s\n", s->name);
printf("Surname: %s\n", s->surname);
printf("Number: %s\n", s->number);
free(s);
fclose(ptrFILE);
}
recordLength = sizeof(p);
is wrong, this is the size of the pointer which is normally 4 on a 32 bit system and 8 on a 64 bit syste.
you need
recordLength = sizeof(*p);
or
sizeof(struct personKnowledge);
which gives you the size of the structure pointed by p.
Update *
I have now tried to return something from the function and still the .exe crashes! I am quite new to c so sorry if I am been a bit thick at not spotting why.
struct packet* addRecord(int *rCount, struct packet *records){
int valid = 0; //used to indicated valid input
int length = 0; //used to store the string lengths
int i = 0; //used in the for loops
char dataTest[51]; //temporary storage of input to be checked before adding to records
do{
puts("What is the source of this packet?: ");
if(scanf(" %c", &records[*rCount].source) == 1){ //if correct insert the record at the index
valid=1; //determined by rCount(the current record count passed to addRecord
}
else{
valid = 0;
getchar();
puts("\nNot a valid input");
}
}while(valid!=1);
do{
puts("What is the destination of this packet?: ");
if(scanf(" %c", &records[*rCount].destination) == 1)
{
valid = 1;
}
else
{
valid = 1;
getchar();
puts("\nNot a valid input");
}
}
while(valid!=1);
records = realloc(records,(*rCount+1)*sizeof(struct packet));
return records;
}
So I have got this code to work, but when I enter a value for &records[*rCount].source, the .exe crashes. I have been looking at this code for an hour now and cannot find the broken link, but I feel like it's something simple.
Here is the little bit of code that I feel like is not working properly.
Also can someone please explain what == 1 means in the if statement, I've kinda just hacked this code together. Thanks
do{
puts("What is the source of this packet?: ");
if(scanf("%i", &records[*rCount].source) == 1){ //if correct insert the record at the index
valid=1; //determined by rCount(the current record count passed to addRecord
}
else{
valid = 0;
getchar();
puts("\nNot a valid input");
}
}while(valid!=1);
Full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct packet{ // declare structure for packet creation
int source;
int destination;
int type;
int port;
char data[51];
};
//function prototypes
void listRecords(int, struct packet*);
struct packet* addRecord(int*, struct packet*);
void save(int, struct packet*);
struct packet* open(int*, struct packet*);
int main ()
{
int recordCount = 0;
char choice;
struct packet *records;
struct packet *temp;
do {
printf("\nWhat would you like to do?\n");
printf("\t1) Add a packet.\n"); //---------------------//
printf("\t2) List all packets.\n"); //---------------------//
printf("\t3) Save packets.\n"); //---------MENU--------//
printf("\t4) Clear all packets.\n"); //---------------------//
printf("\t5) Quit the programme.\n"); //---------------------//
scanf("%i", &choice); // scan user input and put the entry into variable "choice"
if(choice == '/n')
scanf("%i", &choice);
switch(choice)
{
case 1: system("cls");
records = addRecord(&recordCount, records);
break;
case 2: system("cls");
break;
case 3: system("cls");
break;
case 4: system("cls");
break;
default: system("cls");
printf("%i was not a valid option\n", choice);
break;
}
}
while (choice != 5);
return 0;
}
struct packet* addRecord(int *rCount, struct packet *records){
int valid = 0; //used to indicated valid input
int length = 0; //used to store the string lengths
int i = 0; //used in the for loops
char dataTest[51]; //temporary storage of input to be checked before adding to records
do{
puts("What is the source of this packet?: ");
if(scanf("%i", &records[*rCount].source) == 1){ //if correct insert the record at the index
valid=1; //determined by rCount(the current record count passed to addRecord
}
else{
valid = 0;
getchar();
puts("\nNot a valid input");
}
}while(valid!=1);
do{
puts("What is the destination of this packet?: ");
if(scanf("%i", &records[*rCount].destination == 1))
{
valid = 1;
}
else
{
valid = 1;
getchar();
puts("\nNot a valid input");
}
}
while(valid!=1);
}
Change
if(scanf("%i", &records[*rCount].destination == 1))
to
if(scanf("%d", &records[*rCount].destination) == 1)
Also change %i to %d and char choice; to int choice;
Another problem is you are returning nothing from your function which has pointer to struct packet return type .
After some changes that I made the compiling code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct packet{ // declare structure for packet creation
int source;
int destination;
int type;
int port;
char data[51];
};
//function prototypes
void listRecords(int, struct packet*);
void addRecord(int*, struct packet*);
void save(int, struct packet*);
struct packet* open(int*, struct packet*);
int main (void)
{
int recordCount = 0;
int choice;
struct packet *records;
//struct packet *temp;
do {
printf("\nWhat would you like to do?\n");
printf("\t1) Add a packet.\n"); //---------------------//
printf("\t2) List all packets.\n"); //---------------------//
printf("\t3) Save packets.\n"); //---------MENU--------//
printf("\t4) Clear all packets.\n"); //---------------------//
printf("\t5) Quit the programme.\n"); //---------------------//
scanf("%d", &choice); // scan user input and put the entry into variable "choice"
if(choice == '\n')
scanf("%d", &choice);
switch(choice)
{
case 1: system("cls");
addRecord(&recordCount, records);
break;
case 2: system("cls");
break;
case 3: system("cls");
break;
case 4: system("cls");
break;
default: system("cls");
printf("%d was not a valid option\n", choice);
break;
}
}
while (choice != 5);
return 0;
}
void addRecord(int *rCount, struct packet *records){
int valid = 0; //used to indicated valid input
//int length = 0; //used to store the string lengths
//int i = 0; //used in the for loops
//char dataTest[51]; //temporary storage of input to be checked before adding to records
do{
puts("What is the source of this packet?: ");
if(scanf("%d", &records[*rCount].source) == 1){ //if correct insert the record at the index
valid=1; //determined by rCount(the current record count passed to addRecord
}
else{
valid = 0;
getchar();
puts("\nNot a valid input");
}
}while(valid!=1);
do{
puts("What is the destination of this packet?: ");
if(scanf("%d", &records[*rCount].destination) == 1)
{
valid = 1;
}
else
{
valid = 1;
getchar();
puts("\nNot a valid input");
}
}
while(valid!=1);
}
struct packet *records;
All well and good but you never actually created a struct packet for this pointer to point to. Therefore all access through this pointer is to invalid memory that does not belong to you.
I don't see any need for a pointer here. Simply declare it as:
struct packet records;
Then pass a pointer to that object:
case 1: system("cls");
addRecord(&recordCount, &records);
Notice that I've gotten rid of the return for addRecord; you simply do not need it. Make it return void. As it is now, you are taking one invalid pointer and overwriting it with another invalid pointer populated with randomness, since you never actually return anything. It's the same problem, just happening to trigger a crash due to the random value you get.
What is %i supposed to be doing? Are you looking for an integer? If so, you want %d (d for decimal).
== 1 checks that scanf successfully processed 1 item.
And what #haccks said about missing ).
I'm kind of new to programming C with Code::Blocks(Version 12.11), started this semester in my college, but I manage.
I recently learned in class about pointers, memory allocation and dynamic arrays(none are my forte), and I incorporated them in my program(and it compiles)
Now the problem comes when I run the Program and go to Menu -> Add a Product, the program terminâtes when I input a price and I receive "Process Returned -1073741819 (0xc0000005)".
I did some research and found out it's an access violation but I don't really understand how to correct it.
Regards
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxCharName 64
#define maxCharUserPass 8
#define maxCharId 5
// Product Structure
typedef struct{
char name[maxCharName];
char idCode[maxCharId];
float price;
float stock;
}product;
// DataBase of Products
typedef struct{
int sizeT;
product *array;
} TProducts;
TProducts a;
// Press any Key on the Keyboard to Proceed
void pressKeyToContinue(){
puts("\n\n\n Please Press any Key to Continue...");
getchar();
}
// Start Page
void startPage(){
puts("\n\n\n -- Welcome to WeePro Manager --\n\n");
puts(" -- Version 1.0 --\n\n\n\n");
puts(" -- Developped By: Nick --");
pressKeyToContinue();
system("cls");
}
// Program Terminator
void shutdown(){
puts("\n\n\n Good-Bye");
pressKeyToContinue(); // Awaits User Input
exit(0);
}
// Asks User Information for Verification
int userLogin(){
char userName[maxCharUserPass] = "WPuser";
char inputUserName[maxCharUserPass];
char passWord[maxCharUserPass] = "12345";
char inputPassWord[maxCharUserPass];
printf("Username? ");
scanf("%s",inputUserName); fflush(stdin);
printf("Password? ");
scanf("%s", inputPassWord); fflush(stdin);
system("cls");
if((strcmp(userName, inputUserName) == 0)&&(strcmp(passWord, inputPassWord) == 0)){
return 1;
}else{ return 0;}
}
// Lists All Products With their Respective Information
void listAll(){
int idx = 0;
puts("List:");
while((idx < a.sizeT)&&(a.array[idx].name != NULL)){
printf(":::%s ( id: %s )", a.array[idx].name, a.array[idx].idCode);
printf("Price: %6.2f eur/g", a.array[idx].price);
printf("Stock: %6.2f g", a.array[idx].stock);
idx++;
}
pressKeyToContinue();
system("cls");
}
// Input Product ID Code
char* inputIdCode(){
char* tempIdCode;
puts("ID Code?");
scanf("%s", tempIdCode);
system("cls");
return tempIdCode;
}
// Search By ID Code
int searchIdCode(){
int idx = 0;
char* tempIdCode;
tempIdCode = inputIdCode();
do{
if(strcmp(a.array[idx].idCode, tempIdCode) == 0){
return idx;
}else{
idx++;
}
}while(idx < a.sizeT);
puts("No Product With Such an ID Code!");
return -1;
}
// Input Product Name
char *inputProductName(int length){
char name[maxCharName];
puts("Product Name?");
scanf("%s", name); fflush(stdin);
system("cls");
return name;
}
// Input Product Price
float inputProductPrice(int length){
float price;
puts("Product Price?");
scanf("%f", price); fflush(stdin);
system("cls");
return price;
}
// Input Product Stock
float inputProductQuantity(int length){
float quantity;
puts("Product Stock?");
scanf("%f", quantity); fflush(stdin);
system("cls");
return quantity;
}
/////////////////
// Add Product //
/////////////////
// New Product Adder
void addProduct(){
char* tempStr;
float temp;
if(a.sizeT == 0){
a.sizeT = 1;
a.array = (product*)malloc((a.sizeT)*sizeof(product));
}else{
a.sizeT++;
a.array = (product*)realloc(a.array, (a.sizeT)*sizeof(product));
}
tempStr = inputProductName(a.sizeT);
strcpy(a.array[a.sizeT].name, tempStr);
temp = inputProductPrice(a.sizeT);
temp = inputProductQuantity(a.sizeT);
system("cls");
}
void transaction(){}
////////////////////
// Delete Product //
////////////////////
// Delete Product
void deleteProduct(){
int idx, idxPro;
char* tempIdCode;
puts("Delete Which Product?\n");
tempIdCode = inputIdCode();
idxPro = searchIdCode(tempIdCode);
idx = idxPro + 1;
while(idx < a.sizeT){
a.array[idx] = a.array[idx+1];
idx++;
}
a.array = realloc(a.array, (a.sizeT-1)*sizeof(product));
}
//Product Information Modifier
void modifyProduct(){
char choice;
int tabLength;
do{
puts("Modify What?\n");
puts(" -> [N]ame\n");
puts(" -> [P]rice\n");
puts(" -> [S]tock\n\n");
puts(" -> [R]eturn to Previous Menu"); // Prints the Menus' Options
scanf("%c", &choice);
choice = toupper(choice); // Save Users' Choice And Up Case
fflush(stdin);
switch(choice){
case 'N':
system("cls");
tabLength = searchIdCode();
inputProductName(tabLength);
break;
case 'P':
system("cls");
tabLength = searchIdCode();
inputProductPrice(tabLength);
break;
case 'S':
system("cls");
tabLength = searchIdCode();
inputProductQuantity(tabLength);
break;
case 'R':
system("cls");
returnToMenu2();
break;
default:
puts("Something Went Wrong!\n");
pressKeyToContinue();
system("cls");
}
}while(choice != 'o');
}
// Sub-Menu Interface
void menu(){
char choice;
do{
puts("Please Make Your Selection.\n");
puts(" -> [A]dd a New Product\n");
puts(" -> [M]odify a Product\n");
puts(" -> [D]elete a Product\n\n");
puts(" -> [R]eturn to Main Menu"); // Prints the Menus' Options
scanf("%c", &choice); fflush(stdin);
choice = toupper(choice); // Save Users' Choice And Up Case
switch(choice){
case 'A':
system("cls");
addProduct();
break;
case 'M':
system("cls");
modifyProduct();
break;
case 'D':
system("cls");
deleteProduct();
break;
case 'R':
system("cls");
returnToMenu1();
break;
default:
puts("Something Went Wrong!\n");
pressKeyToContinue();
system("cls");
}
}while(choice != 'o');
}
// Return To Ma
> Blockquote
in Menu
void returnToMenu2(){
menu();
}
// Main Menu
void controlPanel(){
char choice;
do{
puts("Please Make Your Selection.\n");
puts(" -> [T]ransaction\n");
puts(" -> [M]enu\n");
puts(" -> [L]ist\n");
puts(" -> [S]hutdown"); // Prints the Panels' Options
scanf("%c", &choice); fflush(stdin);
choice = toupper(choice); // Save Users' Choice And Up Case
switch(choice){
case 'T':
system("cls");
transaction();
break;
case 'M':
system("cls");
menu();
break;
case 'L':
system("cls");
listAll();
break;
case 'S':
system("cls");
shutdown();
break;
default:
puts("Something Went Wrong!\n");
pressKeyToContinue();
system("cls");
}
}while(choice != 'o');
}
// Return To Main Menu
void returnToMenu1(){
controlPanel();
}
int main(){
int loginSuccess=1;
//loginSuccess = userLogin();
switch(loginSuccess){
case 0:
shutdown();
break;
case 1:
startPage();
controlPanel();
break;
}
}
An attempt is being made to write to randon memory, via the uninitialized pointer tempIdCode:
char* inputIdCode(){
char* tempIdCode;
puts("ID Code?");
scanf("%s", tempIdCode);
system("cls");
return tempIdCode;
}
You need to allocate memory for tempIdCode before attempting to write to it. You must use malloc() here (and not return the address of a local array):
char* tempIdCode = malloc(20);
if (tempIdCode)
{
/* The format specifier "%19s" instructs scanf()
to read at most 19 characters, one less than
allocated to allow for terminating null character
written by scanf(), to prevent potential buffer
overrun. */
scanf("%19s", tempIdCode);
}
The caller of the function must explicitly check for a return NULL pointer. The caller must also free() the allocated memory.
This is a killer:
// Input Product Name
char *inputProductName(int length){
char name[maxCharName];
puts("Product Name?");
scanf("%s", name); fflush(stdin);
system("cls");
return name;
}
The reference returned by this function points to an already freed block of memory on the stack, as char name is only valid as long the program is inside the function name is declared in.
When leaving the function name is freed automagically, so any dereferencing of the function's result leads to UB, as it most propably will be accessing unallocated memory.
To solve this you might like to pass in a buffer, where to read the data into:
// Input Product Name
char * inputProductName(int length, char * name){
puts("Product Name?");
scanf("%s", name); fflush(stdin);
system("cls");
return name;
}
and call it like this:
// New Product Adder
void addProduct(){
char* tempStr;
float temp;
if(a.sizeT == 0){
a.array = malloc((a.sizeT)*sizeof(product));
}else{
a.array = realloc(a.array, (a.sizeT)*sizeof(product));
}
a.sizeT++;
inputProductName(a.sizeT, a.array[a.sizeT].name);
...