【问题标题】:Remove \n after scanf() which read integer在 scanf() 读取整数之后删除 \n
【发布时间】:2015-07-30 13:45:49
【问题描述】:

我很原始,我写了很多基本程序。我的新日程安排程序有问题。我想计算总公园食谱并在屏幕上写上车号和小时数。此序列必须是车时收费。 在我的程序中,只有 scanf() 向用户询问小时。用户写小时并输入,程序换行。我想这样输出

Car    Hours     Charge
1       5         3.00

但是程序输出是这样的

Car    Hours    Charge
1       5
       3.00

这是我的程序源代码:

#include<stdio.h>

double calculateCharges ( double time1 );

int main( void )
{ //open main

 double time;
int i;
double TotalCharges=0, TotalTime=0;

printf("Car\tHours\tCharge\t\n");

for(i=1;i<=3;i++)   //there is 3 cars checkin
{  //open for

    printf("%d\t",i);
    scanf("%lf", &time);
    printf("\t");

    TotalTime+=time;

    printf("%lf",calculateCharges(time) ); // fonks calculate

    TotalCharges+=calculateCharges(time);  // for total charge

    puts("");

    } // end for
}  // end main

double calculateCharges ( double time1 )
{  //open fonk

    double totalC=0;

if( time1<=3)       // untill 3 hours, for 2 dolars
{   //open if

    totalC+=2.00;

}   //end if

else if(time1>3)      // after 3 hours, each hours cost 0.5 dolars
{    //open else if

    totalC+=2+(time1-3)*0.5;

}    //end else if


return totalC;
} // end fonk

【问题讨论】:

  • 如果我将 printf("\t"); 更改为 printf("%lf\t", time); 对我来说效果很好
  • 其实我是初学者,不知道这段代码怎么写。请解释一下,也有一些简单的解决方案。
  • 我没有得到你所说的输出。你的意见是什么?
  • t_thirupathi 不幸的是它不起作用。如果我这样做,程序会得到新行并再次写入小时数,并且在充电和输出之间没有相同的线 fonks
  • 好的,现在我明白你的问题了。如果您担心终端上的输出,可以将输出写入变量/文件并在所有处理后输出。

标签: c


【解决方案1】:

据我所知,这是一个与终端相关的“问题”。当您在终端中输入内容时,您的输入不会发送到程序,直到您按下回车并回车才会添加一个新行。

你需要做的是改变终端行为,这样你输入的所有内容都会立即发送到程序中。

看看这个问题,上面的答案会告诉你如何做你想做的事:How to avoid press enter with any getchar()

【讨论】:

  • 或者你可以做的是接受输入,然后一旦你有了所有三个生成你想要的表
  • 我是初学者,我不知道 putchar 等。如果没有简单的解决方案,我会继续我的方式。也许未来会给我新的解决方案:D
  • 那么我建议简单地执行 scanf 3 次以获得所需的 3 小时(例如 time1、time2、time3),然后使用 printf 以您想要的方式输出所有内容跨度>
  • 但我希望我的输入出现在屏幕上,而不是再次出现在屏幕上。程序调用 fonks 和计算写入费用。
  • 除非您更改代码的设计或修改输入发送到程序的方式,否则您希望它执行的方式无法工作
【解决方案2】:

标准 C 输入函数仅在您按下“Enter”键时才开始处理输入。同时,您按下的每个键都会将一个字符添加到键盘缓冲区。

所以基本上当你使用 scanf 函数时,它不会读取缓冲区,直到按下“Enter”键。

有一些解决方法可以通过这个,但标准 C 库没有。

干杯!

编辑: 下面的代码不遵循标准 C 库。但是,它可以满足您的要求:)

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

double calculateCharges ( double time1 );

int main( void )
{ //open main

    double time;
    int i,j = 0;
    double TotalCharges=0, TotalTime=0;
    char chTime[10];


    printf("Car\tHours\tCharge\t\n");

    for(i=1;i<=3;i++)   //there is 3 cars checkin
    {  //open for

        printf("%d\t",i);

        // Input the time
        j = 0;
        memset(chTime, '\0', 10);

        while ( 1 )
            {
            chTime[j] = getch();

            // User pressed "Enter"?
            if ( chTime[j] == 0x0d )
            {
                chTime[j] = '\0';
                break;
            }

            printf("%d", atoi(&chTime[j]));
            j++;
        }

        // Convert to the correct type
        time = atoi(&chTime[0]);

        TotalTime+=time;

        printf("\t%lf",calculateCharges(time) ); // fonks calculate

        TotalCharges+=calculateCharges(time);  // for total charge

        puts("");

        } // end for
}  // end main

double calculateCharges ( double time1 )
    {  //open fonk

        double totalC=0;

    if( time1<=3)       // untill 3 hours, for 2 dolars
    {   //open if

        totalC+=2.00;

    }   //end if

    else if(time1>3)      // after 3 hours, each hours cost 0.5 dolars
    {    //open else if

        totalC+=2+(time1-3)*0.5;

    }    //end else if


    return totalC;
} // end fonk

【讨论】:

  • 你的话的意思是C没有解决方案吧? (我的英语不好。我想确定一下)
  • 查看我的编辑。如果这满足您的需求,请接受我的回答。
  • 我保存了您的项目,但我现在不知道 getch() 和指针。了解后,我会在这里评论。谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多