Note that there are some explanatory texts on larger screens.

plurals
  1. PO.exe crashes when I enter a vaue for &records[*rCount].source
    primarykey
    data
    text
    <p>Update <em>*</em></p> <p>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.</p> <pre><code>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", &amp;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", &amp;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; </code></pre> <p>}</p> <p>So I have got this code to work, but when I enter a value for &amp;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.</p> <p>Here is the little bit of code that I feel like is not working properly.</p> <p>Also can someone please explain what == 1 means in the if statement, I've kinda just hacked this code together. Thanks</p> <pre><code>do{ puts("What is the source of this packet?: "); if(scanf("%i", &amp;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); </code></pre> <p>Full code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; 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", &amp;choice); // scan user input and put the entry into variable "choice" if(choice == '/n') scanf("%i", &amp;choice); switch(choice) { case 1: system("cls"); records = addRecord(&amp;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", &amp;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", &amp;records[*rCount].destination == 1)) { valid = 1; } else { valid = 1; getchar(); puts("\nNot a valid input"); } } while(valid!=1); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload