Saturday, May 22, 2010

Visual c++ help please!!! computer programming?

How do i change the printfs to using cout... ?????





#include %26lt;stdio.h%26gt;





//conversion factors


float y2m=1.09361329834


float m2y=0.9144





main ()


{


int i;


float yards, meters;





for (i=5; i%26lt;=100; i=i+5)


{


yards=getyards(i);


printf("%d meters = %3.2f yards\n",i,yards);


}


for (i=5; i%26lt;=100; i=i+5)


{


getmeters(i,%26amp;meters);


printf("%d yards = %3.2f meters\n",i,meters);


}


} //end main





float getyards(int x)


{


return (x*m2y)


}





void getmeters (int x, float %26amp;m)


{


*m=(x*y2m);


return()


}

Visual c++ help please!!! computer programming?
Use "cout.precision(4)" giving you 4 digits of precision (which includes before and after the decimal). If you want it to mean just after the decimal, then also use "cout %26lt;%26lt; std::fixed".





You can set the field width with "cout.width(6)". To summarize, to do the same thing as %3.2f:





cout.width(6);


cout.precision(4);


cout %26lt;%26lt; std::fixed;


cout %26lt;%26lt; x %26lt;%26lt; endl;





Note that to cancel "std::fixed" you need to do this:





cout.unsetf (std::ios_base::floatfield);
Reply:I haven't used cout in ages, but I believe you would just say





cout%26lt;%26lt;i


cout%26lt;%26lt;" meters ="


cout%26lt;%26lt;yards





Not sure about how to do the %3.2f formatting. Also, I believe you can string them together





cout%26lt;%26lt;i%26lt;%26lt;"meters ="%26lt;%26lt;yards%26lt;%26lt;endl





endl - new line
Reply:#include %26lt;stdio.h%26gt;





//conversion factors


float y2m=1.09361329834


float m2y=0.9144





main ()


{


int i;


float yards, meters;





for (i=5; i%26lt;=100; i=i+5)


{


yards=getyards(i);


cout %26lt;%26lt; i %26lt;%26lt; yards %26lt;%26lt; endl;


}


for (i=5; i%26lt;=100; i=i+5)


{


getmeters(i,%26amp;meters);


cout %26lt;%26lt; i %26lt;%26lt; meters %26lt;%26lt;endl;


}


} //end main





float getyards(int x)


{


return (x*m2y)


}





void getmeters (int x, float %26amp;m)


{


*m=(x*y2m);


return()


}


No comments:

Post a Comment