Remove the \ n after scanf () that read an integer
I'm fed up and I've written a lot of basic programs. There is something wrong with my new schedule program. I want to talk about all the recipes of the park and write on the screen its car number and clock. This sequence should be the responsibility of the car clock. My program only has scanf () setting the clock to the user. The user writes the hour and enters, the program gets a new line. I want it to be like
Car Hours Charge
1 5 3.00
But the program output is like
Car Hours Charge
1 5
3.00
This is the source code of my program:
#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
source to share
As far as I know, this is a "problem" related to the terminal. When you type something in the terminal, your input is not sent to the program until you hit enter and type a new line.
What you need to do is change the behavior of the terminal so that whatever you enter 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 typing press with any getchar ()
source to share
Standard C input functions only start processing input when you press the Enter key. Meanwhile, every key you press adds a character to the keyboard buffer.
So basically, when you use the scanf function, it doesn't read the buffer until the Enter key is pressed.
There are workarounds to pass this, but not with the C standard library.
Hooray!
Edit: The code below is not in line with the C standard libraries. However, it does what you ask :)
#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
source to share