435,444 Members | 3,110 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,444 IT Pros & Developers. It's quick & easy.

Formatting numbers with a given number of decimal places

 P: 24 Can somone please, please give me the solution for the following problem. I need to submit it on Monday. Write a global function called format, which formats numbers with a given number of decimal places. The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the second argument is missing, the formatting should assume two (2) decimal places. No user-interaction code is allowed in this function. Write the main function shown below as a test for your function. Expand|Select|Wrap|Line Numbers #include int main() {   double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;   cout << "'" << format( n1, 1 ) << "'" << endl;   cout << "'" << format( n2, 1 ) << "'" << endl;   cout << "'" << format( n3 )    << "'" << endl;   cout << "'" << format( n3, 3 ) << "'" << endl;   return 0; }   If the format function is written correctly, the above test should output '1.2' '1.3' '3456.68' '3456.680' Thanxxxx in advance. Feb 13 '07 #1
17 Replies

 Expert Mod 2.5K+ P: 4,677 Can somone please, please give me the solution for the following problem. I need to submit it on Monday. Write a global function called format, which formats numbers with a given number of decimal places. The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the second argument is missing, the formatting should assume two (2) decimal places. No user-interaction code is allowed in this function. Write the main function shown below as a test for your function. Expand|Select|Wrap|Line Numbers #include int main() {   double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;   cout << "'" << format( n1, 1 ) << "'" << endl;   cout << "'" << format( n2, 1 ) << "'" << endl;   cout << "'" << format( n3 )    << "'" << endl;   cout << "'" << format( n3, 3 ) << "'" << endl;   return 0; }   If the format function is written correctly, the above test should output '1.2' '1.3' '3456.68' '3456.680' Thanxxxx in advance. What did you get when you tried this? Can you post your code? Feb 13 '07 #2

 P: 24 I wrote the global function but I tried to compile it the program is giving errors and the full program code is as follows: Expand|Select|Wrap|Line Numbers #include  #include double format(double,int);    // function prototype int main() {       double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;       cout<<"'"<

 Expert Mod 2.5K+ P: 4,677 Are you writing this in C or C++? Feb 13 '07 #4

 P: 24 I am writing this program in C++. Feb 13 '07 #5

 Expert Mod 2.5K+ P: 4,677 Updated minor bits of the code for readability. Expand|Select|Wrap|Line Numbers #include  #include using namespace std; double format(double val, int n);       // function prototype int main() {         double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;         cout<<"'"<

 Expert Mod 2.5K+ P: 4,677 And what I meant to ask was: can you pass it 0, or does it have to conform strictly to that test file? Feb 13 '07 #7

 P: 24 Ok so what should I do now? Feb 13 '07 #8

 P: 24 Its still giving errors. Feb 13 '07 #9

 Expert Mod 2.5K+ P: 4,677 Its still giving errors. Can you explain to me what each line of your format function does? (To make sure the algorithm to set the decimal precision is correct). Feb 13 '07 #10

 Expert 5K+ P: 5,000 Expand|Select|Wrap|Line Numbers   #include  #include using namespace std; double format(double val, int n);       // function prototype int main() {     double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;     cout << "'" << format(n1,1) << "'" << endl;     cout << "'" << format(n2,1) << "'" << endl;     cout << "'" << format(n3,0) << "'" << endl;     cout << "'" << format(n3,3) << "'" << endl;     return 0; }   double format(double val,int n) {     double mult, ans, res;     mult = ::pow(val, n);     ans = (val*mult);     res = ans/mult;     return res; }   I fixed some stuff, and now it compiles. See if this works. Feb 13 '07 #11

 P: 24 Thank you very much its working. Feb 13 '07 #12

 Expert Mod 2.5K+ P: 4,677 Thank you very much its working. Thanks RedSon. Feb 13 '07 #13

 P: 24 Can you please tell me what you did in the format( ) part of the program. Feb 13 '07 #14

 Expert 5K+ P: 5,000 Can you please tell me what you did in the format( ) part of the program. I added the scope resolution operator before pow the "::" and also changed your method call to have two arguments. Feb 13 '07 #15

 Expert 2.5K+ P: 3,652 The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the second argument is missing, the formatting should assume two (2) decimal places. So far, people have changed your 3rd format( ) call to be format(n3, 0), but according to the assignment, the second argument is optional - thus, format(n3) should function. In order for this to work, in your function prototype, you must declare a default value for n, like this: Expand|Select|Wrap|Line Numbers double format(double val, int n = 2); If no value is given for n in the call, then then program will assume that a 2 was passed. If a value is provided for n, then the program ignores the n = 2 and takes the given n. Feb 13 '07 #16

 Expert Mod 2.5K+ P: 4,677 So far, people have changed your 3rd format( ) call to be format(n3, 0), but according to the assignment, the second argument is optional - thus, format(n3) should function. In order for this to work, in your function prototype, you must declare a default value for n, like this: Expand|Select|Wrap|Line Numbers double format(double val, int n = 2); If no value is given for n in the call, then then program will assume that a 2 was passed. If a value is provided for n, then the program ignores the n = 2 and takes the given n. Thank you Ganon. I knew there was a way to do it, and have been trying to figure it out! Feb 13 '07 #17

 Expert 100+ P: 463 It appears that there is some kind of conflict between iostream and format. I'm not sure what it is, but gnu doesn't like it, either. Try changing the name of format to something really different (like xxxformat) and see if that particular error goes away. Feb 13 '07 #18 