gpt4 book ai didi

c - c 中的区域程序出现问题

转载 作者:行者123 更新时间:2023-11-30 20:35:50 25 4
gpt4 key购买 nike

所以我一直在开发一个可以找到形状面积的程序我的编译器上出现多个错误,表示我正在比较一个指针和一个整数,但我不是……至少我不认为我是。程序运行,但是当我输入字符串时,它崩溃了

这是我的代码

#include <stdio.h>
#include <math.h>
int main(void){

char str_1;

printf("Hello! This program will find the area of selceted figrues!\n");
printf("What is your shape? The only shapes that work are Triangles,\
Rectangles, and Circles\n");

scanf("%c", str_1);
do {
printf("That is an invalid input the only shapes that work are Triangles, \
Rectangles, and Circles\n");
scanf("%c", str_1);
}
while(str_1 != "circle", "rectangle", "triangle");

if (str_1 == "circle") {
printf("What is the radius?\n");
scanf("%i");
}
}

最佳答案

您应该使用 char *char[] 作为字符串并重构您的程序流程:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <memory.h>

void msg() {
printf("What is your shape? The only shapes that work are Triangles,\
Rectangles, and Circles\n");
}

int main(void) {
char *str_1 = malloc(32);
float f, f2, f3;
printf("Hello! This program will find the area of selected figures!\n");
msg();
scanf("%s", str_1);
for (; ;) {
if (strcmp(str_1, "circle") && strcmp(str_1, "rectangle") && strcmp(str_1, "triangle")) {
printf("That is an invalid input the only shapes that work are Triangles, \
Rectangles, and Circles\n");
msg();
scanf("%s", str_1);
}
if (!strcmp(str_1, "circle")) {
printf("What is the radius?\n");
scanf("%f", &f);
printf("The area is %f\n", f * M_PI * M_PI);
msg();
scanf("%s", str_1);
continue;
}
else if (!strcmp(str_1, "rectangle")) {
printf("What is the length?\n");
scanf("%f2", &f2);
printf("What is the height?\n");
scanf("%f3", &f3);
printf("The area is %f\n", f2*f3);
msg();
scanf("%s", str_1);
continue;
}
}
free(str_1); /* unreachable */
}

测试

Debug/gnu
Hello! This program will find the area of selected figures!
What is your shape? The only shapes that work are Triangles,Rectangles, and Circles
circle
What is the radius?
5
The area is 49.348022
What is your shape? The only shapes that work are Triangles,Rectangles, and Circles
cube
That is an invalid input the only shapes that work are Triangles, Rectangles, and Circles
What is your shape? The only shapes that work are Triangles,Rectangles, and Circles
rectangle
What is the length?
2
What is the height?
4
The area is 8.000000
What is your shape? The only shapes that work are Triangles,Rectangles, and Circles

关于c - c 中的区域程序出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37601643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com