Sunday, August 2, 2009

Sorry but another question. plzz help with my c++ program?

At the end i'm asking for decimal move i know i have to use 10 to power of the (variable d), times the product but dont know how. plzzzz help . thank you lots.





#include %26lt;conio.h%26gt;// needed for getch


#include %26lt;iostream.h%26gt;//needed for cout and cin messages


#include %26lt;iomanip.h%26gt;//





void startup();


void method();


int main()


{


startup();


method();


getch();


return 0;


}





void startup()


{


cout%26lt;%26lt;""%26lt;%26lt;'\n';


cout%26lt;%26lt;"jan 17,2008"%26lt;%26lt;'\n';


cout%26lt;%26lt;""%26lt;%26lt;'\n';


cout%26lt;%26lt;""%26lt;%26lt;'\n';


}


void method()


{


float a, b, c;


int d;


cout%26lt;%26lt; "Enter number less than 1000: ";


cin%26gt;%26gt;a;


cout%26lt;%26lt; "Enter another number less than 1000: ";


cin%26gt;%26gt;b;





c = a*b;





cout%26lt;%26lt; "The product is "%26lt;%26lt;c%26lt;%26lt;'\n';





cout%26lt;%26lt; "How many decimal space(s) do you want to move right: ";


cin%26gt;%26gt;d;











cout%26lt;%26lt;"The new number is: "%26lt;%26lt;z%26lt;%26lt;'\n';








}

Sorry but another question. plzz help with my c++ program?
your last line, the new number is %26lt;%26lt; z %26lt;%26lt; ?


where did z come from?





All you have to do is take your product "c" and multiply it by 10 to the power of d, like you said.





As for seeing the output, you need to format your last line (i guess z) to show amount of places after the decimal place, because by default, if the numbers to the right of the decimal place is 0, it wont show up. You could do something like this





#include %26lt;iostream%26gt;





int main()


{


double x = 2.3767553235;





std::cout.precision(11);


std::cout %26lt;%26lt; x %26lt;%26lt; '\n';


}#include %26lt;iostream%26gt;





int main()


{


double x = 2.3767553235;





std::cout.precision(11);


std::cout %26lt;%26lt; x %26lt;%26lt; '\n';


}





or use the length command like soemthihg here





#include %26lt;iostream%26gt;


#include %26lt;sstream%26gt;





namespace jrd {


int digits(double val)


{


std::ostringstream oss;





oss %26lt;%26lt; static_cast%26lt;int%26gt;(val);





return oss.str().length();


}


}





int main()


{


double x = 2.3767553235;





std::cout.precision(10 + jrd::digits(x));


std::cout %26lt;%26lt; x %26lt;%26lt; '\n';


}#include %26lt;iostream%26gt;


#include %26lt;sstream%26gt;





namespace jrd {


int digits(double val)


{


std::ostringstream oss;





oss %26lt;%26lt; static_cast%26lt;int%26gt;(val);





return oss.str().length();


}


}





int main()


{


double x = 2.3767553235;





std::cout.precision(10 + jrd::digits(x));


std::cout %26lt;%26lt; x %26lt;%26lt; '\n';


}





http://www.daniweb.com/forums/thread2459...





As a side note, your line that says


cout %26lt;%26lt; ""%26lt;%26lt; '\n';


just puts in a hard return, correct? cout %26lt;%26lt; "" doesn't do anything, why dont you just use


cout %26lt;%26lt; endl;





good luck.


No comments:

Post a Comment