Practical 11 Part B Input
Practical 11 Part B Input
PART B
QUESTION 1
INPUT
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
using namespace std;
int main(void)
{
int number;
long long product = 1;
ifstream inFile("numbers.txt");
if (!inFile)
cout << "Error opening input file\n";
else {
inFile >> number;
while (inFile)
{
product *= number;
inFile >> number;
}
cout << "Product of the numbers is " << product << endl;
inFile.close();
}
return 0;
}
OUTPUT
PART B
QUESTION 2
INPUT
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
using namespace std;
int main(void)
{
int numbers[30],count=0;
ifstream inFile("numbers.txt");
if (!inFile)
cout << "Error opening input file\n";
else {
inFile >> numbers[count];
while (inFile)
{
//or count++; here
inFile >> numbers[count ++];
}
cout << "The numbers in reverse are:\n ";
for (int i = count - 1; i >= 0; i--)
{
cout << numbers[i] << " ";
}
cout << endl;
inFile.close();
}
return 0;
}
OUTPUT
PART B
QUESTION 3
INPUT
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
using namespace std;
int main(void)
{
char word[21];
ofstream outFile("output.txt");
if (!outFile)
cout << "Error opening input file\n";
else
{
do
{
cout << "Enter a word: ";
cin >> word;
for (int i = 0; i < strlen(word); i++)
outFile << word[i] << " ";
outFile << endl;
} while (cin);
outFile.close();
}
return 0;
}
OUTPUT
PART B
QUESTION 4
INPUT
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <iomanip>
using namespace std;
typedef struct
{
char reg_no[7];
float scores[3];
float average;
}STUDENT_TYPE;
int main(void)
{
STUDENT_TYPE students[50];
int index = 0;
float total;
ifstream inFile("student.txt");
ofstream outFile("report.txt");
if (!inFile || !outFile)
{
if(!inFile)
cout << "Error opening input file\n";
if (!inFile)
cout << "Error opening output file\n";
}
else
{
outFile << "Reg. No.\t Test1\t Test2\t Test3\t Average\n";
inFile >> students[index].reg_no;
while (inFile)
{
//read record and calculate average
total = 0.0 ;
for (int i = 0; i < 3; i++)
{
inFile >> students[index].scores[i];
total += students[index].scores[i];
}
students[index].average = total / 3;
//write report
outFile << fixed << setprecision(1);
outFile << students[index].reg_no << "\t\t";
for (int i = 0; i < 3; i++)
outFile << setw(6) << students[index].scores[i] << "\t";
outFile << setw(8) << students[index].average << endl;
//read nect reg no
inFile >> students[++index].reg_no;
}
inFile.close();
outFile.close();
}
return 0;
}
OUTPUT
PART D
QUESTION 1
INPUT
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstring>
#include <iomanip>
using namespace std;
typedef struct
{
char number[6];
char desc[31];
double price;
} PRODUCT_TYPE;
int main()
{
PRODUCT_TYPE products[50];
int index = 0, choice;
ifstream inFile("product.txt");
if (!inFile)
cout << "Error opening input file\n";
else
{
inFile >> products[index].number;
while (inFile)
{
inFile.ignore();
inFile.getline(products[index].desc, 31);
inFile >> products[index].price;
// menu starts
do
{
cout << "\nDo you want to:\n";
cout << "1. List all products\n";
cout << "2. Display the price for a product\n";
cout << "3. Update the price for a product\n";
cout << "4. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1: list(products, index);
break;
case 2: showPrice(products, index);
break;
case 3: updatePrice(products, index);
break;
case 4: break;
default: cout << "Invalid choice\n";
}
} while (choice != 4);
ofstream outFile("product.txt");
if (!outFile)
cout << "Error opening output file, records are not updated.\n";
else
{
for (int i = 0; i< index; i++)
{
outFile << products[i].number << endl;
outFile << products[i].desc << endl;
outFile << products[i].price << endl;
}
outFile.close();
}
}
return 0;
}
return;
}
return;
}
OUTPUT