Setting Precision Confusion

This is a discussion on Setting Precision Confusion within the C++ Programming forums, part of the General Programming Boards category; Hello all, so I am doing an exercise from a book, and I am suppose to take multiple students name, ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    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;
        }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You've defined grade as an integer. No such thing as precision with an integer. Try another datatype, like float or double.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Also precision only affects how the floating point value is output (show me that many digits), not the calculations.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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;
        }

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    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?
    Last edited by CodeMonkey; 01-13-2009 at 02:20 PM. Reason: grammar
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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;
        }
    Last edited by dnguyen1022; 01-14-2009 at 09:19 AM.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    Talking

    bump..i am still looking for help

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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;
        }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 11:34 AM
  2. Setting precision?
    By markucd in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2006, 05:53 AM
  3. Setting decimal precision in fstream
    By CodeMonkey in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2003, 06:08 PM
  4. Your favourite fantasy game setting?
    By fry in forum Game Programming
    Replies: 4
    Last Post: 10-16-2002, 07:26 AM
  5. setting a float to 4 digit decimal precision
    By river-wind in forum C Programming
    Replies: 8
    Last Post: 01-21-2002, 05:03 PM

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