Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove \n after scanf() which read integer

Tags:

c

I am raw and I wrote a lot of basic programs. There is something wrong in my new schedule program. I want to caltculate total park recipes and write on screen it with car number and hours. This sequence must be Car Hours Charge. In my program, there is only scanf() asking hours to user.User wrote hour and enter, the program get new line.I want to out put like this

Car    Hours     Charge
1       5         3.00

But program output is like

Car    Hours    Charge
1       5
       3.00

This my program source code:

#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
like image 279
gok Avatar asked Nov 25 '25 17:11

gok


1 Answers

As far as I'm aware this is a "problem" linked to the terminal. When you type something in the terminal your input isn't sent to the program until you press enter and enter will add a new line.

What you have to do is change the terminal behavior, so that everything you type is immediately sent to the program.

Have a look at this question, the top answer will show you how to do what you want to do: How to avoid press enter with any getchar()

like image 166
valegians Avatar answered Nov 28 '25 16:11

valegians



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!