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);
}