c - 我不明白为什么这没有正确显示扫描信息

标签 c pointers

我认为问题肯定出在地址或指针上,但我找不到它。有人可以描述这个问题,或者如果感觉慷慨修复代码吗?

我的问题是,出现提示时它不会打印我输入的内容。

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

void getData(int* enterhour, int* enterminute, int* exitotalhour, int* exminute);
void rate(int exitotalhour, int exminute, int enterhour, int enterminute, int* totalhour, int* totalminute, int* round);
void charge(char* vehic, float* rate1, float* rate2, int enterhour);
void result(int* exitotalhour, int* exminute, int* enterhour, int* enterminute, int* totalhour, float* rate1, float* rate2, int* round, float* total);

int main(void)
{
    char vehic;
    int enterhour;
    int enterminute;
    int exitotalhour;
    int exminute;
    int totalhour;
    int totalminute;
    int round;

    float rate1;
    float rate2;
    float total;

    getData(&enterhour, &enterminute, &exitotalhour, &exminute);
    rate(exitotalhour, exminute, enterhour, enterminute, &totalhour, &totalminute, &round);
    charge(&vehic, &rate1, &rate2, enterhour);
    total= rate1 + rate2;
    result( &exitotalhour, &exminute, &enterhour, &enterminute, &totalhour, &rate1, &rate2, &round, &total);

    return 0;
}

void getData(int* enterhour, int* enterminute, int* exitotalhour, int* exminute)
{
    char v;

    printf("Enter c for car, b for bus, t for truck: ");
    scanf("%c", &v);
    printf("\nHour vehicle entered 0-24: ");
    scanf("%d", &enterhour);
    printf("\nMinute vehicle entered 0-60: ");
    scanf("%d", &enterminute);
    printf("\nHour vehicle exited 0-24: ");
    scanf("%d", &exitotalhour);
    printf("\nMinute vehicle exited 0-60: ");
    scanf("%d", &exminute);
    return;
}

void rate(int exitotalhour, int exminute, int enterhour, int enterminute, int* totalhour, int* totalminute, int* round)
{
    if(enterminute < exminute)
    {
        enterminute= enterminute + 60;
        exitotalhour= exitotalhour - 1;
    }

    *totalhour = enterhour - exitotalhour;
    *totalminute = enterminute - exminute;

    if ((*totalminute > 0 && *totalminute <= 60))
    {
        *totalhour = *totalhour + 1;
        *round = *totalminute * 0;
    }
    return;
}

void charge(char* vehic, float* rate1, float* rate2, int enterhour)
{
    switch (*vehic)
    {
        case 'c': if (enterhour <= 3)
        {
            *rate1 = 0.00;
            if (enterhour > 3)
            *rate2 = 1.50 * (float)(enterhour - 3);
        }
        break;

        case 't': if (enterhour <= 2)
        {
            *rate1 = 1.00 * (float)enterhour;
            if (enterhour > 2)
            *rate2 = 2.30 * (float)(enterhour - 2);
        }
        break;

        case 'b': if (enterhour <= 1)
        {
            *rate1 = 2.00 * (float)enterhour;
            if (enterhour > 1)
            *rate2 = 3.70 * (float)(enterhour - 1);
        }
        break;
    }
    return;
}

void result(int* exitotalhour, int* exminute, int* enterhour, int* enterminute, int* totalhour, float* rate1, float* rate2, int* round, float* total)
{
    printf("\n\t\t LOT CHARGE \t\t\n");
    printf("\nType of vehicle: Car or Bus or Truck");
    printf("\nTIME-IN\t\t %d:%d", enterhour, enterminute);
    printf("\nTIME-OUT\t\t %d:%d", exitotalhour, exminute);
    printf("\n\t\t\t --------");
    printf("\nPARKING TIME\t\t %d:%d", totalhour, round);
    printf("\n\t\t\t --------");
    *total= *rate1 + *rate2;
    printf("\nTOTAL CHARGE\t\t %4.2f\n\n", total);

    return;
}

最佳答案

我认为您的问题出在这个函数中:

void getData(int* enterhour, int* enterminute, int* exitotalhour, int* exminute)
{
   char v;

   printf("Enter c for car, b for bus, t for truck: ");
   scanf("%c", &v);
   printf("\nHour vehicle entered 0-24: ");
   scanf("%d", &enterhour);
   printf("\nMinute vehicle entered 0-60: ");
   scanf("%d", &enterminute);
   printf("\nHour vehicle exited 0-24: ");
   scanf("%d", &exitotalhour);
   printf("\nMinute vehicle exited 0-60: ");
   scanf("%d", &exminute);
   return;
}

您将 int 指针传递给函数,然后将这些指针的地址提供给 scanf。您应该只传递指针本身:

void getData(int* enterhour, int* enterminute, int* exitotalhour, int* exminute)
{
   char v;

   printf("Enter c for car, b for bus, t for truck: ");
   scanf("%c", &v);
   printf("\nHour vehicle entered 0-24: ");
   scanf("%d", enterhour);
   printf("\nMinute vehicle entered 0-60: ");
   scanf("%d", enterminute);
   printf("\nHour vehicle exited 0-24: ");
   scanf("%d", exitotalhour);
   printf("\nMinute vehicle exited 0-60: ");
   scanf("%d", exminute);
   return;
}

关于c - 我不明白为什么这没有正确显示扫描信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9475148/

相关文章:

c - 使用 popen(3) 与系统交互的 C 程序与 bash shell 相比如何?

c - 如何将十六进制字节 {0x00, 0x03, 0x9e, 0x40} 转换为 int 以在 c 中进行计算?

c - 使用 IMAP C-Client 获取正文部分 header

c++ - const int *p 与 int const *p - 类型后的 const 是否可以接受?

c - 将指针传递给函数并在外部访问它

c - 提取特定范围的位并在 C 中找到它们之间的零数

c - 在调用 pthread_join 之前到达 pthread_exit()

c - C 中的游程长度编码,strcat 的问题

c++ - 如何在 C++ 中跟踪无效指针?

c++ - C 字符串和字符数组声明