Long Numbers

This is a discussion on Long Numbers within the C++ Programming forums, part of the General Programming Boards category; i am trying to print in standard notation but it keeps printing in scientific notation. here is the coding. Code: ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    Long Numbers

    i am trying to print in standard notation but it keeps printing in scientific notation. here is the coding.

    Code:
    #include <iostream>
    using namespace std;
    
    int main() 
    {
    	long double no1, no2, ans; 
        char ch1;
    
    	cout << "Enter an equation:";
    
    	cin >> no1 >> ch1 >> no2;
    
        if (ch1 == '+')
        {
    		ans = no1 + no2;
        }
        if (ch1 == '-')
        {
            ans = no1 - no2;
        }
        
        cout<<ans<<endl;
        
    	return 0;
    }
    i am trying to get
    123456789012345678901234567890 + 12345678901234567890
    as the input to output
    246913578024691357802469135780

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,438
    Try using manipulators to change how floating point values are output. The catch is you need to use the manipulator every time you output a value (as the stream typically gets reset to defaults once the value is output)

    Code:
       cout << fixed << setw(20) << ans << endl;
    std::fixed is a manipulator that outputs floating point in fixed notation. std::setw() sets the width of the output for the next value.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    all this is doing is puttin a space of 50 chars in front of the scientific notation answer, it doesnt change it to standard notation. please help this is killing me.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main() 
    {
    	long double no1, no2, ans; 
        char ch1;
    
    	cout << "Enter an equation:";
    
    	cin >> no1 >> ch1 >> no2;
    
        if (ch1 == '+')
        {
    		ans = no1 + no2;
        }
        if (ch1 == '-')
        {
            ans = no1 - no2;
        }
        
        cout <<  setw(50) << ans <<endl;
        
    	return 0;
    }
    Last edited by themexican; 11-15-2005 at 12:21 AM.

  4. #4
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49

    Smile

    As far as I know, you can use cout.precision(int n) to change the number of digits it displays, but careful about the maximum precision of the data type itself.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    k i got that but now if my answer doesnt fill the (int n) it puts zeros at the end of the answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  2. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  5. Background-process app
    By philip in forum C++ Programming
    Replies: 14
    Last Post: 03-26-2002, 11:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21