我无法理解为什么这不能正确显示扫描信息

时间:2012-02-28 01:36:16

标签: 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;
}

2 个答案:

答案 0 :(得分:2)

我相信你的问题在于这个功能:

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;
}

答案 1 :(得分:1)

由于result的参数为int*,您是否需要在printf中取消引用它们?

printf("\nTIME-IN\t\t %d:%d", *enterhour, *enterminute);