Link to home
Start Free TrialLog in
Avatar of Thibo
Thibo

asked on

I would like the user to key in (stdin) specific details of a log and end with EOF

Here is an example how I would like the user to enter the input:
name\tstartkm\tendkm\tdate\tdestination
Tim   18500    20000  10/09/83  Paris
Bob   27000    28000  11/02/85  N.Y.

The input is terminated by an EOF or an error compilation. Maximum charachters per line is 80.
Here is what I did:

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


int main(void)
{

char * line;
int startkm, endkm;
 
line = fgets(line, 81, stdin);


while (fgets(line,sizeof line,stdin) != NULL){ /*read the input line*/
     {
         
     sscanf(line,"%*[^\t]%d%*[^\t]%d%*[^\t]%*[^\n]",&startkm,&endkm);
         
     sizeLine i = sizeof (80);
     for (i=0; line[i] != '\n' && i <= sizeof line; i++){ /*makes sure the line is < 80 characters*/

          if (i == sizeof line){ /*this will be true only when the line exceed 80 characters*/

                  fprintf(stderr, "The input line exceeds 80 characters.\n");
                              return EXIT_FAILURE;
         
                             
               line = fgets(line, 81, stdin);
               }              

               
}
</code>

What is wrong wz that?
TQ.
Avatar of Mayank S
Mayank S
Flag of India image

The first thing that I can see is that you haven't allocated any memory for * len....

len = ( char * ) malloc ( 81 * sizeof ( char ) ) ; ???? before starting with your fgets () statement.

I also guess that you don't need the first fgets () statement.... the first sentence read will go unattended because the while also starts with an fgets ().

Mayank.


Avatar of Thibo
Thibo

ASKER

do i have to creat a new pointer with the name of "len"?
No no.... I meant 'line', not 'len'. The same pointer 'line' which you have.

Mayank.
Avatar of Thibo

ASKER

do i have to creat a new pointer with the name of "len"?
Please don't click on 'Refresh'.... click on the 'Reload This Question' link on the left-hand side to reload this page..

Mayank.
Avatar of Thibo

ASKER

ok so that would be like this?
<code>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

char * line;
int startkm, endkm;
 
line = ( char * ) malloc ( 81 * sizeof ( char ) );

while (fgets(line,sizeof line,stdin) != NULL)
{
         
sscanf(line,"%*[^\t]%d%*[^\t]%d%*[^\t]%*[^\n]",&startkm,&endkm);
         
         
for (i=0; line[i] != '\n' && i <= sizeof line; i++)
{

if (i == sizeof line)
{
fprintf(stderr, "The input line exceeds 80 characters.\n");
return EXIT_FAILURE;
         
line = fgets(line, 81, stdin);
               }              
           
}
</code>
Avatar of Thibo

ASKER

ok so that would be like this?
<code>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

char * line;
int startkm, endkm;
 
line = ( char * ) malloc ( 81 * sizeof ( char ) );

while (fgets(line,sizeof line,stdin) != NULL)
{
         
sscanf(line,"%*[^\t]%d%*[^\t]%d%*[^\t]%*[^\n]",&startkm,&endkm);
         
         
for (i=0; line[i] != '\n' && i <= sizeof line; i++)
{

if (i == sizeof line)
{
fprintf(stderr, "The input line exceeds 80 characters.\n");
return EXIT_FAILURE;
         
line = fgets(line, 81, stdin);
               }              
           
}
</code>
Avatar of Thibo

ASKER

sorry for clicking on 'refresh'.
didnt know we had to reload the question...
>> for (i=0; line[i] != '\n' && i <= sizeof line; i++)
>> {

>> if (i == sizeof line)
>> {
>> fprintf(stderr, "The input line exceeds 80  characters.\n");
>> return EXIT_FAILURE;
         
>> line = fgets(line, 81, stdin);
>>               }              

sizeof line will always return 81 because we have allocated 81 characters for 'line'. Also, this 'line = fgets (....) ;' statement here will never be reached, and is not required either.

Make it simply:

for ( i = 0 ; line[i] != '\n' && line[i] != '\0' ; i ++ )
  if ( i == 80 )
  {
    fprintf ( stderr, "\n The input exceeds 80 characters. " ) ;
    return EXIT_FAILURE ;

  } // end if, for

Mayank.

Avatar of Thibo

ASKER

Thx Mayank
But how should I declare the 'i' identifier?

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


int main(void)
{

char * line;
int startkm, endkm;


line = ( char * ) malloc ( 81 * sizeof ( char ) );

while (fgets(line,sizeof line,stdin) != NULL) /*read the input line*/
{
         
sscanf(line,"%*[^\t]%d%*[^\t]%d%*[^\t]%*[^\n]",&startkm,&endkm);
         
         
for ( i = 0 ; line[i] != '\n' && line[i] != '\0' ; i ++ )
if ( i == 80 )
{
fprintf ( stderr, "\n The input exceeds 80 characters. " ) ;
return EXIT_FAILURE ;
} // end if, for          
}                      
}
</code>
int i ;
Avatar of Thibo

ASKER

as simple as that....sorry for that question
its compiling ok, but its not quiting when im putting more than 80 characters

and if i want the program to exit if the startkm is more than the endkm or if startkm in one record is always less than or equal to endkm of the previous record:


for ( i = 0 ; line[i] != '\n' && line[i] != '\0' ; i ++ )
if ( i == 80 )
{
fprintf ( stderr, "\n The input exceeds 80 characters. " ) ;
return EXIT_FAILURE ;
} // end if, for          
if(startkm > endkm || startkm <= endkm)
{
fprintf( stderr, "\n Invalid numbers...");
return EXIT_FAILURE;
}
}                      
}

TQ.
Avatar of Thibo

ASKER

For the max characters, i tried on Visual c++ n its working
but not on command prompt
Apologises. Its ok as long as it works
cheers
>> i want the program to exit if the startkm is more than the endkm

sscanf(line,"%*[^\t]%d%*[^\t]%d%*[^\t]%*[^\n]",&startkm,&endkm);

if ( startkm > endkm )
{
  printf ( "\n Invalid. " ) ;
  exit ( 1 ) ; // include <process.h>

} // end if

Mayank.
Avatar of Thibo

ASKER

Thanks mayankeagle
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thibo

ASKER

if i want to print the number of trips, total kilometre and average trip length as a short summary to 'stdout' when ill end the program can i do something similar to this?
<code>
#include <stdio.h>
#include "Car.c"



void countTotalAverage()
{
     int c, n1;
        while ((c == getchar()) != EOF)
        if(c == '\n')
     ++n1;
         
     int count = 0;
     double av = 0;
     int i;

     for(int i=0; i < endkm; i++)
     count++;
     
     av = n1 / count;    
     
     
     printf("Number of trips: %d\n", n1);
     printf("\nTotal kilometres: %d", count);
     printf("\nThe average trips length is %d", av);
}
</code>
TQ.
Avatar of Thibo

ASKER

Very helpfull
Thx for ure help
#include <stdio.h>
#include <stdlib.h>
#include <process.h>

int main ( void )
{
  char * line ;
  int startkm, endkm, total = 0 ;
  int num_of_trips = 0 ;
  float avg_trip_length ;

  line = ( char * ) malloc ( 81 * sizeof ( char ) ) ;

  while ( fgets ( line, sizeof line, stdin ) != NULL )
  {    
    no_of_trips ++ ;
    sscanf ( line, "%*[^\t]%d%*[^\t]%d%*[^\t]%*[^\n]", &startkm, &endkm ) ;        
    total += ( endkm - startkm ) ;

    if ( startkm > endkm )
    {
      printf ( "\n Invalid. " ) ;
      exit ( 1 ) ; // include <process.h>

    } // end if
       
    for ( i = 0 ; line[i] != '\n' && line[i] != '\0' ; i ++ )
      if ( i == 80 )
      {
        fprintf ( stderr, "\n The input exceeds 80 characters. " ) ;
        exit ( 2 ) ;

      } // end if, for          

  } // end while

  avg_trip_length = ( ( float ) total ) / num_of_trips ;
  printf ( "\n Total: %d \n No. of trips: %d \n Average: %f", total, no_of_trips, avg_trip_length ) ;
 
  return 0 ;
                     
} // end of main ()


Mayank.
Avatar of Thibo

ASKER

Thank you
I taught once i clicked on correct answer i need to add another question
Cheerz
:)