• C

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.
ThiboAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
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.


0
ThiboAuthor Commented:
do i have to creat a new pointer with the name of "len"?
0
Mayank SAssociate Director - Product EngineeringCommented:
No no.... I meant 'line', not 'len'. The same pointer 'line' which you have.

Mayank.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ThiboAuthor Commented:
do i have to creat a new pointer with the name of "len"?
0
Mayank SAssociate Director - Product EngineeringCommented:
Please don't click on 'Refresh'.... click on the 'Reload This Question' link on the left-hand side to reload this page..

Mayank.
0
ThiboAuthor Commented:
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>
0
ThiboAuthor Commented:
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>
0
ThiboAuthor Commented:
sorry for clicking on 'refresh'.
didnt know we had to reload the question...
0
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.

0
ThiboAuthor Commented:
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>
0
Mayank SAssociate Director - Product EngineeringCommented:
int i ;
0
ThiboAuthor Commented:
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.
0
ThiboAuthor Commented:
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
0
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
ThiboAuthor Commented:
Thanks mayankeagle
0
Mayank SAssociate Director - Product EngineeringCommented:
Please accept/ rate the answer so that it can be PAQed.

Thanks,

Mayank.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ThiboAuthor Commented:
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.
0
ThiboAuthor Commented:
Very helpfull
Thx for ure help
0
Mayank SAssociate Director - Product EngineeringCommented:
#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.
0
ThiboAuthor Commented:
Thank you
I taught once i clicked on correct answer i need to add another question
Cheerz
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.