Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Reading numbers with FOR loop solved: New problem emerges
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="GeekBoy, post: 2860526"] Thanks for the help obnoxious. Reading in the rest of the line with "getline" worked great and now the averages work. Now for the new problem. Program on first run works flawlessly. Next runs enter in some data I don;t know what instead of the data in the file. Tried closing and opening file again, but it's not working. #include<iomanip> #include<fstream> #include<iostream> #include<string> using namespace std; int main() { ifstream inFile; int months = 1; char quest; int x, y, z; float avg = 0, data; string dummy_string_buffer; inFile.open("input4.txt"); if (!inFile) { cerr << "Error Opening File" << endl; system ("pause"); return 1; } while (months >= 1 && months <= 12) { cout << "Please enter the number of months/year to be averaged: "; cin >> months; cout << "Year/" << endl; cout << " /Month"; for (x = 1; x <= months; x++) //prints out number of months header { cout << setw(4) << x; } // end of for loop #1 cout << " Avg\n" << endl ; for (y = 1; y <= 10; y++) // loop for 10 years of rain fall data { cout << setw(2)<< y << " "; for (z = 1; z <= months; z++) // loop for (x)months of rain fall { inFile >> data; cout << setw(4) << data << setw(4); avg = data + avg; } // end of for loop #3 getline(inFile, dummy_string_buffer); // Gets a line from the file and stores it in dummy_string_buffer // cout << dummy_string_buffer; cout <<": " << setw(2) << static_cast<int>(avg/ static_cast<float>(months)); cout << " inches\n"; avg=0; } //end of for loop #2 cout << "Do you want to do it again? (y/n:)"; cin >> quest; if (quest == 'n'){ break; } else{ system ("cls"); //clears screen //inFile.close(); // inFile.open("input4.txt"); //data =0; //if (!inFile) //{ //cerr << "Error Opening File" << endl; //system ("pause"); //return 1; // } } //end of else } //end of while loop inFile.close(); system ("pause"); return 0; } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Reading numbers with FOR loop solved: New problem emerges
Top