Hello all,
I am working on an assignment for my OOP1 class that requires reading a .DAT file, performing a bubble sort according to user input using the keyboard and then writing the sorted data to an output.DAT file. I am able to get my program to compile with no errors and the program accepts user input and says it has written the sorted data to the specified output file. But when I check the output file, there is nothing written to it. Please point me in the right direction and tell me what I may be overlooking.
Thank you
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char*argv[])
{
ifstream in ("C:\Assignment_4_Smith_Stacey_CIS326\clientListInput.DAT");
int numRecords = 0;
string line;
while (!in.eof()){
getline(in, line);
if (in.fail()){
break;
}
numRecords += 1;
}
in.close();
string *clients = new string[numRecords];
string *businessTypes = new string[numRecords];
in.clear();
in.open("C:\Assignment_4_Smith_Stacey_CIS326\clientListInput.DAT");
int pos =0;
while (!in.eof()){
getline(in, line);
if (pos < numRecords){
clients[pos] = line.substr(0, line.find(" : "));
businessTypes[pos]= line.substr(line.find(" , 1") +1);
pos +=1;
}
if (in.fail()){
break;
}
}
int option = 0;
while (option !=3){
cout <<"Sort by Business or Client?" <<endl;
cout <<"To sort by Client, enter 1" <<endl;
cout << "To sort by Business Type enter 2" <<endl;
cout << "To exit program enter 3" <<endl;
cin>>option;
cin.clear();
cin.ignore(INT_MAX,'\n');
string temp;
bool loop = true;
if (option == 3){
break;
}
if (option == 1){
while (loop){
loop = false;
for (int i = 0; i<numRecords -1; i++){
if (clients[i].compare(clients[i+1])>0){
temp = clients[i];
clients[i] = clients [i+1];
clients[i+1] = temp;
temp = businessTypes[i];
businessTypes[i] = businessTypes [i+1];
businessTypes[i+1] = temp;
loop = true;
}
}
}
}
else if (option ==2) {
loop = true;
while (loop){
loop = false;
for (int i = 0; i < numRecords - 1; i++){
if (businessTypes[i].compare(businessTypes[i +1]) >0) {
temp = businessTypes [i];
businessTypes[i] = businessTypes[i+1];
businessTypes[i+1] = temp;
temp = clients[i];
clients[i] = clients [i+1];
clients [i+1] = temp;
loop = true;
}
}
}
}
cout << endl <<"Client List has been sorted and copied to 'clientListOuput.DAT.'" << endl;
}
ofstream ofs ("C:\Assignment_4_Smith_Stacey_CIS326\clientListOutput.DAT");
for (int i = 0; i<numRecords; i++){
ofs <<clients [i] << " : " << businessTypes[i] << endl;
}
ofs.close();
delete[] clients;
delete[] businessTypes;
return 0;
}