C语言字符串比较编译错误:需使用显式字符串比较函数
解决C语言字符串比较错误及代码优化方案
核心问题解决:字符串比较错误
你遇到的编译错误是因为在C语言中,不能直接用==比较字符串内容。choice_1是字符数组的首地址,"1"是字符串字面量的内存地址,==比较的是这两个地址是否相同,而非字符串内容是否一致。
要正确比较字符串内容,需使用<string.h>头文件提供的strcmp函数:
- 当
strcmp(str1, str2)返回0时,说明两个字符串内容完全相等 - 返回正数表示
str1大于str2,负数表示str1小于str2
把你的判断代码修改为:
if (strcmp(choice_1, "1") == 0) { // 2D图形处理逻辑 } else if (strcmp(choice_1, "2") == 0) { // 3D图形处理逻辑(可后续补充) } else { printf("无效输入,请输入1或2\n"); }
代码其他问题修复
除了字符串比较,你的代码还有几个会导致运行异常的问题:
1. Switch Case缺少break
每个case执行完后没有break;,会导致程序继续执行后续所有case的代码(比如选了正方形后会接着执行矩形、三角形的逻辑)。在每个case的末尾添加break;:
case '1':{ // 正方形逻辑 break; // 退出switch }
2. 字符输入读取残留换行符
scanf("%c",&_2dshape);会读取上一次scanf后留在输入缓冲区的换行符,导致直接跳过输入。修改为:
scanf(" %c",&_2dshape); // 前面加空格,自动跳过空白字符(换行、空格等)
3. 公式错误修正
- 三角形面积:应该是
(底×高)/2,你代码里没除以2 - 菱形面积:对角线乘积的一半,修改为
(diagonal1 * diagonal2) / 2 - 梯形面积:正确公式是
(上底+下底)×高/2,你的代码写成了length*diagonal1*diagonal2,要改成(diagonal1 + diagonal2) * length / 2 - 风筝面积:应该是对角线乘积的一半,不是直接相乘
4. 冗余变量与输入格式问题
_3dshape变量未使用,可以删除scanf("%f\n%f",&length,&breadth);里的\n多余,会导致输入异常,改成scanf("%f %f",&length,&breadth);
修改后的完整代码
#include<stdio.h> #include <string.h> #define PI 3.14159 int main() { char name[100], choice_1[100], shape_choice; float length, breadth, SA, perimeter, diagonal1, diagonal2, height; printf("What should we call you?\n>>"); scanf("%s", name); printf("Hello %s :)\nThis Program will help you calculate the SA, Volume, Perimeter and Circumference of your geometric shapes!\nInput your choice below:\n", name); printf("What Shapes are we looking at?\n(1)2 Dimensional (2)3 Dimensional\n>>"); scanf("%s", choice_1); if (strcmp(choice_1, "1") == 0) { printf("Which Shape's Area and Perimeter do you want to find out?\n1.Square 2.Rectangle 3.Triangle 4.Rhombus 5.Parallelogram 6.Kite 7.Trapezoid\n>>"); scanf(" %c", &shape_choice); switch(shape_choice){ case '1':{ printf("You chose square\nThe formula of perimeter of square is 4*side and that of area of square is side*side\nEnter the length of each side of square below:\n>>"); scanf("%f", &length); perimeter = 4 * length; printf("The Perimeter of Square is: %.2f units\n", perimeter); SA = length * length; printf("The Area of Square is: %.2f square units\n", SA); break; } case '2':{ printf("You chose Rectangle\nThe formula of perimeter of Rectangle is 2*(length+breadth) and that of area of Rectangle is length*breadth\nEnter the values of length and breadth below:\n>>"); scanf("%f %f", &length, &breadth); perimeter = 2 * (length + breadth); printf("The Perimeter of Rectangle is: %.2f units\n", perimeter); SA = length * breadth; printf("The Area of Rectangle is: %.2f square units\n", SA); break; } case '3':{ printf("You chose Triangle\nThe formula of perimeter of Triangle is sum of all three sides and that of area of Triangle is (base*height)/2\nEnter the values of three sides below:\n>>"); float side2, side3; scanf("%f %f %f", &length, &side2, &side3); perimeter = length + side2 + side3; printf("The Perimeter of Triangle is: %.2f units\n", perimeter); printf("Enter the values of base and height below:\n>>"); scanf("%f %f", &length, &height); SA = (length * height) / 2; printf("The Area of Triangle is: %.2f square units\n", SA); break; } case '4':{ printf("You chose Rhombus\nThe formula of perimeter of Rhombus is 4*side and that of area of Rhombus is (product of diagonals)/2\nEnter the length of a side below:\n>>"); scanf("%f", &length); perimeter = 4 * length; printf("The Perimeter of Rhombus is: %.2f units\n", perimeter); printf("Enter the values of the two diagonals below:\n>>"); scanf("%f %f", &diagonal1, &diagonal2); SA = (diagonal1 * diagonal2) / 2; printf("The Area of Rhombus is: %.2f square units\n", SA); break; } case '5':{ printf("You chose Parallelogram\nThe formula of perimeter of Parallelogram is 2*(side1+side2) and that of area of Parallelogram is base*height\nEnter the values of side 1 and side 2 below:\n>>"); scanf("%f %f", &length, &breadth); perimeter = 2 * (length + breadth); printf("The Perimeter of Parallelogram is: %.2f units\n", perimeter); printf("Enter the values of base and height below:\n>>"); scanf("%f %f", &length, &height); SA = length * height; printf("The Area of Parallelogram is: %.2f square units\n", SA); break; } case '6':{ printf("You chose Kite\nThe formula of perimeter of Kite is 2*(side1+side2) and that of area of Kite is (product of diagonals)/2\nEnter the values of the two unequal sides below:\n>>"); scanf("%f %f", &length, &breadth); perimeter = 2 * (length + breadth); printf("The Perimeter of Kite is: %.2f units\n", perimeter); printf("Enter the values of the two diagonals below:\n>>"); scanf("%f %f", &diagonal1, &diagonal2); SA = (diagonal1 * diagonal2) / 2; printf("The Area of Kite is: %.2f square units\n", SA); break; } case '7':{ printf("You chose Trapezoid\nThe formula of perimeter of Trapezoid is sum of all sides and that of area of Trapezoid is (sum of two parallel sides)*height/2\nEnter the values of all four sides below:\n>>"); float side3, side4; scanf("%f %f %f %f", &length, &breadth, &side3, &side4); perimeter = length + breadth + side3 + side4; printf("The Perimeter of Trapezoid is: %.2f units\n", perimeter); printf("Enter the values of the two parallel sides and height below:\n>>"); scanf("%f %f %f", &diagonal1, &diagonal2, &height); SA = (diagonal1 + diagonal2) * height / 2; printf("The Area of Trapezoid is: %.2f square units\n", SA); break; } default:{ printf("无效的选择,请输入1-7之间的数字\n"); break; } } } else if (strcmp(choice_1, "2") == 0) { printf("3D图形功能将在后续版本中添加\n"); } else { printf("无效输入,请输入1或2\n"); } return 0; }
内容的提问来源于stack exchange,提问作者Daksh Shukla
相关产品推荐
相关产品推荐

