#include <fstream>
#include <string>
#include <iostream>
usingnamespace std;
int main()
{
string line;
ifstream in("letter_number.txt"); // the file that the data is read out off
if( !in.is_open())
{
cout << "Input file failed to open\n";
return 1;
}
while( getline(in,line) ) // it reads line by line
{
cout<< line[0] << " "; // prints the letter
for (int pos = 2; pos <= line.length(); ++pos) // prints the numbers
{
cout << line[pos];
}
cout << "\n";
}
in.close();
string end;
cin >> end;
return 0;
}
now i would need some code that saves it into array or something, sorts the data by the number but the corresponding letter has to be left next number. Then the program needs to print the letter corresponding to the biggest number. If the letter was printed before it should print the second biggest and so on..
The simplest way is to use standard container std::vector for std::pair<char, int>
for example
1 2 3 4 5 6 7 8 9 10 11
std::vector<std::pair<char, int> > v;
while ( std::getline( in, line ) )
{
std::istringstream is( line );
std::pair<char, int> p;
is >> p.first >> p.second;
v.push_back( p );
}
When you can sort your vector as needed.
Or another variant is to use container std::multimap<int, char> It will be sorted automatically when you add records to it.
You could store these in a vector. If you want to keep them togeather, stick them into a structure, or better yet, a pair. You can use the pre-defined sort algorithm from STL to put everything in order according the the number.
I think there is something wrong with my compPair function, but I have to leave you with something right?
#include <utility> // for std::pair
#include <vector> // for std::vector
#include <sstream> // for std::stringstream and std::string
#include <fstream> // for std::ifstream
#include <iostream> // for std::cout and std::endl
#include <algorithm> // for std::sort()
usingnamespace std;
//Function for the sort algorithm
struct compPair {
booloperator()(const pair<char,int> &i, const pair<char,int> &j) { return i.second > j.second; }
};
int main()
{
vector<pair<char, int> > MyList;
vector<pair<char, int> >::iterator it;
vector<pair<char, int> >::iterator jt;
//Read in stuff
string line;
ifstream in("letter_number.txt");
if( !in.is_open())
{
cout << "Input file failed to open\n";
return 1;
}
while ( getline(in, line)) // Read line by line
{
stringstream iss(line); //stick the line into a stringstream
pair<char, int> temp;
iss >> temp.first >> temp.second; //divide the line into seperate objects
MyList.push_back(temp); //Add it to the vector
}
in.close(); // close the file (this is where your stuff ends)
//sort the list
sort(MyList.begin(), MyList.end(), compPair);
//if the letter existed before, delete it
for (it = MyList.begin()+1; it < MyList.end(); it++)
for (jt = MyList.begin(); jt < it; jt++)
if (jt->first == it->first)
MyList.erase(it);
//print out the letters in order
for (it = MyList.begin(); it < MyList.end(); it++)
cout << it->first;
}
This is compiled snip of code. I only changed from your input stream ifstream in("letter_number.txt"); to std::cin for testing. Try to use this snip code as a template for your program.
devilsk13, we are not here to solve your homework problems for you. If you don't want to do your research, you're not going to learn anything, and you're going to end up getting bad grades later on, or worse, causing more heartache for the people that you have to work with. At least make an attempt at creating your own code with all of the information that you've been given, and then maybe we can help you debug it.