C++ Please help me with this sorting problem. If more info is needed please comm
ID: 3872820 • Letter: C
Question
C++ Please help me with this sorting problem. If more info is needed please comment and let me know. The sorting portion is commented out.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include<cstring>
#include <string.h>
#include <sstream>
#include <cstdlib>
using namespace std;
int main() {
//decalre variabe
string input;
string line;
//declare array to store int col values
int col1[1000];
//declare arrays to store the string column values
string col2[1000], col3[1000], col4[1000];
int lineno = 0;
cout << "Enter in your text file name or type 'done' to end the program:" << endl;
cin >> input;
//read inpupt unitll user input done
while (strcmp(input.c_str(), "done") != 0)
{
//Read file
ifstream myfile(input.c_str());
if (myfile.is_open())
{
//lineno variabel to add row to index lineno in the array
lineno = 0;
while (getline(myfile, line))
{
//split the line and read into variable below
istringstream ss(line);
int c1;
string c2, c3, c4;
ss >> c1 >> c2 >> c3 >> c4;
//store the values in the array
col1[lineno] = c1;
col2[lineno] = c2;
col3[lineno] = c3;
col4[lineno] = c4;
lineno += 1;
/*//FixMe Sorting
for (i = 0; i<lineno - 1; i++)
{
for (j = i + 1; j < lineno; j++)
{
//sorting the different cards
if (col1[j]>col1[j + 1])
{
temp1 = col1[j];
col1[j] = col2[j + 1];
col1[j + 1] = temp1;
if (strcmp(col2[j - 1], col2[j]) < 0)
strcpy(temp2, col2[j]);
strcpy(col2[j], col2[j + 1]);
strcpy(col2[j + 1], temp2);
strcpy(temp3, col3[j]);
strcpy(col3[j], col3[j + 1]);
strcpy(col3[j + 1], temp3);
strcpy(temp4, col4[j]);
strcpy(col4[j], col4[j + 1]);
strcpy(col4[j + 1], temp4);
}//if
}//for j
}//for i
}
*/
myfile.close();
//printing 3 groups in each line
for (int i = 0; i<lineno; i++)
{
//printing three elements of a group
for (int k = 1; k <= 3; k++)
{
cout << col1[i] << " " << col2[i] << " " << col3[i] << " " << col4[i] << " . ";
i++;
}
cout << " ";
}//for
}
else
cout << "Unable to open file";
cout << "Enter in your text file name or type 'done' to end the program:" << endl;
cin >> input;
}
}
Explanation / Answer
There are some small correction on the code.i am solved that please find below the code and execute your self
#include <iostream>
#include <fstream>
#include <stdio.h>
#include<cstring>
#include <string.h>
#include <sstream>
#include <cstdlib>
using namespace std;
int main() {
//decalre variabe
string input;
string line;
//declare array to store int col values
int col1[1000];
//declare arrays to store the string column values
string col2[1000], col3[1000], col4[1000];
int lineno = 0;
cout << "Enter in your text file name or type 'done' to end the program:" << endl;
cin >> input;
//read inpupt unitll user input done
while (strcmp(input.c_str(), "done") != 0)
{
//Read file
ifstream myfile(input.c_str());
if (myfile.is_open())
{
//lineno variabel to add row to index lineno in the array
lineno = 0;
while (getline(myfile, line))
{
//split the line and read into variable below
istringstream ss(line);
int c1;
string c2, c3, c4;
ss >> c1 >> c2 >> c3 >> c4;
//store the values in the array
col1[lineno] = c1;
col2[lineno] = c2;
col3[lineno] = c3;
col4[lineno] = c4;
lineno += 1;
/*//FixMe Sorting
for (i = 0; i<lineno - 1; i++)
{
for (j = i + 1; j < lineno; j++)
{
//sorting the different cards
if (col1[j]>col1[j + 1])
{
temp1 = col1[j];
col1[j] = col2[j + 1];
col1[j + 1] = temp1;
if (strcmp(col2[j - 1], col2[j]) < 0)
strcpy(temp2, col2[j]);
strcpy(col2[j], col2[j + 1]);
strcpy(col2[j + 1], temp2);
strcpy(temp3, col3[j]);
strcpy(col3[j], col3[j + 1]);
strcpy(col3[j + 1], temp3);
strcpy(temp4, col4[j]);
strcpy(col4[j], col4[j + 1]);
strcpy(col4[j + 1], temp4);
}//if
}//for j
}//for i
}
*/
myfile.close();
//printing 3 groups in each line
for (int i = 0; i<lineno; i++)
{
//printing three elements of a group
for (int k = 1; k <= 3; k++)
{
cout << col1[i] << " " << col2[i] << " " << col3[i] << " " << col4[i] << " . ";
i++;
}
cout << " ";
}//for
}}
else{
cout << "Unable to open file";
cout << "Enter in your text file name or type 'done' to end the program:" << endl;
cin >> input;
}}}
Thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.