9 Replies - 304 Views - Last Post: 09 October 2012 - 10:50 PM Rate Topic: -----

#1 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 08:47 PM

Hi guys,

I tried to search online but most of my search will point me to the following result.
#include <iostream>
using namespace std;

int main () {
  double a,b,c;
  a = 3.1415926534;
  b = 2006.0;
  c = 1.0e-10;
  cout.precision(5);
  cout       <<         a << '\t' << b << '\t' << c << endl;
  cout <<   fixed    << a << '\t' << b << '\t' << c << endl;
  cout << scientific << a << '\t' << b << '\t' << c << endl;
  return 0;
}



However, I am given the following only and requested to produce the above outcome. (not to use the full std function)
#include <iostream>
using std::cout;
using std::endl;
int main()
{
 double x = 0.001234567;
 double y = 1.946e9;
}



expected output
Displayed in default format:
0.001234567     1.946e+009
Displayed in scientific format:
1.234567e-003   1.946000e+009
Displayed in fixed format:
0.001235        194000000.000000



How should I go about it?
I am thinking maybe operator(<<) overloading...but without the built-in function, how do I convert double to scientific and fixed format?

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying double in scientific format and fixed format. No built-in.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 09:52 PM

Please restate your problem, as presented it doesn't make much sense to me. What are you trying to achieve, and why can't you use the setprecision(), setw(), and the other functions available from iomanip?

For example:

Quote

However, I am given the following only and requested to produce the above outcome. (not to use the full std function)

What do you mean by the section in the parentheses?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 09:58 PM

if we use the following
using namespace std;


then we are able to do following
cout << scientific << a << '\t' << b << '\t' << c << endl;



if we are only given
using std::cout;
using std::endl;


the "scientific" keyword is not functioning.

This post has been edited by B_M: 09 October 2012 - 09:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 10:14 PM

If you are not using the using namespace std; clause you must properly scope your functions for example without any using statements your cout would look like:
std::cout << std::scientific << a << '\t' << b << '\t' << c << std::endl;


Jim
Was This Post Helpful? 0
  • +
  • -

#5 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 10:21 PM

View Postjimblumberg, on 09 October 2012 - 10:14 PM, said:

If you are not using the using namespace std; clause you must properly scope your functions for example without any using statements your cout would look like:
std::cout << std::scientific << a << '\t' << b << '\t' << c << std::endl;


Jim



Thanks Jim.
But is there other way to do it without using "std::scientific" or "std::fixed"??
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 10:29 PM

Why? But it's possible to use the using std::scientific; clause but the proper way would be to use the scope resolution operator:: as shown.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 10:32 PM

Let me restate my problem.
I am given the following.
#include <iostream>
using std::cout;
using std::endl;

int main()
{
 double x = 0.001234567;
 double y = 1.946e9;
}


Question:
Write the complete program using the above to get the following output.
Displayed in default format:
0.00123457  1.946e+009
Displayed in scientific format:
1.234567e-003 1.946000e+009
Displayed in fixed format:
0.001235  194000000.000000



That is all about the question.
And I think I am not allow to use or add in either std::scientific or std::fixed.
Was This Post Helpful? 0
  • +
  • -

#8 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 10:39 PM

View Postjimblumberg, on 09 October 2012 - 10:29 PM, said:

Why? But it's possible to use the using std::scientific; clause but the proper way would be to use the scope resolution operator:: as shown.

Jim



I know what you mean by that. But that is the question's requirement. Anyhow, I think I am just going to do the std::scientific way...because I can't find any other way to convert double to scientific or fixed...
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 10:40 PM

Without being able to use the manipulators trying to get your program to produce the desired output will be very difficult. You will probably need to convert the numbers to strings in order to manually format the numbers. And trying to figure out how to format a number in scientific notation is probably above your pay grade. I suggest you ask your instructor if using the iomanipulators is acceptable. I really think your instructor is trying to show you how to properly use the different namespace scoping methods but you need to ask.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Displaying double in scientific format and fixed format. No built-in.

Posted 09 October 2012 - 10:50 PM

View Postjimblumberg, on 09 October 2012 - 10:40 PM, said:

Without being able to use the manipulators trying to get your program to produce the desired output will be very difficult. You will probably need to convert the numbers to strings in order to manually format the numbers. And trying to figure out how to format a number in scientific notation is probably above your pay grade. I suggest you ask your instructor if using the iomanipulators is acceptable. I really think your instructor is trying to show you how to properly use the different namespace scoping methods but you need to ask.

Jim



Totally agree(way above my knowledge). Or I can also do the operator overloading (<<) and hard code the output so that when I call "cout<< something" then the output will display the x or y in scientific/fixed format.

Thanks alot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1