I have a small problem. I am trying to read in numbers from a text file and using Selection Sort with them. I have some code so far, but I don't know what to do now. I need some help. Where do I go from here in order to start my Selection Sort?
//Program that demonstrates how to use input and output files
//Libraries used in the program
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <conio.h>
using namespace std;
int main()
{
//Declaraton of Variable And Arrays
int Index = 0;
int NumData = 1;
vector<string> Array1(NumData);
vector<string> Array2(NumData);
//Specifies the Files that are used for Input and Output
ifstream InFile1("InData.txt");
//Displays error messageif File can't be opened
if (InFile1.fail())
{
cout << "File could not be opened";
return(0);
}
//Loads Array from Input File - READS ENTIRE LINE
while (getline(InFile1,Array1[Array1.size()-1]))
{
Array1.resize(Array1.size()+1);
}
InFile1.close();
//Removes extra space at the end
Array1.resize(Array1.size()-1);
//**********************************************************************
//Specifies the Files that are used for Input and Output
ifstream InFile2("InData.txt");
//Displays error messageif File can't be opened
if (InFile2.fail())
{
cout << "Fle could not be opened";
return(0);
}
//Loads Array from Input File - READS ENTIRE LINE
while (InFile2 >>Array2[Array2.size()-1])
{
Array2.resize(Array2.size()+1);
}
InFile2.close();
//Removes extra space at the end
Array2.resize(Array2.size()-1);
//Displays array and outputs it to outfile
ofstream OutFile("InData.txt");
cout << "Array: ";
for (Index = 0; Index < Array2.size(); Index++)
{
OutFile << Array2[Index] << endl;
cout << Array2[Index] << endl;
}
return(0);
}
Question
C++ Guy
I have a small problem. I am trying to read in numbers from a text file and using Selection Sort with them. I have some code so far, but I don't know what to do now. I need some help. Where do I go from here in order to start my Selection Sort?
//Program that demonstrates how to use input and output files //Libraries used in the program #include <iostream> #include <fstream> #include <vector> #include <string> #include <conio.h> using namespace std; int main() { //Declaraton of Variable And Arrays int Index = 0; int NumData = 1; vector<string> Array1(NumData); vector<string> Array2(NumData); //Specifies the Files that are used for Input and Output ifstream InFile1("InData.txt"); //Displays error messageif File can't be opened if (InFile1.fail()) { cout << "File could not be opened"; return(0); } //Loads Array from Input File - READS ENTIRE LINE while (getline(InFile1,Array1[Array1.size()-1])) { Array1.resize(Array1.size()+1); } InFile1.close(); //Removes extra space at the end Array1.resize(Array1.size()-1); //********************************************************************** //Specifies the Files that are used for Input and Output ifstream InFile2("InData.txt"); //Displays error messageif File can't be opened if (InFile2.fail()) { cout << "Fle could not be opened"; return(0); } //Loads Array from Input File - READS ENTIRE LINE while (InFile2 >>Array2[Array2.size()-1]) { Array2.resize(Array2.size()+1); } InFile2.close(); //Removes extra space at the end Array2.resize(Array2.size()-1); //Displays array and outputs it to outfile ofstream OutFile("InData.txt"); cout << "Array: "; for (Index = 0; Index < Array2.size(); Index++) { OutFile << Array2[Index] << endl; cout << Array2[Index] << endl; } return(0); }Edited by C++ GuyLink to comment
Share on other sites
1 answer to this question
Recommended Posts