5 Replies - 1068 Views - Last Post: 13 February 2009 - 05:08 AM Rate Topic: -----

#1 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Probelm with input

Posted 12 February 2009 - 08:31 AM

Hi,
I am having problem with getting input for floating point under an structure array.The code is below:
#include<conio.h>
#include<stdio.h>

struct census
{
char city[20];
long int pop;
float lt;
};


int main()
{
struct census ct[5];
int i;

clrscr();

printf("Give details for 5 cities:");

for(i=0;i<5;i++)
	{
	printf("Cities(%d):\n",i+1);
	printf("Name:");
	scanf("%s",ct[i].city);
	printf("Population:");
	scanf("%ld",&ct[i].pop);
	printf("Literacy:");
	scanf("%f",&ct[i].lt);
	}

clrscr();
for(i=0;i<5;i++)
	{
	printf("Cities(%d):\n",i+1);
	printf("\nName:%s",ct[i].city);
	printf("\nPopulation:%ld",ct[i].pop);
	printf("\nLiteracy:%f",ct[i].lt);
	}

getch();
return 0;
}


Here i am having problem getting the value for lt.can anyone tell me waht is the probelm?

Is This A Good Question/Topic? 0
  • +

Replies To: Probelm with input

#2 Smurphy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Re: Probelm with input

Posted 12 February 2009 - 09:34 AM

Ok well I have found one mistake. There might be more I am still begining C. But in this line in the for loop
scanf("%s", ct[i].city); 
// you fell into the same trap I always do it should be 
scanf("%s",&ct[i].city); 
// you forgot one of those dum & I always do that hope this helps and if it did please press the THIS POST WAS HELPFUL BUTTON

This post has been edited by Smurphy: 12 February 2009 - 09:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Probelm with input

Posted 12 February 2009 - 10:06 AM

View PostSmurphy, on 12 Feb, 2009 - 08:34 AM, said:

Ok well I have found one mistake. There might be more I am still begining C. But in this line in the for loop
scanf("%s", ct[i].city); 
// you fell into the same trap I always do it should be 
scanf("%s",&ct[i].city); 
// you forgot one of those dum & I always do that hope this helps and if it did please press the THIS POST WAS HELPFUL BUTTON


Well,it still not working.My program show the following problem during input after ct[i].pop:

"floating point formats not linked.Abnormal program termination."
Was This Post Helpful? 0
  • +
  • -

#4 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Probelm with input

Posted 12 February 2009 - 10:14 PM

Any help??
Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Probelm with input

Posted 12 February 2009 - 11:32 PM

View Postaaa111, on 12 Feb, 2009 - 11:14 PM, said:

Any help??

Your program worked for me! No error messages. I ran it in Dev-C++ earlier, and the only problem I could find was that it always outputs the lt with 4 decimal places. I couldn't get it to stop doing that, so I did not reply earlier.
Was This Post Helpful? 0
  • +
  • -

#6 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Probelm with input

Posted 13 February 2009 - 05:08 AM

You might like to see these modifications ... (to extend the range of inputs)

Shalom,
David


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define size 3 /* size of sample set to a small number for testing ...*/

struct census
{
    char *city;
    unsigned long pop;
    float lt;
};

typedef char * CString;

CString getLine();


int main()
{
    struct census ct[size];
    int i;

    printf( "Maximum population for a city in this survey is %lu\n", ULONG_MAX );
    printf( "Give details for %d cities ...\n", size );

    i = 0;
    for( ;; )
    {
        ct[i].pop = 0; /* error flag values ... */
        ct[i].lt= - 1;

        printf( "\nCities(%d) ...\n", i+1 );
        printf( "Name          : " );
        ct[i].city = getLine();

        printf( "Population    : " );
        scanf( "%lu", &ct[i].pop );
        while( getchar() != '\n' ); /* flush  stdin ... */

        printf( "Literacy      : " );
        scanf( "%f", &ct[i].lt );
        while( getchar() != '\n' ); /* flush  stdin ... */

        if( ct[i].pop == 0  )
        {
            printf( "Numeric data entry error for population ... Redo.\n" );
            continue;
        }
        else if(ct[i].lt <= -1  ||  ct[i].lt > 1.0 )
        {
            printf( "Numeric data entry error for Literacy. \n" );
            printf( "Enter numbers in range 0.0 to 1.0 only ... \n" );
            continue;
        }
        
        if( ++i == size ) break;
    }
    
    for(i=0; i<size; i++)
    {
        printf( "\nCities(%d) ...", i+1 );
        printf( "\nName          : %s", ct[i].city );
        printf( "\nPopulation    : %lu", ct[i].pop );
        printf( "\nLiteracy      : %.3f", ct[i].lt );
    }

    printf( "\n\nPress 'Enter' to continue ..." );
    getchar();
    return 0;
}

CString getLine()
{
    int bufSize = 255; /* larger, if you think you'll have most lines larger */
    CString buffer = (CString) calloc( bufSize+1, sizeof(char) );
    int i = 0, c;
    
    /* gets WHOLE line ... excludes '\n' */
    while( (c = fgetc( stdin )) != EOF && c != '\n' ) 
    {
        if( i >= bufSize )
        {
            bufSize += 256;
            buffer = (CString) realloc( buffer, bufSize );
        }
        buffer[i++] = c;
    }
    buffer[i] = 0; /* confirm NULL termination */
    return realloc( buffer, i+1  );
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1