0

I'm using MS Visual C++ 6.0 (as my work requires) on a pretty decent Windows 10 PC.

I'm having trouble with displaying the value after the decimal that is entered manually via "cin >>", I've tried the fix that was answered thru DSKVP's question (Show two digits after decimal point in c++) but it still won't show the decimal values, it just drops the digits after the decimal and/or rounds them off.

#include <iostream>
#include <iomanip>    //for setprecision and fixed
using namespace std;  

int readResults()
{
    cout << setprecision(2);
    cout << "Scanned Value: ";
    double vrrm(0.0);
    cin >> vrrm;
    return vrrm;
}

int main()
{
    double vrrm ( readResults() );
    double vrwm ( readResults() );
    double vr   ( readResults() );
    double vrms ( readResults() );
    double io   ( readResults() );
    double vfm  ( readResults() );

    cout << "\nParameters \tScanned Values  \t Limits \t\tUnit" << endl;
    cout << "Vrmm \t \t" << fixed << setprecision (2) << vrrm << " \t\t\t 45.00 - 50.00 \t\tV" << endl;
    cout << "Vrwm \t \t" << fixed << setprecision (2) << vrwm << " \t\t\t 45.00 - 50.00 \t\tV" << endl;
    cout << "Vr \t \t"   << fixed << setprecision (2) << vr   << " \t\t\t 45.00 - 50.00 \t\tV" << endl;
    cout << "Vrms \t \t" << fixed << setprecision (2) << vrms << " \t\t\t 33.00 - 37.00 \t\tV" << endl;             
    cout << "Io \t \t"   << fixed << setprecision (2) << io   << " \t\t\t 00.90 - 1.50  \t\tA" << endl;
    cout << "Vfm \t \t"  << fixed << setprecision (2) << vfm  << " \t\t\t 00.95 - 1.05  \t\tV" << endl;
    cout << endl;
    
    if ( vrrm >= 45.00  &&  vrrm <= 50.00  &&
         vrwm >= 45.00  &&  vrwm <= 50.00  &&
         vr >= 45.00    &&  vr <= 50.00    &&
         vrms >= 33.00  &&  vrms <= 37.00  && 
         io >= 0.90     &&  io <= 1.50     &&
         vfm >= 0.95    &&  vfm <= 1.05       )
        
        cout << "PASS" << endl << endl;
        
    else
        cout << "FAIL" << endl << endl;
                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    return 0;
}

When I input decimal values, this is the result:

Scanned Value: 49.98
Scanned Value: 49.99
Scanned Value: 49.85
Scanned Value: 35.01
Scanned Value: 0.99
Scanned Value: 1.01

Parameters      Scanned Values           Limits                 Unit
Vrmm            49.00                    45.00 - 50.00          V
Vrwm            49.00                    45.00 - 50.00          V
Vr              49.00                    45.00 - 50.00          V
Vrms            35.00                    33.00 - 37.00          V
Io              0.00                     00.90 - 1.50           A
Vfm             1.00                     00.95 - 1.05           V

FAIL

Press any key to continue

I tried initializing Vrrm to 0.0 (as seen here) and even tried to multiply it by 1.00 as seen on Can't get cout to display decimals c++ but I believe it does not apply to what I am writing here - still wouldn't display the values after the decimal point.

I very much want to have the complete value displayed and the program to properly output PASS (as it should be). Do anyone of you mind to kindly explain it to me where I went wrong/refer me to some good reference/source materials?

I just started learning C++ this week and Alex's tutorial is doing an awesome job educating me and his work is my reference for now: https://www.learncpp.com/.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    `setprecision` sets the number of significant digits, not the number of decimals. Also, look very closely at `int readResults()` and consider the significance of the return type. – molbdnilo Mar 01 '21 at 08:34
  • [Read the docs at cppref](https://en.cppreference.com/w/cpp/io/manip/setprecision). I'm trying to find a dupe, which must be here somewhere. I found a heavily upvoted answer, but it was code only=bad. – JHBonarius Mar 01 '21 at 08:35
  • Hi molbdnilo - I see! Thank you for the input! Hi JHBonarius - link bookmarked - Thanks! :D – Newbie_02_2021 Mar 01 '21 at 08:50

1 Answers1

0

I tested your code, and the only thing I changed was the return type of readResults() from int to double. Once I did that, I got the results I expected!

Wes
  • 104
  • 5