1

The code below is to display the value of a,b,c,d to four decimal places and the value of e to one decimal place. However, when I run the function, all five variables are in one decimal place. Can you tell me what's wrong with my code and how can I fix it?

#include <iomanip>
#include <algorithm>
#include <vector>

struct Record{
    int year;
    string name;
    string team;
    double completions, attempts, yards, touchdowns, interceptions, e;
};

void passerRate ()
{
    double a, b, c, d, e;

    //quarterbacks is a vector of struct 
    for (int i = 0; i < quarterbacks.size(); i++){
    Record player = quarterbacks[i];
    a = (player.completions / player.attempts - 0.3) * 5;
    b = (player.yards / player.attempts - 3) * 0.25;
    c = (player.touchdowns / player.attempts) * 20;
    d = 2.375 - (player.interceptions / player.attempts * 25);
    cout << fixed << setprecision(4); // I want to set the precision of a,b,c,d to four decimal places 

    e = (a + b + c + d) / 6 * 100;
    cout << fixed << setprecision(1); //I want to set the precision of e to one decimal place
    quarterbacks[i].e = e;  
}
Phyllis Qu
  • 27
  • 5
  • It is unclear what you are asking. – Sid S Feb 05 '18 at 05:31
  • How are you printing your variables? Show your code. – MFisherKDX Feb 05 '18 at 05:34
  • It is hard to understand what the question is. Is it that you want to print a,b,c,d upto 4 decimal places and e only till one ? Also, e == (a + b + c + d)/6 * 100, does not set the computed value to e but instead performs a check if e is equal to it. Is that what you want ? – Vishaal Shankar Feb 05 '18 at 05:40
  • Also, what is fixed here ? Please refer to [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) before asking a question. – Vishaal Shankar Feb 05 '18 at 05:42
  • 1
    You can't. Floating-point variables don't have decimal places. They have binary places, and the two are incommensurable. If you want decimal places you have to use a decimal radix. More usually in non-money calculations you will retain all precision until the final display. – user207421 Feb 05 '18 at 07:04

2 Answers2

1

You can set the precision for a particular usage of cout.

// displays a to 4 decimal places  
cout << setprecision(4) << a;

// displays e to 1 decimal place
cout << setprecision(1) << e;

Aside, e = = (a + b + c + d) / 6 * 100; does not make any sense. I assume you meant to use a single =.

GoodDeeds
  • 5,837
  • 4
  • 28
  • 51
  • Yes, you are right. I mean to use a single =. Don't I need to use fixed? And I actually don't want to print out a,b,c,d but just use them to derive the value of e at their four decimal places. After e is calculated, I want then to display e in its one decimal place. Can I do that? – Phyllis Qu Feb 05 '18 at 06:59
-1

The line cout << setprecision(4) << fixed; will only affect how decimal values are displayed to the standard output thereafter. Here's two solutions that should help you. If what you want is the numbers themselves to be rounded before they're used in other calculations, the following should work well for you:

double x = 1.23456;
double x1 = round(x); // x1 is x rounded to nearest whole number
double x2 = round(x * 10.0) / 10.0; // x2 is x rounded to nearest tenth
double x3 = round(x * 1000.0) / 1000.0; // x3 is x rounded nearest thousandth

Otherwise, you can set the standard output's precision between print statements, as follows:

cout << setprecision(4) << fixed; // cout will now print 4 decimal places
cout << "a = " << a << endl;
cout << setprecision(1); // cout will now print 1 decimal place
cout << "e = " << e << endl;
alter igel
  • 5,696
  • 3
  • 16
  • 38
  • Hmm, I don't want to cout the value of a,b,c,d,e. I actually wanted to leave a,b,c,d to four decimal places when calculating e. After the value of e is derived, I want to displace it to one decimal place and save it into a vector of struct (e is one element of the struct). Can I do that? – Phyllis Qu Feb 05 '18 at 06:56
  • This solution is much repeated over the years, but it doesn't actually work. See [here](https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java/12684082#12684082) for proof. – user207421 Feb 05 '18 at 07:04
  • @EJP : you're quoting a Java answer. – MSalters Feb 05 '18 at 08:21