How do I write this code in C++ String class instead of C strings? #ifdef _MSC_V
ID: 3557600 • Letter: H
Question
How do I write this code in C++ String class instead of C strings?
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
class ClientData
{
public:
ClientData(char*, char*);
void inputData();
void outputData();
void bubbleSort(int);
private:
static const int ROWS=6;
char* inputfile;
char* outputfile;
char clients[ROWS][31];
char* client_rows[ROWS];
char businesses[ROWS][31];
char* business_rows[ROWS];
char nextchar;
char client_chars[31];
char* client_word;
char business_chars[31];
char* business_word;
};
// 1-parameter constructor with passed file name
ClientData::ClientData(char* infilename, char* outfilename)
:inputfile(infilename), outputfile(outfilename)
{
//cout << "Created a ClientData instance with 2-parameter constructor" << endl;
cout << "Input file: " << inputfile << endl;
cout << "Output file: " << outputfile << endl;
};
// Input the data from a file
void ClientData::inputData()
{
int pos;
cout << "Reading from file: " << inputfile << endl;
ifstream infile(inputfile);
// Set up strings for inputs
client_word = &client_chars[0];
business_word = &business_chars[0];
for (int row=0; row < ROWS; row++)
{
// Get client
pos = 0;
client_word[pos] = '';
nextchar = infile.get();
//cout << "priming client char = " << nextchar << endl;
for (pos = 0; pos < 30; pos++)
{
client_word[pos] = nextchar;
nextchar = infile.get();
//cout << "loop client char = " << nextchar << endl;
}
client_word[pos] = '';
//cout << "client = " << client_word << endl;
client_rows[row] = &clients[row][0];
strcpy(client_rows[row],client_word);
// Get business
pos = 0;
business_word[pos] = '';
nextchar = infile.get();
//cout << "priming business char = " << nextchar << endl;
for (pos = 0; pos < 30; pos++)
{
business_word[pos] = nextchar;
nextchar = infile.get();
//cout << "loop business char = " << nextchar << endl;
}
business_word[pos] = '';
//cout << "business = " << business_word << endl;
business_rows[row] = &businesses[row][0];
strcpy(business_rows[row],business_word);
// Ignore rest of line
infile.ignore();
}
};
// Output the data to a file
void ClientData::outputData()
{
// Open output file
cout << "Writing output to file: " << outputfile << endl;
ofstream outfile(outputfile);
if (outfile.is_open())
{
for (int row=0; row < ROWS; row++)
{
outfile << client_rows[row] << " " << business_rows[row] << endl;
}
outfile.close();
}
else
{
cout << "Error opening output file";
}
};
// Perform bubble sort
void ClientData::bubbleSort(int option)
{
// Set up some needed local variables
int numTotalPasses = 0;
int numTotalSwaps = 0;
int endPosition; // last position to check for each pass
char temp[31]; // needed for swapping
char* temp_row;
bool swapDone = true;
// Set initial stopping array position to next-to-last element
endPosition = ROWS - 2;
while (swapDone)
{
// Do next pass
numTotalPasses++; // bump pass count
swapDone = false; // assume no swap needed this pass
// 1 = Client sort, 2 = Business sort
//cout << "option= " << option << endl;
if (option == 1)
{
for (int pos = 0; pos <= endPosition; pos++)
{
//cout << "client_rows[" << pos << "] = " << client_rows[pos] << endl;
//cout << "client_rows[" << pos + 1 << "] = " << client_rows[pos+1] << endl;
if (strcmp(client_rows[pos],client_rows[pos+1]) > 0)
{
// Swap these two elements in both arrays
//cout << "need to swap clients" << endl;
swapDone = true; // Yes, we're doing at least 1 swap this pass!
temp_row = &temp[0];
strcpy(temp_row,client_rows[pos]); // hold larger string
strcpy(client_rows[pos],client_rows[pos+1]); // move smaller to larger position
strcpy(client_rows[pos+1],temp_row); // move larger to old smaller position
strcpy(temp_row,business_rows[pos]); // hold larger string
strcpy(business_rows[pos],business_rows[pos+1]); // move smaller to larger position
strcpy(business_rows[pos+1],temp_row); // move larger to old smaller position
}
}
}
else if (option == 2)
{
for (int pos = 0; pos <= endPosition; pos++)
{
//cout << "business_rows[" << pos << "] = " << business_rows[pos] << endl;
//cout << "business_rows[" << pos + 1 << "] = " << business_rows[pos+1] << endl;
if (strcmp(business_rows[pos],business_rows[pos+1]) > 0)
{
// Swap these two elements in both arrays
//cout << "need to swap businesses" << endl;
swapDone = true; // Yes, we're doing at least 1 swap this pass!
temp_row = &temp[0];
strcpy(temp_row,business_rows[pos]); // hold larger string
strcpy(business_rows[pos],business_rows[pos+1]); // move smaller to larger position
strcpy(business_rows[pos+1],temp_row); // move larger to old smaller position
strcpy(temp_row,client_rows[pos]); // hold larger string
strcpy(client_rows[pos],client_rows[pos+1]); // move smaller to larger position
strcpy(client_rows[pos+1],temp_row); // move larger to old smaller position
}
}
}
else
{
cout << "Invalid sort option. Try again." << endl;
}
// One less position to go to for next pass
endPosition--;
} // end while
};
int main()
{
int option; // sort type
// Set up input and output files
char* infilename = "C:\CIS326\ClientData.txt";
char* outfilename = "C:\CIS326\SortedClientData.txt";
ClientData myData(infilename, outfilename);
// Input client and business data from a file
myData.inputData();
// Determine sort type and perform bubble sort on it
cout << "Enter 1 for Client sort or 2 for Business sort: ";
cin >> option;
myData.bubbleSort(option);
// Output initial data
myData.outputData();
// Keep the output window open before exiting
system("PAUSE");
return 0;
}
Explanation / Answer
In Java, every variable, constant, and function must be inside some class. Here's a simple example program:
Things to note:
Like the C++ << operator, these functions can be used to print values of any type. For example, all of the following are fine:
The + operator can be useful when printing. It is overloaded to work on Strings as follows:
If either operand is a String, it
Example
The only operator is +, so the expression is evaluated left-to-right (if there were another operator with higher precedence, the sub-expression involving that operator would be evaluated first). The leftmost sub-expression is:
Evaluation of the argument to println continues, producing as the final value the String shown above (note that means the newline character):
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.