0% found this document useful (0 votes)
52 views8 pages

Practical 11 Part B Input

The document contains the source code for 4 programs that perform various file input/output operations: 1. A program that reads numbers from a file, calculates the product of all numbers, and outputs the result. 2. A program that reads numbers from a file into an array, and outputs the numbers in reverse order. 3. A program that prompts the user for words and writes each letter of each word to an output file. 4. A program that reads student records from a file into a struct, calculates averages, and writes a report to an output file.

Uploaded by

Ng Siewmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views8 pages

Practical 11 Part B Input

The document contains the source code for 4 programs that perform various file input/output operations: 1. A program that reads numbers from a file, calculates the product of all numbers, and outputs the result. 2. A program that reads numbers from a file into an array, and outputs the numbers in reverse order. 3. A program that prompts the user for words and writes each letter of each word to an output file. 4. A program that reads student records from a file into a struct, calculates averages, and writes a report to an output file.

Uploaded by

Ng Siewmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PRACTICAL 11

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;

void list(PRODUCT_TYPE prod[], int rows);


void showPrice(PRODUCT_TYPE prod[], int rows);
void updatePrice(PRODUCT_TYPE prod[], int rows);
int searchProduct(PRODUCT_TYPE prod[], int rows);

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;

// read next number


inFile >> products[++index].number;
}
inFile.close();

// 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;
}

void list(PRODUCT_TYPE prod[], int rows)


{
cout << fixed << setprecision(2);
for (int i = 0; i<rows; i++)
cout << prod[i].number << "\t" << prod[i].desc << "\t"
<< setw(8) << prod[i].price << endl;
return;
}

int searchProduct(PRODUCT_TYPE prod[], int rows)


{
int i = 0;
bool found = false;
char number[6];

cout << "Enter product number to search: ";


cin >> number;

while (i<rows && !found)


{
if (strcmp(number, prod[i].number) == 0)
found = true;
else
i++;
}
if (found)
return i;
else
return -1;
}

void showPrice(PRODUCT_TYPE prod[], int rows)


{
int pos = searchProduct(prod, rows);
if (pos != -1)
cout << "Current price is " << fixed << setprecision(2) << prod[pos].price
<< endl;
else
cout << "Product not found\n";

return;
}

void updatePrice(PRODUCT_TYPE prod[], int rows)


{
double newPrice;

int pos = searchProduct(prod, rows);


if (pos != -1)
{
cout << "Current price is " << fixed << setprecision(2) << prod[pos].price;
cout << "\nEnter new price: ";
cin >> newPrice;
prod[pos].price = newPrice;
cout << "Price updated!\n";
}
else
cout << "Product not found\n";

return;
}

OUTPUT

You might also like