Problem using floating number in a structure

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
Syed Mubashshir Husain
New poster
Posts: 9
Joined: Wed Apr 02, 2003 10:28 am

Problem using floating number in a structure

Post by Syed Mubashshir Husain »

I face a problem in using floating number.

#include <stdio.h>

struct tag
{
double a;
}x[10];

int main ()
{
int i;
for (i = 0; i < 2; ++i) scanf ("%f", &x.a);
return 0;
}

---
SMH :oops:

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

You might want to scanf() it to a "float" type first ... and then you can assign it to your "double" type ... That's what I always do ...

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).

bery olivier
Learning poster
Posts: 90
Joined: Sat Feb 15, 2003 1:39 am
Location: Paris, France
Contact:

Post by bery olivier »

Just use %lf, this works with gcc
[c]#include <stdio.h>
#define MAX 10
struct tag
{
double a;
}x[MAX];

int main ()
{
int i;
for (i = 0; i < MAX; ++i) scanf ("%lf", &x.a);
for (i = 0; i < MAX; ++i) printf ("%lf\n", x.a);
return 0;
}
[/c]
Not AC yet Image AC at last Image

Syed Mubashshir Husain
New poster
Posts: 9
Joined: Wed Apr 02, 2003 10:28 am

Post by Syed Mubashshir Husain »

I got problem from following code.

The correct code is :

#include<stdio.h>

struct tag
{
double x;
} ar[12];

int main()
{
long i;
for(i = 0; i < 2; i++)
{
scanf("%lf",&ar.x);
}
return 0;
}

Post Reply

Return to “C”