I am writing the function, getCString which takes in 3 parameters. The first parameter is the user inputted string. The second parameter is the minimum number of characters the string has to be and the third parameter is supposed to be the maximum number of characters allowed for the string. Below is the function which calls getCString: (Note that the first input 'horse' is a test input to show error messages and catch errors)
void test08_getCString(void) {
char cstringValue[7];
printf("TEST #8: - Instructions:\n"
"1) Enter the word 'horse' [ENTER]\n"
"2) Enter the word 'BERLIN' [ENTER]\n"
":>");
// You may want to comment the next line if you have not yet created the getInteger function:
getCString(cstringValue, 6, 6);
printf("////////////////////////////////////////\n");
printf("TEST #8 RESULT: ");
printf("%s (Answer: BERLIN)\n", cstringValue);
printf("////////////////////////////////////////\n\n");
}
Basically this function wants the user to input a string and accept 6 as the minimum value and 6 as the maximum value for the number of characters allowed.
This is what I have so far:
Note that if the minimum and maximum are the same the error code:
**("Error: String length must be exactly %d chars: ", max)** will appear.
If the minimum and max are different and the user enters a value greater than the maximum. Then this will appear:
**("ERROR: String length must be no more than %d chars.", max)**
If the minimum and max are different and the user enters a value less than the minimum. Then this will appear:
**("ERROR: String length must be between %d and %d chars: ", min, max)
**
char getCString(char *text[], int min, int max) {
int i = 0;
scanf("%s", *text);
while (*text[i] != '0') {
i++;
}
while (min == max && (i > max || i < min)) {
printf("Error: String length must be exactly %d chars: ", max);
}
while (min != max) {
if (i > max) {
printf("ERROR: String length must be no more than %d chars.", max);
}
if (i < min) {
printf("ERROR: String length must be between %d and %d chars: ", min, max);
}
}
return *text;
}
This is what it looks like in my header file:
char getCString(char*[], int, int);
However when I compile the program, I get this error code: **Exception thrown at 0x7B2C98F1 (ucrtbased.dll) in A1MS1.c.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.
this error code is shown on this line:
scanf("%s", *text);
If anyone can explain to me where in my program there is an issue. Whether that be in the header file or if I am not using the pointer syntax correctly, please let me know.