Advertisement
Porr011

Lesson 9 activity 5

Feb 17th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10.     // filestream variable file
  11.     fstream file;
  12.     string Name, clas, gpa, filename;
  13.     int count=0;
  14.  
  15.     // filename of the file
  16.   cout << "Enter the  file name (file.txt)" << endl;
  17.     cin >> filename; // putting the stars when each part starts
  18.     cout << "**********" << endl;
  19.  
  20.     // opening file
  21.     file.open(filename);
  22.    
  23.  if(file){
  24.     // extracting words form the file
  25.     while (file >> Name)
  26.     {
  27.         // displaying content
  28.          count=count + 1;
  29.             // couting the Name,Class,and GPA
  30.             if (Name=="Name:"){
  31.         cout << Name << endl;
  32.     }
  33.     }
  34.     // trying to separate the class
  35.     while (file >> clas)
  36.     {
  37.         count=count + 1;
  38.         if (clas=="Class:"){
  39.             cout << clas << endl;
  40.         }
  41.     }
  42.     // trying to separate the gpa
  43.     while (file >> gpa)
  44.     {
  45.         count= count + 1;
  46.         if (gpa=="GPA:"){
  47.             cout << gpa << endl;
  48.         }
  49.     }
  50. }
  51. // else statement for the infile if statement
  52. else
  53. {
  54.       cout << "Error";
  55.       //error if file is not open
  56.   }
  57.   // ending with asterisks
  58.   if (count>5){
  59.                 count=0;
  60.                 cout << "**********" << endl;
  61.             }
  62.    
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement