3

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
gok
  • 31
  • 3
  • 1
    It works fine for me if I change `printf("\t");` to `printf("%lf\t", time);` – Thirupathi Thangavel Jul 30 '15 at 13:59
  • I am beginner actually , and I do not know how to write this code.Please explain me, there is something easy solution either. – gok Jul 30 '15 at 14:01
  • I do not get the output you've said. What was your input anyway? – Thirupathi Thangavel Jul 30 '15 at 14:03
  • t_thirupathi It does not work unfortunately. If I do this, Program get new line and write hours again and there is no the same line between charge and output of fonks – gok Jul 30 '15 at 14:04
  • Ok, Now I get your question. If you're concerned about the output on terminal, you can write the output to a variable/file and output it after all the processing. – Thirupathi Thangavel Jul 30 '15 at 14:10
  • Yes my input is on screen . Program invoke fonk and the calculation is written on screen. – gok Jul 30 '15 at 14:14
  • 'time' is a system function. Therefore, a very bad idea to use it as a variable. Suggest using a different name for the variable – user3629249 Jul 31 '15 at 04:32
  • to fix the problem. do not output any of the data until after inputting all three time values. Suggest prompting the user for each time value An array of double array[3] would be sufficient for inputing the time values. – user3629249 Jul 31 '15 at 04:39

2 Answers2

3

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()

Community
  • 1
  • 1
valegians
  • 850
  • 1
  • 7
  • 17
  • Alternatively what you can do is take the inputs and then once you have all three generate the table you want – valegians Jul 30 '15 at 14:10
  • I am beginner and I do not know putchar etc. If there is no easy solution and I continue my way. Maybe future get me new solutions :D – gok Jul 30 '15 at 14:18
  • then what I would recommend is simply doing scanf 3 times to get the 3 hours (for example time1, time2, time3) you want, then afterwards simply output everything the way you want with a printf – valegians Jul 30 '15 at 14:22
  • But I want to my input is on screen and not again being on screen .Program invoke fonks and calculations write under charges. – gok Jul 30 '15 at 14:43
  • The way you want it to do can't work unless you change the design of your code or modify the way input is sent to the program – valegians Jul 30 '15 at 14:47
2

Standard C input functions only starts processing the input when you press the "Enter" key. Meanwhile, every key you press adds a character to a keyboard buffer.

So basically when you use the scanf function, it does not read the buffer until the "Enter" key has been pressed.

There are workarounds to pass this, but not with the standard C library.

Cheers!

Edit: The code below does not follow the standard C libraries. However, it does what you are asking for :)

#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
ezFreak
  • 82
  • 2
  • 8
  • Your words means there is no solutions in C right? (My english is not good.I want to be sure) – gok Jul 30 '15 at 14:48
  • See my edit. Please accept my answer if this satisfies ur needs. – ezFreak Jul 30 '15 at 15:52
  • I saved your project but I do not know getch() and pointers now. After I learned ,I will be here to comment.Thanks. – gok Aug 29 '15 at 22:26