0% found this document useful (0 votes)
5 views

Lect - 12 - Input - Out Functions - Getline - Getch - Stringhandling - FileHandling3

The document discusses various ways of handling input and output in C++ programs using concepts like cin, cout, getline(), file handling etc. It provides code examples to demonstrate reading input with spaces, mixing different input methods, writing to and reading from files.

Uploaded by

uh2683505
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lect - 12 - Input - Out Functions - Getline - Getch - Stringhandling - FileHandling3

The document discusses various ways of handling input and output in C++ programs using concepts like cin, cout, getline(), file handling etc. It provides code examples to demonstrate reading input with spaces, mixing different input methods, writing to and reading from files.

Uploaded by

uh2683505
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1. 3.

8 Working with Characters and string objects:


Output:
#include<iostream>
#include <fstream> Please enter your name Moosa
#include <string> Please enter city you live in
using namespace std; Lahore
int main()
Hello Moosa
{
string name; You live in Lahore
string city;
cout << "Please enter your name ";
cin >> name;

cout << "Please enter city you live in ";


cin >> city;

cout << "Hello " << name << endl;


cout << "You live in " << city << endl;

return 0;
}

Issue if we enter the name with spaces, cin >> unable to read spaces

Output:
Please enter your name muhammad moosa

Please enter city you live in Hello muhammad

You live in moosa

2. getline() reading string having spaces


#include<iostream>
#include <fstream> Please enter your name
#include <string> muhammad moosa
using namespace std; Please enter city you live in lahore
int main() Hello muhammad moosa
{
string name;
You live in lahore
string city;
cout << "Please enter your name ";
getline (cin, name);

cout << "Please enter city you live in ";


getline (cin, city);

cout << "Hello " << name << endl;


cout << "You live in " << city << endl;

return 0;
}
Type a character
3. Inputting a character :
and press enter: a
Your entered a
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char ch;
cout << "Type a character and press enter: ";
cin >> ch;
cout << "Your entered " << ch << endl;
return 0;
}

// issue: cin >> unable to read enter and space characters from the keyboard

4. Use of cin.get(ch) OR ch =cin.get


// This program shows the three ways to pause the program

#include<iostream> Output:
using namespace std; This program has a paused. Press enter to continue
int main()
It has paused a second time, Please press Enter again:
{
It has paused third time, Please press Enter again:
Thank you!
char ch;
cout << "This program has a paused. Press enter to continue ";
cin.get(ch);
cout << "It has paused a second time, Please press Enter again: ";
ch = cin.get();
cout << "It has paused third time, Please press Enter again: ";
cin.get();

cout << "Thank you!";


return 0;
}
5. Mixing cin >> and cin.get
// This program demostrates the problem occurs when mixing cin >> and cin.get()

#include<iostream>
#include <string>
using namespace std;
int main() Enter a number 30
{ Enter a character Thank you!
char ch;
int number;
cout << "Enter a number ";
cin >> number;
cout << "Enter a character ";
cin.get(); // it is okay with cin >> ch;
cout << "Thank you!";
return 0;
}

Contents on keyboard buffer: issue is cin does not read \n


9 . use of igone()
cin.igonre(n, ch) // tell the cin object to skip number of characters or ch characters
occurring in keyboard buffer.
// This program successfully uses both cin >> and cin.get() for keyboard input

#include<iostream>
#include <fstream>
#include <string> Enter a number 30
using namespace std;
int main() Enter a character c
{
char ch; Thank you!
int number;
cout << "Enter a number ";
cin >> number;
cin.ignore();
cout << "Enter a character ";
cin.get(); // it is okay with cin >> ch;
cout << "Thank you!";
return 0;
}

10.sting members functions and Operators


// This program demostrate string memeber function (lenght()) and operators (+, +=)

#include<iostream> Sample Output:


#include <fstream>
#include <string> length of concatenated name is: 12
using namespace std; Concatenated name using + operator:
int main() Muhammad Ali
{ Concatenation uisng += opertor: Hello
string name1 = "Muhammad";
Muhammad Ali
string name2 = "Ali";
string name = name1 + " " + name2;
int size = name.length();
cout << "length of concatenated name is: " <<size << endl;
cout << "Concatenated name using + operator: " << name << endl;
string greeting = "Hello ";
greeting += name;
cout << "Concatenation uisng += opertor: " << greeting;

return 0;
}

12. what about mixing of cin >> and getline(cin, name)

#include<iostream>
#include <string>
using namespace std;
int main()
{
string name;
double sal;
cout << "Enter the Salay ";
cin >> sal;
cin.ignore();
cout << "Enter a employee name ";
getline(cin, name); // it is okay with cin >> ch;

cout << "Entered values: " << sal << ", " << name << endl;
cout << "Thank you!";
return 0;
}

13. what about mixing of cin >> and getline(cin, name) and cin.get()

;;;;;;;;;;; File handling


