# Setting Precision Confusion

• 01-13-2009
dnguyen1022
Setting Precision Confusion
Hello all, so I am doing an exercise from a book, and I am suppose to take multiple students name, and their final grades when computed into two separate vectors. I am unsure if my program even works at all because of the usage of precision. I think I want to round the grades up to about 3 decimal places. However the book I am using briefly describes how to use precision. Can someone please point me in the right direction on using the precision method?

Code:

```#include <iomanip> #include <ios> #include <iostream> #include <string> #include <vector> using std::cin; using std::setprecision; using std::cout; using std::string; using std::endl; using std::streamsize; using std::vector; int main(){         vector<string> students;     vector<int> grades;     string state;         for(int i = 0; state != "DONE"; i++){                 // ask for and read the student's name     cout << "Please enter student's first name: ";     string name;     cin >> name;     students.push_back(name);     // ask for and read the midterm and final grades     cout << "Please enter midterm and final exam grades: ";     double midterm, final;     cin >> midterm >> final;         // ask for the homework grades     cout << "Enter all your homework grades, "                   "followed by end-of-file if done,";     // the number and sum of grades read so far     int count = 0;     double sum = 0;     // a variable into which to read     double x;     // invariant:     // we have read count grades so far, and     // sum is the sum of the first count grades     while (cin >> x) {           ++count;           sum += x;           }         //PROBLEM HERE     //adds final grade to the vector     int grade = setprecision(3) <<  0.2 * midterm + 0.4 * final + 0.4 * sum / count     grades.push_back(grade);         cout << "DONE?" ;     cin >> state; }         // write the result         cout << "Your final grades are: " << endl;     for(int i = 0; i != students.size(); i++){             cout << students[i]                 << grades[i] << endl;                 }         return 0;     }```
• 01-13-2009
Dino
You've defined grade as an integer. No such thing as precision with an integer. Try another datatype, like float or double.
• 01-13-2009
anon
Also precision only affects how the floating point value is output (show me that many digits), not the calculations.
• 01-13-2009
dnguyen1022
Ok I think I fixed that problem. Thank you for your help. However my next problem is actually solving this exercise. I am unsure of how I would go about asking for the next student's name and grades. I am trying to get it to loop until I type DONE so it will output the names of the students with their grades. As of this moment it simply runs on and on outputting something I can't even see...
Code:

``` #include <iomanip> #include <ios> #include <iostream> #include <string> #include <vector> using std::cin; using std::setprecision; using std::cout; using std::string; using std::endl; using std::streamsize; using std::vector; int main(){         vector<string> students;     vector<int> grades;     string state;         for(int i = 0; state != "DONE"; i++){                 // ask for and read the student's name     cout << "Please enter student's first name: ";     string name;     cin >> name;     students.push_back(name);     // ask for and read the midterm and final grades     cout << "Please enter midterm and final exam grades: ";     double midterm, final;     cin >> midterm >> final;         // ask for the homework grades     cout << "Enter all your homework grades, "                   "followed by end-of-file if done,";     // the number and sum of grades read so far     int count = 0;     double sum = 0;     // a variable into which to read     double x;     // invariant:     // we have read count grades so far, and     // sum is the sum of the first count grades     while (cin >> x) {           ++count;           sum += x;           }         //adds final grade to the vector     double grade = 0.2 * midterm + 0.4 * final + 0.4 * sum / count;     grades.push_back(grade);         cout << "DONE?" ;     cin >> state; }         // write the result         cout << "Your final grades are: " << endl;     for(int i = 0; i != students.size(); i++){             cout << students[i]                 << setprecision(3) << grades[i] << endl;                 }         return 0;     }```
• 01-13-2009
CodeMonkey
The vector has to be of doubles as well, since you are putting doubles into it. Also, you don't use i in the first loop. Maybe use
Code:

```do {   //big code }while(state != "DONE");```
And, what did you say is the problem you're having? How do you know it is outputting if you can't see it?
• 01-13-2009
dnguyen1022
it outputs a jumble of strings/numbers at a speed the average human eye can not read..yeahh its still producing the same problem despite fixing the vector type and the do&while loop.

