int next Int int x do coutPlease enter

  • Slides: 31
Download presentation

int next. Int() { int x; do{ cout<<"Please enter an int number: "; cin.

int next. Int() { int x; do{ cout<<"Please enter an int number: "; cin. clear(); cin>>x; }while (cin. fail()); return x; }

cout << "Enter a floating point number (or CTRL d to end): " <<

cout << "Enter a floating point number (or CTRL d to end): " << endl; cin >> Num; while (! cin. fail()) { Total = Total + Num; Count++; cout << "Enter a floating point number (or CTRL d to end): " << endl; cin >> Num; } if (Count > 0) cout << "Average is " << Total / Count << endl; else cout << "No data given" << endl;

#include <iostream> //readfile 2. cpp #include <fstream> using std: : ifstream; using std: :

#include <iostream> //readfile 2. cpp #include <fstream> using std: : ifstream; using std: : cout; using std: : endl; void read. From. File( char name[]){ ifstream in; char s[20]; in. open(name); if (in. is_open()){ while(!in. eof()){ in>>s; cout<<s<<endl; } } else cout<<"The file was not opened"; in. close(); }

#include <iostream> // readfile. cpp #include <fstream> // needed for files using namespace std;

#include <iostream> // readfile. cpp #include <fstream> // needed for files using namespace std; int main() { fstream In. File; float Num, Total; int Count; In. File. open("readfile. txt", ios: : in); if (In. File. fail()) { cout << "Could not open readfile. txt" << endl; exit(1); } Count = 0; Total = 0. 0; In. File >> Num; while (! In. File. fail()) { Total = Total + Num; Count++; In. File >> Num; } if (Count > 0) cout << "Average is " << Total / Count << endl; else cout << "No data given" << endl; In. File. close(); return 0; }

using std: : ofstream; //writefile. cpp void write. To. File( char name[], char to.

using std: : ofstream; //writefile. cpp void write. To. File( char name[], char to. Write[]) { ofstream out; out. open(name); if(!out. is_open()) cout<<"File was not opened"<<endl; else out<<to. Write; out. close(); }

#include <iostream> //writefile 2. cpp #include <fstream> using namespace std; int main() { float

#include <iostream> //writefile 2. cpp #include <fstream> using namespace std; int main() { float Num; fstream Out. File; Out. File. open(“writefile 2. txt", ios: : out); if (Out. File. fail()) { cout << "Open of writefile 2. txt failed" << endl; exit(1); } cout << "Enter a floating point number (or CTRL z to end): " << endl; cin >> Num; while (! cin. fail()) { // Process the data: Out. File << Num << endl; cout << "Enter a floating point number (or CTRL z to end): " << endl; cin >> Num; } Out. File. close(); return 0; }

#include <iostream> #include <fstream> using namespace std; void Process. File(fstream & fs); int main()

#include <iostream> #include <fstream> using namespace std; void Process. File(fstream & fs); int main() { fstream In. File; In. File. open("readfile. txt", ios: : in); if (In. File. fail()) { cout << "Could not open readfile. txt“ << endl; exit(1); } Process. File(In. File); In. File. close(); return 0; } void Process. File(fstream & fs) { float Num, Total; int Count; Count = 0; Total = 0. 0; fs >> Num; while (! fs. fail()) { Total = Total + Num; Count++; fs >> Num; } if (Count > 0) cout << "Average is " << Total / Count << endl; else cout << "No data given" << endl; }

 פתרון void read. From. Position(char name[], int pos, char to[]) { ifstream in;

פתרון void read. From. Position(char name[], int pos, char to[]) { ifstream in; in. open(name); in. seekg(pos); in>>to; in. close(); }

 מספר התווים בקובץ int size(char name[]) { ifstream in; in. open(name); in. seekg(0,

מספר התווים בקובץ int size(char name[]) { ifstream in; in. open(name); in. seekg(0, std: : ios: : end); return in. tellg(); }

void grades(char name[]) { fstream f; f. open(name, std: : ios: : in |

void grades(char name[]) { fstream f; f. open(name, std: : ios: : in | std: : ios: : out); char student[40]; int grade, begin=0, end; char c; f>>student>>grade; while(!f. eof()) { if(grade < 55) c = '-'; else if(grade > 90) c = '+'; else c = ' '; end = f. tellg(); f. seekp(begin); f<<c; end++; begin = end; f. seekg(end); f>>student>>grade; } f. close(); }