Search code examples
c++bgi

Warning passing `double' for converting 2 of `void moveto(int, int)'


I got lot of those wanring in this program: Cannot figure out why, please help. The program works, it shows the graphics i need but i got those warnings starting with lines where is moveto and lineto Here is the full program:

int main()
{

    int Um=220, f=5, R=5;
    double XL=10, XC=5, t, Im, Z, fi; // jaunie mainīgie
    int s=1000, n=0;  // jaunie mainīgie datu masīvam - s-max.izmērs, n-aktuāls datu skaits
    double ue[s],it[s],ur[s],ul[s],uc[s]; //datu masīvi

    Z=sqrt(R*R+pow(XL-XC,2));   //Z aprēķins
    Im = Um/Z;                  //Im aprēķins
    fi = asin((XL-XC)/Z);       //fi aprēķins

    //Ķēdes modelēšanas cikls
    for (t=0;t<=2;t+=0.002) //cikls mainot t no 0 lidz 2
    {
    ue[n] = Um*sin(2*M_PI*f*t);         //ue (avota spriegums) datu ieraksts masīvā
    it[n] = Im*sin(2*M_PI*f*t+fi);      //it (strāva) datu ieraksts masīvā
    ur[n] = Im*R*sin(2*M_PI*f*t+fi);    //ur (rez.spriegums) datu ieraksts masīvā
    ul[n] = Im*XL*sin(2*M_PI*f*t+fi+M_PI/2);    //ul (spol.spriegums) datu ieraksts masīvā
    uc[n] = Im*XC*sin(2*M_PI*f*t+fi-M_PI/2);    //uc (kond.spriegums) datu ieraksts masīvā  
    n++;        //mērījumu skaita palielināšana 
    }

    for (int i=0; i<5; i++) 
        cout << "i = " << i << " " << ue[i] << " " << it[i] << " " <<  ur[i] << " " << ul[i] << " " << uc[i] << endl;

    initwindow (1000,500,"Grafiks");    

    //Grafiku izvades cikls
    for (int x=0; x<n-1; x++)
    {
        //ue grafika izvades komandas
        setcolor(10);               //krāsa         
        moveto(x,50-ue[x]*0.1);     //kursora ievietošana
        lineto(x+1,50-ue[x+1]*0.1); //līnijas zīmēšana
        //ur grafika izvades komandas
        setcolor(11);
        moveto(x,150-ur[x]*0.1);
        lineto(x+1,150-ur[x+1]*0.1);
        //ul grafika izvades komandas
        setcolor(12);
        moveto(x,250-ul[x]*0.1);
        lineto(x+1,250-ul[x+1]*0.1);
        //uc grafika izvades komandas
        setcolor(13);
        moveto(x,350-uc[x]*0.1);
        lineto(x+1,350-uc[x+1]*0.1);        
        //it 4 grafiku izvades cikls
        for (int i=50; i<=350; i+=100)
        {
        //it grafika izvades komadas            
        setcolor(14);
        moveto(x,i-it[x]);
        lineto(x+1,i-it[x+1]);          
        }                               
    }   
    while (!kbhit());
    closegraph();
}

Solution

  • You are passing arguments of type double to a function that accepts arguments of type int. A simple example would be:

    #include <iostream>
    void moveto(int x, int y){
        std::cout << x << ' ' << y << std::endl;
    }
    int main(){
        double a = 123.4, b = 567.8;
        moveto(a, b);  // decimal parts are cut off, warning issued
    }
    

    To suppress the compiler warning you could cast the double to integer using the static_cast:

    moveto(static_cast<int>(a), static_cast<int>(b));
    

    Or explore the rounding functions such as std::round or modify the function so that it accepts arguments of type double:

    void moveto(double x, double y){
        std::cout << x << ' ' << y << std::endl;
    }
    

    Casting doubles to integer removes the decimal parts (no rounding occurs) so the 123.4 becomes 123 and 567.8 becomes 567.