我正在做一项大学作业(job),当我的循环呼叫gets()
时,它会打印我想打印两次的行.这只会在第二个循环中发生.如果您在第一个循环中输入三角形并输入一些数字,则在循环结束并重新启动后,它将打印提示序列两次,而不是一次.
#include <stdio.h>
#include <string.h>
void main() {
const float pi = 3.1415926535; // Constant for pi
char choice[100] = ""; // String for inputted, also used for exiting loop
float height, width, base, area, radius, radians, verticalHeight;
int i = 1;
while (i < 2) {
puts("Area Calculator - Please choose one of the choices listed below\n----------------------------------------------------\nTriangle, Rectangle, Circle, Trapezium\n----------------------------------------------------");
printf("Enter choice: ");
gets(choice);
if (strcmp(choice, "Done") == 0 || (strcmp(choice, "done") == 0)) {
i++;
}
else if (strcmp(choice, "Triangle") == 0 || (strcmp(choice, "triangle") == 0)) {
printf("\nEnter vertical height: ");
scanf_s("%f", &verticalHeight);
printf("Enter base: ");
scanf_s("%f", &base);
printf("Area = %.2f\n\n", (0.5) * verticalHeight * base);
strcpy_s(choice, 100, "");
}
else if (strcmp(choice, "Rectangle") == 0 || (strcmp(choice, "rectangle") == 0)) {
puts("\nrectangle");
}
else if (strcmp(choice, "Circle") == 0 || (strcmp(choice, "circle") == 0)) {
puts("\ncircle");
}
else if (strcmp(choice, "Trapezium") == 0 || (strcmp(choice, "trapezium") == 0)) {
puts("\ntrapezium");
}
}
}
我试着用scanf_s()
代替gets()
,我可以让它像那样工作,但我的分配规定我必须使用gets()
.
任何帮助都将不胜感激.谢谢:)