Code:

```#include <iomanip> #include <ios> #include <iostream> #include <string> #include <vector> using std::cin; using std::setprecision; using std::cout; using std::string; using std::endl; using std::streamsize; using std::vector; int main(){         vector<string> students;     vector<double> grades;     string state;         do{                 // ask for and read the student's name     cout << "Please enter student's first name: ";     string name;     cin >> name;     students.push_back(name);     // ask for and read the midterm and final grades     cout << "Please enter midterm and final exam grades: ";     double midterm, final;     cin >> midterm >> final;         // ask for the homework grades     cout << "Enter all your homework grades, "                   "followed by end-of-file if done,";     // the number and sum of grades read so far     int count = 0;     double sum = 0;     // a variable into which to read     double x;     // invariant:     // we have read count grades so far, and     // sum is the sum of the first count grades     while (cin >> x) {           ++count;           sum += x;           }         //adds final grade to the vector     double grade = 0.2 * midterm + 0.4 * final + 0.4 * sum / count;     grades.push_back(grade);         cout << "DONE?" ;     cin >> state;     }         while(state != "DONE");         // write the result         cout << "Your final grades are: " << endl;     for(int i = 0; i != students.size(); i++){             cout << students[i] << " " <<                 setprecision(3) << grades[i] << endl;                 }         return 0;     }```
• 01-14-2009
dnguyen1022
bump..i am still looking for help
• 01-14-2009
tabstop
Once somebody types end-of-file at you, you can't read from cin anymore. That's what end-of-file means, cin has no more data. You'll have to .clear() cin before it will work again.
• 01-14-2009
dnguyen1022
ohh nice, so it works nice. however there is one minor problem. It does not round off to 3 decimal places, but instead gives me a whole number. Here is my fixed code.

Code:

```#include <iomanip> #include <ios> #include <iostream> #include <string> #include <vector> #include <conio.h> using std::cin; using std::setprecision; using std::cout; using std::string; using std::endl; using std::streamsize; using std::vector; int main(){         vector<string> students;     vector<double> grades;     string state;         do{                 // ask for and read the student's name     cout << "Please enter student's first name: ";     string name;     cin >> name;     students.push_back(name);     // ask for and read the midterm and final grades     cout << "Please enter midterm and final exam grades: ";     double midterm, final;     cin >> midterm >> final;         // ask for the homework grades     cout << "Enter all your homework grades, "                   "followed by end-of-file if done,";     // the number and sum of grades read so far     int count = 0;     double sum = 0;     // a variable into which to read     double x;     // invariant:     // we have read count grades so far, and     // sum is the sum of the first count grades     while (cin >> x) {           ++count;           sum += x;           }         //adds final grade to the vector     double grade = 0.2 * midterm + 0.4 * final + 0.4 * sum / count;     grades.push_back(grade);         cin.clear();         cout << "DONE?" << endl;     cin >> state;     }         while(state != "DONE");         // write the result         cout << "Your final grades are: " << endl;     for(int i = 0; i != students.size(); i++){             cout << students[i] << " " <<                 setprecision(3) << grades[i] << endl;                 }                     cout << "Press any key to continue... ";   _getch();     return 0;     }```
• 01-14-2009
tabstop
I get one decimal place, but that's because the grades were two-digit numbers. setprecision doesn't give decimal places by default, because that's not what the word "precision" means. If you want precision to mean decimal places, you need to use fixed format.
• 01-14-2009
dnguyen1022
it doesn't? i mustve misunderstood what i read then..

http://www.cplusplus.com/reference/i...precision.html

Can you explain the "setprecision(#)" stuff to me please?
• 01-14-2009
tabstop
Precision = maximum number of digits. 2500 has two digits of precision (the 2 and the 5), as does 250, as does 2.5, as does 0.000025.

If you set to fixed format, then precision sets the (fixed) number of digits after the decimal point to display.