Converting float value to int

Oct 8, 2013 at 6:37am
Hi All,

I would like to convert a a value of the variable from float to int.

I'm getting error as "converting to `int' from `float'". and it shows me the wrong answer for n.

please help me with this. thanks in advance.

1
2
3
  float h,dx;
  int n;
  n=(h/dx)+1;
Oct 8, 2013 at 6:46am
I assume, that it is shortened code and you do initialize h and dx before use.

Is it an error or warning? Because it is completely valid operation in C++ but compiler can issue warning because there might be loss of data.

Can you please provide values for h, dx and wrong n? I need them to understand what exactly your problem is. I see at least 3 mutually exclusyve problems which could arise.
Oct 8, 2013 at 6:52am
h=0.04;
dx=0.001;

n=1 (output)

where, I shout get n=41

I'm getting a warning message.
Last edited on Oct 8, 2013 at 6:52am
Oct 8, 2013 at 7:05am
That is strange. Your snippet should work: http://ideone.com/VGU2YS (40 in answer is because of imprecision of float and default mode of rounding down)

Mind posting your full code? Might be something there.
Oct 8, 2013 at 7:13am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream.h>
#include <conio.h>
#include <algorithm>

void print();

class ftcs
{
      private:
              float a,b,c,d,e,f,u[100];
      
      public:
             int read_data (float a,float b, float c, float d, float e, float f)
                 {
                        print();
                        cout<<"\nThis program computes the velcoity of suddenly accelerated plate \nover a period of time \n";
                        print();
                        cout<<"\nPlease Enter the following details in SI units.....\n\n";
                        cout<<"Distance between the two plates (h)  m   :";cin>>a;
                        cout<<"Velocity of the moving plate    (u0) m/s :";cin>>b;
                        cout<<"Kinematic viscosity of fluid    (v)  m2/s:";cin>>c;
                        print();
                        cout<<"\nSetting the Grid for the problem...\n\nGrid Spacing required along\n1.Y-Direction\t\t:";cin>>d;
                        cout<<"2.Time step Interval\t:";cin>>e;
                        cout<<"Time step at which velcoity to be found:";cin>>f;
                        print();cout<<"\n";
                        
                 }
                 
             int array(float *u, int n)
             {
             for(int i=0;i<=n-1;i++)
                {
                        if(i==n-1) 
                        {u[i]=10;}else
                        {u[i]=0;}
                        
                }
             }            
              
};

int main()
{
    float h,u0,v,dx,dt,t,d,u[100];
    int n,cc;
    ftcs x;
    x.read_data(h,u0,v,dx,dt,t);
    
    d=v*(dt/(dx*dx));
    n=(h/dx)+1;
    cc=n+1;
    
    x.array( &u[100] ,n);

    getch();
}

void print()
{
     cout<<"-----------------------------------------------------------------";
}  
  
Oct 8, 2013 at 7:26am
your h and dx values are not initialised. So you are getting nonsential output from them. I suppose, you are using VS which zeroes memory in debug mode. That why you getting 1 and not 65868132146784 for example.
Oct 8, 2013 at 7:32am
Even after initializing h and dx to ZERO. Answer is getting wrong

Please help me to work out the problem.
Last edited on Oct 8, 2013 at 7:33am
Oct 8, 2013 at 7:41am
The problem not that you are not initialising them, but that you do not assign them any value you want to work with anywhere.
Oct 8, 2013 at 8:11am
Do you mean to declare h and dx=0 before line 48.

Please help me
Oct 8, 2013 at 8:30am
Even if you define them as 0 and 0, your answer would be:
1
2
n=(h/dx)+1; //= (?) + 1
  0↑  ↑0
You are not setting it to values you actually need anywhere.
Oct 8, 2013 at 8:49am
Lets cut to the essential:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void foo ( int a )
{
  a = 42;
  // 'a' is a local copy that is initialized by the caller
  // 'a' vanishes right here
}

void bar ( int & b )
{
  b = 3;
  // 'b' is a reference to a variable of the caller
}

int main()
{
  int b = 7;
  foo( b );
  // b is still 7

  bar( b );
  // b is now 3

  return 0;
}

Your ftcs::read_data() is like foo(). That is why the "cin >> a;" does not change the "h". Besides, the input can fail, or the user might write 0. You must cover those cases too.

Oh, ftcs has member variable 'a'. tcs::read_data() has parameter 'a'. One shadows the other within tcs::read_data(). Do you know which 'a' is in the "cin >> a"? Neither helps your 'h'.
Oct 8, 2013 at 9:40am
Thank you for your help .

It worked now.
Topic archived. No new replies allowed.