1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main () {
string ModelType, // Stores the command Models (model, update, range, and exit) given by Script.txt
ComputerModel, // Model of the Computer
ModelName[100], // Stores the models of the computers in this array
ModelTypeComputers[100], // Stores the command models of the computers in this array
AnotherComputerModel[100], // Stores the models of the computers in this array
ComputerModelName, // Computer Model Names
ComputerName[100], // Array for computer model names
TheManufacturerName, // Manufacturers
ManufacturerName[100]; // Manufacturer arrays
int newprice = 0, // New, updated prices specified by Script.txt
NewPrice[100], // Array of the new, updated prices specified by Script.txt
LowerBound = 0, // Lower bound
UpperBound = 0, // Upper bound
NumLines = 0, // Counter
Price, // Original prices of the computers
ComputerPrice[100], // Array for the prices
truelength = 0,
idx = 0, // Counter
NewLines = 0, // Counter
Lines = 0, // Counter
MoreNewLines = 0, // Counter
LoopCount = 0; // Counter
ifstream iFile; // open input file stream
iFile.open("Script.txt"); // open input file: Compsys.txt
while (iFile) {
iFile >> ModelType; // Start reading line
ModelName[NumLines] = ModelType;
iFile.ignore(4, '\t');
if (ModelName[NumLines] == "model") {
getline (iFile, ComputerModel, '\n'); // Continue reading the line
AnotherComputerModel[NumLines] = ComputerModel; // Build array for the computer models supplied by the second input file
}
else if (ModelName[NumLines] == "update") {
getline(iFile, ComputerModel, '\t');
AnotherComputerModel[NumLines] = ComputerModel; // Build array for the computer models supplied by Script.txt input file
iFile >> newprice;
NewPrice[NumLines] = newprice; // Grab the updated prices
}
else if (ModelName[NumLines] == "range") {
iFile >> LowerBound;
iFile >> UpperBound; // Read range bounds
}
else if (ModelName[NumLines] == "exit") {
break;
}
NumLines++;
}
iFile.close();
iFile.open("CompSys.txt"); // open input file: CompSys.txt
while (iFile) { // take in data until the data file runs out of data; puts data into arrays mentioned above and in loop
getline(iFile, ComputerModelName, '\t'); // Getting data for ComputerModel
ComputerName[idx] = ComputerModelName;
getline(iFile, TheManufacturerName, '\t'); // Getting data for ManufacturerName
ManufacturerName[idx] = TheManufacturerName;
iFile >> Price; // Getting data for ComputerPrice
ComputerPrice[idx] = Price;
idx++;
}
truelength = idx - 2;
iFile.close();
ofstream oFile; // output file stream
string it;
oFile.open("CompLog.txt"); // open output file
oFile << "Programmer: Evan Becker" << endl; // Build and output the header
oFile << "CS 1044 Project 4 Fall 2011" << endl;
oFile << "--------------------------------------------------------------------" << endl;
//while () {
while (LoopCount <= NumLines) {
oFile << AnotherComputerModel[Lines] << '\t' << ComputerName[NewLines] << '\t' << MoreNewLines << '\t' << truelength << endl << endl;
if (AnotherComputerModel[Lines] == ComputerName[NewLines]) { //The trouble is here. C++ compares once, but not again, despite elements being the same. I don't understand.
Lines++;
LoopCount++;
}
else {
if (NewLines < truelength) {
NewLines++;
}
else{
oFile << AnotherComputerModel[Lines] << " not found" << endl;
}
}
}
oFile.close();
}
| |