c - 扫描字符然后在 IF 语句中使用它们? (如果 char= ='c' ){} 等

标签 c

到目前为止我的代码:

#include <stdio.h>

main() 
{
    float p_Asphalt = 5.2, p_Concrete = 4.93, p_Stones = 2.21;
    float c_Concrete = 8.88; 
    float d_Length =, d_Width;
    char typeOfPaving, c_Curbing;
    float totalCost = 0;
    float GST = 1.13, PST = 1.10;

    printf("asphalt company\n\n");
    printf("Length of Driveway: ");
        scanf("%f", &d_Length);
    printf("Width of Driveway: ");
        scanf("%f", &d_Width);
    printf("\nType of Paving:\n");
    printf(" 'A' for asphalt paving\n");
    printf(" 'C' for concrete paving\n");
    printf(" 'S' for paving stones\n\n");
    printf("Select (A,C,S): ");
        scanf("%c%c", &typeOfPaving);
    printf("%c", typeOfPaving);

    if (typeOfPaving == "A") 
    {
        totalCost = p_Asphalt * (d_Length * d_Width);
        printf("Concrete curbing? [y/n]: ");

            scanf("%c%c", c_Curbing);

            if (c_Curbing == 'y') { 
                totalCost = totalCost + (d_Length * c_Concrete);
            }
            else {
                totalCost = totalCost * GST;
                printf("Quoted Price of Paving: %f\n", totalCost);
            }
    } 
    else if (typeOfPaving == "C") 
    {
        totalCost = p_Concrete * (d_Length * d_Width);
        printf("Quoted Price of Paving: %f\n", totalCost);
    } 
    else if (typeOfPaving == "S") 
    {
        totalCost = p_Stones * (d_Length * d_Width);
        printf("Quoted Price of Paving: %f\n", totalCost);
    }
}

出于某种原因,当我编译时,我得到以下输出:

:~/> cc assignment1.c ^C
:~/>
:~/> a.out
asphalt company

Length of Driveway: 123
Width of Driveway: 123

Type of Paving:
 'A' for asphalt paving
 'C' for concrete paving
 'S' for paving stones

Select (A,C,S): A

:~/>

没有任何反应。在我有 scanf("%c%c", &typeOfPaving); 的第 22 行,我使用 %c%c 因为我的教授告诉我第一个 %c 存储输入数据后的换行符进入第 16 行的变量 d_Width:scanf("%f", &d_Width);。我现在看到的问题是(我认为?)char typeOfPaving 存储\nA?不只是 A 所以我的 if 语句不起作用?对吗?

谁能帮帮我,不知道该怎么办,哈哈,有点沮丧

最佳答案

"C" 是一个char 数组,'C' 是一个char。这就是您可能需要的:

if (typeOfPaving == 'C') 

关于c - 扫描字符然后在 IF 语句中使用它们? (如果 char= ='c' ){} 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8989775/

相关文章:

c - 在C中,变量不保留我分配给它的值并返回到0

c - 如何使用Windows和C从URL获取数据?

c - 黑客 samba - 如何从 struct fd_handle 获取目录

c - 使用指针删除链表中的节点

c - 分配内存时 C 中的段错误

c - 如何使结构常量中的函数指针用于初始化常量数组?

c - 为什么用函数的返回值初始化全局变量在声明时失败,但在文件范围内工作正常?

c++ - CreateFile 打开在另一个终端 session 中创建的 MS-DOS 设备

c - Getc 过度读取一个\n 字符

c - 打印菜单功能选择后继续打印