Lect - 12 - Input - Out Functions - Getline - Getch - Stringhandling - FileHandling3
Lect - 12 - Input - Out Functions - Getline - Getch - Stringhandling - FileHandling3
return 0;
}
Issue if we enter the name with spaces, cin >> unable to read spaces
Output:
Please enter your name muhammad moosa
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
#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();
#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;
}
#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;
}
return 0;
}
#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()
outputFile.open("EmpolyeeData.txt");
// 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");
1.
Output:
The total salary = 51500
2.
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.
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.