我无法弄清楚为什么这不能正确显示扫描的信息

我无法弄清楚为什么这不能正确显示扫描的信息

我想问题当然出在地址或指针上,但我找不到。有人能描述一下这个问题吗,或者如果你觉得慷慨的话,可以修改代码吗?
我的问题是,当提示时,它不会打印我输入的内容。

#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/

10-12 05:55