【问题标题】:Trouble with an area program in cc中的区域程序有问题
【发布时间】:2016-10-02 18:26:27
【问题描述】:

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

这是我的代码

#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 数组。
  • 使用strcmp()......
  • while(str_1 != "circle", "rectangle", "triangle"); C 不会那样做。你用的是什么教材?我们将向您介绍适当的函数和逻辑运算符。
  • scanf("%c", str_1); -> scanf(" %c", &amp;str_1); 在格式字符串中添加一个空格并在str_1 上使用&amp; 运算符。
  • 相信编译器。它比你更懂 C。

标签: c math area


【解决方案1】:

您应该对字符串使用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

【讨论】:

  • 除非你为 str_1 分配内存,否则它会爆炸。
  • @RSahu 好的,我会立即改进代码..你的意思似乎是我们需要做 malloc
  • 要么,要么保留char 类型的str_1 并在&amp;str_1 中使用&amp;str_1
  • 我在您的程序中看到的一个问题是它没有考虑到使用负数的用户。我的也没有,这是我刚刚注意到的
猜你喜欢
  • 2018-12-06
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-12
  • 2012-07-16
  • 1970-01-01
  • 2017-04-30
相关资源
最近更新 更多