1. Save names having space in the file using string’s getline (cin , name1) function
"friends.txt"
#include<iostream> muhammad moosa
#include <fstream> muhammad wahab
#include <string> ali raza
using namespace std;
int main()
{
// Open file
ofstream outputFile;
string name1, name2, name3;
//outputFile.open("H:\\PUCIT\\PF_2024_Spring\\CLassLecture_PF\\demoFile5.txt");
outputFile.open("friends.txt");

// get name of three friends from the user


cout << "Enter a name of friend 1: ";
getline (cin , name1);
cout << "Enter a name of friend 2: ";
getline(cin, name2);
cout << "Enter a name of friend 3: ";
getline(cin, name3);

// write some message to the file


if (!outputFile.fail())
{
cout << "Now writting data to the file" << endl;

outputFile << name1 << endl;


outputFile << name2 << endl;
outputFile << name3 << endl;

cout << "Done!";


}
else
{
cout << "There is some error in file writting";
}
//close the file
outputFile.close();
return 0;
}
EmpolyeeData.txt

2 saving emplyee’s data on the file: muhammad moosa 3000


#include<iostream> muhammad wahab 4000
#include <fstream> fatima bibi 5000
#include <string>
using namespace std;
int main()
{
// Open file
ofstream outputFile;
string name1, name2, name3;
double salary1,salary2, salary3;

outputFile.open("EmpolyeeData.txt");

// get name of three friends from the user


cout << "Enter the name of Empolyee 1: ";
getline (cin , name1);
cout << "Enter the salary of Empolyee 1: ";
cin >> salary1;
cin.ignore();
cout << "Enter a name of Empolyee 2: ";
getline(cin, name2);
cout << "Enter the salary of Empolyee 2: ";
cin >> salary2;
cin.ignore();
cout << "Enter a name of Empolyee 3: ";
getline(cin, name3);
cout << "Enter the salary of Empolyee 3: ";
cin >> salary3;
cin.ignore();
// write some message to the file
if (!outputFile.fail())
{
cout << "Now writting data to the file" << endl;

outputFile << name1 <<" " << salary1<<endl;


outputFile << name2 <<" " << salary2 <<endl;
outputFile << name3 <<" " << salary3 << endl;

cout << "Done!";


}
else
{
cout << "There is some error in file writting";
}
//close the file
outputFile.close();
return 0;
}

3 ; Reading employee’s data from the “EmpolyeeData.txt” file


Sample output:
Now reading data from the file
Done!
Average Salary of muhammad moosa, muhammad wahab, and fatima bibi: 4000

// reading empolyee data from the file. each record is saved like : ali raza 200
// read the two data as string in name and then salary as double

#include<iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Open file
ifstream fin;
string name1, name2, name3;
string str1, str2;
double salary1 = 0.0,salary2 = 0.0, salary3 = 0.0 , average= 0.0;
//fin.open("H:\\PUCIT\\PF_2024_Spring\\CLassLecture_PF\\EmpolyeeData.txt");
fin.open("EmpolyeeData.txt");

// reading empolyess's data from the file


if (!fin.fail())
{
cout << "Now reading data from the file" << endl;

fin >> str1 >> str2;


name1 = str1 +" "+ str2;
fin >> salary1;

fin >> str1 >> str2;


name2 = str1+ " " + str2;
fin >> salary2;

fin >> str1 >> str2;


name3 = str1 +" " +str2;
fin >> salary3;

cout << "Done!" << endl;


}
else
{
cout << "There is some error in file writting";
}

// average salary of empolyees are:


average = (salary1 + salary2 + salary3) / 3;
cout << "Average Salary of " << name1 << ", " << name2 << ", and " << name3 << ":
";
cout << average;
//close the file
fin.close();
return 0;
}
;;;;;;;;;;;;;;;
Homework:

1.

Sample input file


Aamir 12000
Amara 15000
Adnan 13000
Afzal 11500

Output:
The total salary = 51500

2.

The sample input file is:


Syed N Ali
Muhammad A Butt
Faisal A Malik
Muhammad A Jamil

If the above file is used as input, the output should be as follows:

Syed Nali
Muhammad Abutt
Faisal Amalik
Muhammad Ajamil

3. Write a simple program, which will read from a file ‘myfile.txt’ and print it on
the screen. myFile.txt” contains employee’s name, salary and department of employees.

Sample “myfile.txt”.
Name Salary Department
Aamir 12000 Sale
Amara 15000 HR
Adnan 13000 IT
Afzal 11500 Marketing
4. The program calculates the average age and average GPA of a class. Also determine
the grade of the class and the student with max GPA. You should read the data of a
student from a file manipulate it to get the desired result.

Sample input file


Ali Pf 20 3.8
Wahab Pf 20 3.9
Habib PF 18 4.0

5. Create a program that replicates content from one file to another. User interaction is needed
to input the filenames. The initial filename is identified as the source file, and the
subsequent filename is identified as the destination file. You may reference the input file
mentioned in question 4.

You might also like