Problem using floating number in a structure

Write here if you have problems with your C source code

Moderator: Board moderators

Problem using floating number in a structure

Postby Syed Mubashshir Husain » Tue Apr 08, 2003 9:17 am

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[i].a);
return 0;
}

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

Postby turuthok » Tue Apr 08, 2003 11:37 am

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).
turuthok
Experienced poster
 
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia

Postby bery olivier » Tue Apr 08, 2003 1:42 pm

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[i].a);
for (i = 0; i < MAX; ++i) printf ("%lf\n", x[i].a);
return 0;
}
[/c]
Not AC yet Image AC at last Image
bery olivier
Learning poster
 
Posts: 90
Joined: Sat Feb 15, 2003 1:39 am
Location: Paris, France

Postby Syed Mubashshir Husain » Wed Apr 09, 2003 9:24 am

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[i].x);
}
return 0;
}
Syed Mubashshir Husain
New poster
 
Posts: 9
Joined: Wed Apr 02, 2003 10:28 am


Return to C

Who is online

Users browsing this forum: No registered users and 0 guests