?
Solved

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

Posted on 2003-03-26
20
Medium Priority
?
248 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:Thibo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 8
20 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8215809
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
 

Author Comment

by:Thibo
ID: 8216070
do i have to creat a new pointer with the name of "len"?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8216102
No no.... I meant 'line', not 'len'. The same pointer 'line' which you have.

Mayank.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Thibo
ID: 8216138
do i have to creat a new pointer with the name of "len"?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8216157
Please don't click on 'Refresh'.... click on the 'Reload This Question' link on the left-hand side to reload this page..

Mayank.
0
 

Author Comment

by:Thibo
ID: 8216170
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
 

Author Comment

by:Thibo
ID: 8216180
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
 

Author Comment

by:Thibo
ID: 8216204
sorry for clicking on 'refresh'.
didnt know we had to reload the question...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8216216
>> 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
 

Author Comment

by:Thibo
ID: 8216262
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8216314
int i ;
0
 

Author Comment

by:Thibo
ID: 8216412
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
 

Author Comment

by:Thibo
ID: 8216434
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8216439
>> 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
 

Author Comment

by:Thibo
ID: 8216447
Thanks mayankeagle
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 200 total points
ID: 8216515
Please accept/ rate the answer so that it can be PAQed.

Thanks,

Mayank.
0
 

Author Comment

by:Thibo
ID: 8216535
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
 

Author Comment

by:Thibo
ID: 8216537
Very helpfull
Thx for ure help
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8216606
#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
 

Author Comment

by:Thibo
ID: 8216669
Thank you
I taught once i clicked on correct answer i need to add another question
Cheerz
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question