Reputation: 185
I am looking at putting some validation into this loop to make sure then entry is greater or equal to and less than or equal to 1024.
I have tried
if(scanf("%d", &records[*rCount].source) == 1 && &records[*rCount].source >= 1 && &records[r*Count].source <= 1024)
but that stops all entries working.
Below is the do while loop I am currently using thanks.
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);
Upvotes: 0
Views: 59
Reputation: 2589
You are testing the address of the desired variable after you've read it.
You pass &records[*rCount].source
, the address of records[*rCount].source
(note the extra ampersand at the beginning) to scanf, so the function will save what it's reading from the standard input into this address. After that, you want to compare the variable value, not its address, against your limits.
Your corrected if
will be:
if (scanf("%d", &records[*rCount].source) == 1
&& records[*rCount].source >= 1
&& records[*rCount].source <= 1024)
{
// do stuff...
}
Upvotes: 2