#include <stdio.h>
//defined structure
struct option {
char *string;
float price;
};
float ask(const char *prompt, size_t n, const struct option options[n]) {
for(;;) {
printf("%snn", prompt);
for(int i = 0; i < n; i++) {
printf("[%d] %s - $%.2fn"
"-----------------------------------------------------n",
i + 1,
options[i].string,
options[i].price
);
}
printf("n");
int o;
if(scanf("%d", &o) != 1) {
printf("Option could not be read.nn");
while(getchar() != 'n');
continue;
}
printf("n");
for(int i = 0; i < n; i++) {
if(o == i + 1)
return options[i].price;
}
printf("Option %d was not valid.n", o);
}
}
int main(void) {
for(;;) {
float service = ask(
"-------------Welcome-------------n"
"**********M.A.C PETS SPA*********n"
" choose from our Specialsn"
"---------------------------------",
3,
(const struct option []) {
{ "CLEAN UP Special includes General shower and haircut", 20 },
{ "THE WORKS Special includes General shower, haircut, ear cleaning, and nail trim", 30
},
{ "FULL GROOM Special includes Breed appropriate shower, specific haircut, nail trim, ear cleaning, bandana and cologne", 40 }
}
);
float size = ask(
"What size is your dog?",
3,
(const struct option []) {
{ "Small", 0 },
{ "Medium", 5 },
{ "Large", 15 },
}
);
int numPhone;
char petName;
printf("what is the name of your pet?n");
scanf("%s", &petName);
printf("what is your phone number so you can pick up your friend later:n");
scanf("%d", &numPhone);
printf("n tt 22******************************22 n");
printf(" tt **********************Receipt***************n");
printf("tttDogs name: %s n");
printf("tttOwners Number: %d n");
printf("Total Price including extra charge for the size is = $%.2fnn", service + size);
}
}
这是代码,我遇到的麻烦是收据的代码块,我仍然要清理它,但我只是想得到我想要的答案。
我期待收据显示业主的电话号码和他们的宠物名字从扫描在前几行,我认为它会工作得很好,因为它是一个简单的扫描,但它不工作。我很感激你的帮助,谢谢!!!!
您需要为petName分配一个足够大的char数组,以容纳预期的答案,例如:char petName[64]
printf语句没有指定打印哪个变量。它们应该是这样的:
PP_4