Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ converting txt file data into double variables
    primarykey
    data
    text
    <p>Hi so I started to create a program to calculate a person's GPA and save that info for future reference. My problem is that I can't figure out how to read the numbers saved in the .txt file and then store them for GPA calculating purposes. Here is the unfinished program's code and any help would be great</p> <p>EDIT: The .txt file is laid out like this: 5.0 3.7 5.0 3.7 5.0 4.0 ... Below is my progress in the program but when I run it I receive a GPA of 0 (incorrect). Not sure if the lexical cast is my problem, the getline() method or something else. Any help (the calculateGPA() method is the trouble area)?</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;boost/lexical_cast.hpp&gt; using namespace std; string newCC, newCG; double curGPA; char command; bool running = 1; void calculateGPA(); void writeGrades(); void mainMenu(); void addClass(); int main() { while(running) { mainMenu(); } return 0; } void calculateGPA() { double credit_sum = 0.0, grade_sum = 0.0; double credit, grade; ifstream gReader("grades.txt"); for( int i = 0; ! gReader.eof() ; i++ ) { string number; getline( gReader , number ) ; double dblNumber; try { dblNumber = boost::lexical_cast&lt;double&gt;(number); } catch (boost::bad_lexical_cast const&amp;) { dblNumber = 0; } credit_sum = credit_sum + dblNumber; } ifstream cReader("credits.txt"); for( int i = 0; ! cReader.eof() ; i++ ) { string number; getline( cReader , number ) ; double dblNumber; try { dblNumber = boost::lexical_cast&lt;double&gt;(number); } catch (boost::bad_lexical_cast const&amp;) { dblNumber = 0; } credit_sum = credit_sum + dblNumber; } if(credit_sum == 0.0) { curGPA = 0.0; } curGPA = (grade_sum / credit_sum); cReader.close() ; gReader.close() ; }//End calculateGPA void writeGrades() { string cToWrite = newCC + "\n"; string gToWrite = newCG + "\n"; ofstream cWriter("credits.txt", ios::app); cWriter &lt;&lt; cToWrite; ofstream gWriter("grades.txt", ios::app); gWriter &lt;&lt; gToWrite; cWriter.close(); gWriter.close(); }//End writeGrades void addClass() { cout &lt;&lt; "New class' credits?"; cin &gt;&gt; newCC; cout &lt;&lt; endl &lt;&lt; "New class' grade? (GP)"; cin &gt;&gt; newCG; writeGrades(); cout &lt;&lt; "Add another class? (y/n)" &lt;&lt; endl; cin &gt;&gt; command; if(command == 'y') addClass(); else mainMenu(); }//End addClass void mainMenu() { string command; cout &lt;&lt; "What would you like to do?" &lt;&lt; endl; cout &lt;&lt; "(V)iew GPA" &lt;&lt; endl; cout &lt;&lt; "(A)dd grades" &lt;&lt; endl; cout &lt;&lt; "(E)xit" &lt;&lt; endl; cin &gt;&gt; command; if(command == "v") { calculateGPA(); cout &lt;&lt; "Your current GPA is " &lt;&lt; curGPA &lt;&lt; endl; } else if(command == "a") { addClass(); } else running = 0; }//End mainMenu </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload