Saturday, July 18, 2020

CS201 Assignment No.3 Solution with Download Link 2020























Code:



#include<iostream>
#include<fstream>
#include <stdlib.h>
using namespace std;

class sportorts
{
private :
string playername;
string game_name;
int total_wins;
public :
sportorts()
{
playername="";
game_name="";
total_wins=0;
}
void input_file()
{
cout<<"\nWriting in the file";
cout<<"\nEnter the name of the player : ";
cin>>playername;
cout<<"Enter the sportorts name of the player : ";
cin>>game_name;
cout<<"Enter the number of wins for the current season : ";
cin>>total_wins;

ofstream outFile;
char FileName[] = "Sample.txt";
outFile.open(FileName, ios::out);
if (!outFile)
{
cout << "Can't open input file named Sample.txt"<< endl;
exit(1);
}
outFile<<playername<<"\n";
outFile<<game_name<<"\n";
outFile<<total_wins;
outFile.close();
cout<<"\nData have been entered in the file";
}
void read_file()
{
char FileName[] = "Sample.txt";
ifstream inFile;
inFile.open(FileName, ios::in);

if (inFile)

      string dt;
      while(getline(inFile, dt))
{
          cout << dt << "\n";
      }
      inFile.close();
    }
    else
    {
    cout << "Can't open input file named " << FileName << endl;
exit(1);
}
}
void notstartingwithA()
{
char FileName[] = "Sample.txt";
ifstream inFile;
inFile.open(FileName, ios::in);
int count=0;
char s;
if (inFile)

      string dt;
      while(getline(inFile, dt))
{
          s=dt.at(0);
          if(s!='A')
          {
          count++;
}
      }
      inFile.close();
      cout<<"\nThe number of lines not started with 'A' including empty lines : "<<count<<endl;
    }
    else
    {
    cout << "Can't open input file named " << FileName << endl;
exit(1);
}
}
void lower()
{
char FileName[] = "Sample.txt";
ifstream inFile;
inFile.open(FileName, ios::in);
int count=0;
char s;
if (inFile)

      string dt;
      while(getline(inFile, dt))
{
          s=dt.at(0);
          if(islower(s))
          {
          count++;
}
      }
      inFile.close();
      cout<<"\nThe number of lines started with lower character are : "<<count<<endl;
    }
    else
    {
    cout << "Can't open input file named " << FileName << endl;
exit(1);
}
}
void upper()
{
char FileName[] = "Sample.txt";
ifstream inFile;
inFile.open(FileName, ios::in);
int count=0;
char s;
if (inFile)

      string dt;
      while(getline(inFile, dt))
{
          s=dt.at(0);
          if(isupper(s))
          {
          count++;
}
      }
      inFile.close();
      cout<<"\nThe number of lines started with upper character are: "<<count<<endl;
    }
    else
    {
    cout << "Can't open input file named " << FileName << endl;
exit(1);
}
}
void digit()
{
char FileName[] = "Sample.txt";
ifstream inFile;
inFile.open(FileName, ios::in);
int count=0;
char s;
if (inFile)

      string dt;
      while(getline(inFile, dt))
{
          s=dt.at(0);
          if(isdigit(s))
          {
          count++;
}
      }
      inFile.close();
      cout<<"\nThe number of lines started with digit are : "<<count<<endl;
    }
    else
    {
    cout << "Can't open input file named " << FileName << endl;
exit(1);
}
}

};

main()
{

int choice;
sportorts sport;
char iterate;
do
{


cout<<"\n\n************** WELCOE TO THE UNIVERSITY sportORTS SURVEY SYSTEM ****************";
cout<<"\n\n************** Press 1 for entering record of player ************************";
cout<<"\n************** Press 2 for reading data from the file ***********************";
cout<<"\n************** Press 3 for checking line not started with A *****************";
cout<<"\n************** Press 4 for checking line started with lower case characters**";
cout<<"\n************** Press 5 for checking line started with upper case characters**";
cout<<"\n************** Press 6 for checking line started with digits ****************\n\n";
cin>>choice;

switch(choice)
{
case 1 :
sport.input_file();
break;
case 2 :
sport.read_file();
break;
case 3 :
sport.notstartingwithA();
break;
case 4 :
sport.lower();
break;
case 5 :
sport.upper();
break;
case 6 :
sport.digit();
break;
default :
cout<<"\nInvalid choice entered!\n";
break;
}
cout<<"\nDo you want to continue (y for yes) : ";
cin>>iterate;
}
while(iterate=='y'|| iterate=='Y');
}

No comments:

Post a Comment