Thread: Question about exponents

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    Question about exponents

    Here's the code

    Code:
    #include <iostream>
    
    using namespace std;
    
    main(){
    int x;
    x = 5 * 45e-6;
    cout<< x;
    
    }

    I need to get an answer with an exponent in it like for example the answer in the program is 0.000225 but i need an answer like this 2.25e-4 how can i do that?... any idea guys.? tnx in advance...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Before anything else, main() returns an int, so declare it as returning an int. Then. x is an int, but you want it to be a double.

    I need to get an answer with an exponent in it like for example the answer in the program is 0.000225 but i need an answer like this 2.25e-4 how can i do that?
    Before printing x, set:
    Code:
    cout.setf(ios::scientific);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    or you could do

    Code:
    cout << scientific << x << endl;
    btw, using an int for these calculations is useless

  4. #4
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    weeeiiuuhh tnx for the help laserlight and KIBO i really appreciate it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM