1

I get the warning "warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]" and also when I run the program it wont read any input I give it. I've been spending hours figuring out ways to make this program work due to being new with programming. Any help would be much appreciated!

int oilChange = 35;
int tireRotation = 19;
int carWash = 7;
int carWax = 12;
int totPrice = 0;
char service1[15];
char service2[15];

printf("Davy's auto shop services\n");                                  //printing the menu
printf("Oil change -- $%d\n", oilChange);
printf("Tire rotation -- $%d\n", tireRotation);
printf("Car wash -- $%d\n", carWash);
printf("Car wax -- $%d\n\n", carWax);

printf("Select first service: ");                                      //prompting and reading first service
scanf("%[^\n]%s", service1);
printf("%s", service1);

printf("Select second service: ");                                     //prompting and reading service service
scanf("%[^\n]%s", service1);
printf("%s", service2);
  • First of all, your use of scanf() is unsafe, e.g. if more than 14 characters are entered. Lookup "Buffer Overflow" for more. It is a common security vulnerability. – resiliware Oct 06 '20 at 03:44
  • Please [edit] your post to add the full error message, and indicate which line this error occurred on. The code looks OK as is, so we need more information. – Ken Y-N Oct 06 '20 at 03:44

1 Answers1

4

The input format:

scanf("%[^\n]%s", service1);

has two conversion specifications — the scan set and the string. You've only provided one variable. That's why you're getting the compiler warning. You probably want something like:

scanf("%s", service1);

The string conversion will skip white space, including newlines, and read a single word into service1.

Or it's possible you want:

scanf(" %[^\n]", service1);

That also skips white space, then reads the rest of a line into service1.

All scanf() conversion specifiers except %c, %[…] (scan sets) and %n skip white space automatically.

You should also prevent buffer overflow with scanf() by using "%14s" or " %14[^\n]".

Note that you prompt for "Select second service" but read the value into service1 again (not service2). You then print the value from service2. You should change the 1 to 2 in the scanf() line, as well as fixing the format string.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278