Outputing

SO lets say I have a file that says

10 tennent
9 Eccleston
12 Capaldi
11 Smith

How do I get the number's in order but not changing the name it goes with?
SO here is how I started it

# include <iostream>
# include <fstream>
# include <sstream>
# include <set>
# include <list>
# include <string>
# include <cctype>
# include <vector>

using namespace std;

int main()
{
fstream ifile("who.txt");
if (!ifile.good())
{
cout<<" Can't open the file"<<endl;
}
string header;
getline (ifile, header);
cout<<header<<endl;


string line;
string term;
while (getline (ifile,line))
{

istringstream iss(line);
string p;
iss >> p;

vector<string> terms;
string m;
string value;

for (vector<string>::const_iterator iter = terms.begin(); iter != terms.end(); ++iter)
{
cout << *iter<< ", ";
}
cout << endl;
}
ifile.close();
return 0;
}

Not sure if I started it the right way. Plz help
closed account (18hRX9L8)
std::pair and std::map

http://www.cplusplus.com/reference/utility/pair/pair/
http://www.cplusplus.com/reference/map/map/
could u be more specific? Maybe give me a pseudocode ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#include <string>
#include <utility> // std::pair
#include <sstream> // #include <fstream>
#include <algorithm>
#include <iterator> // std::begin(), std::end()

int main()
{
    std::istringstream file // std::ifstream file("who.txt");
    ( "10 Tennant\n" "9 Eccleston\n" "12 Capaldi\n" "11 Smith\n" );

    // vector holding pairs int+string
    // http://en.cppreference.com/w/cpp/utility/pair
    std::vector< std::pair<int,std::string> > seq ;

    int number ;
    std::string text ;

    // for each line in the file
    while( file >> number && std::getline(file,text) ) // read the number and the string
        seq.emplace_back(number,text) ; // add the pair to vector
        // http://en.cppreference.com/w/cpp/container/vector/emplace_back

    std::sort( std::begin(seq), std::end(seq) ) ; // sort the pairs
    // http://en.cppreference.com/w/cpp/utility/pair/operator_cmp
    // http://en.cppreference.com/w/cpp/iterator/begin

    // and print out the sorted pairs
    // pair.first is the number, pair.second is the text
    for( auto pair : seq ) std::cout << pair.first << ' ' << pair.second << '\n' ;
}

http://coliru.stacked-crooked.com/a/f86e507b39a243b5
i got error for the seq.emplace
and auto pair:seq

PLz help
Use a data structure and a vector. Look the rest up on your own, this is basic.

Also, I tend to prefer to write my own sorting algorithms, that way, I can determine how the sort if performed on things like data structures.
Last edited on
what do u mean? Kind of new to this
> i got error for the seq.emplace
> and auto pair:seq

Needs C++11.
This is the legacy C++ version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <string>
#include <utility> // std::pair
#include <sstream> // #include <fstream>
#include <algorithm>
#include <iterator> // std::begin(), std::end()

int main()
{
    std::istringstream file // std::ifstream file("who.txt");
    ( "10 Tennant\n" "9 Eccleston\n" "12 Capaldi\n" "11 Smith\n" );

    // vector holding pairs int+string
    // http://en.cppreference.com/w/cpp/utility/pair
    std::vector< std::pair<int,std::string> > seq ;

    int number ;
    std::string text ;

    // for each line in the file
    while( file >> number && std::getline(file,text) ) // read the number and the string
        seq.push_back( std::make_pair(number,text) ) ; // add the pair to vector
        // http://en.cppreference.com/w/cpp/utility/pair/make_pair
        // http://en.cppreference.com/w/cpp/container/vector/push_back

    std::sort( seq.begin(), seq.end() ) ; // sort the pairs
    // http://en.cppreference.com/w/cpp/utility/pair/operator_cmp

    // and print out the sorted pairs
    // seq[i].first is the number, seq[i].second is the text
    for( std::size_t i = 0 ; i < seq.size() ; ++i )
        std::cout << seq[i].first << ' ' << seq[i].second << '\n' ;
}

http://coliru.stacked-crooked.com/a/46d5a990a1657f1b
move your topic to the beginner's forum.

look up data structures.
I am still not getting the output in numerical order
Please, create a class or something like that!
I am still not getting the output in numerical order


Okay, what have you tried? Another thing, I think you should learn what a vector is and all the features of it before using them.
http://www.cplusplus.com/reference/vector/vector/
There it is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream> //cout - main
#include <string> //string - main ,class and func
#include <algorithm> //reverse, sort, greater - class
#include <vector> //vector - main and func
#include <sstream> //stringstream - main

using namespace std;
class Person
{
    private:
        string name_;
        unsigned num_;
    public:
        Person(string raw)
        {
            this->num_ = stoi(raw);
            reverse(raw.begin(), raw.end()); //Those "reverse" are for getting only the number and the string
            raw = raw.substr(0, raw.find(' '));
            reverse(raw.begin(), raw.end()); //Ends here :P
            this->name_ = raw;
        }
        string name() {return this->name_;}
        unsigned num() {return this->num_;}
};

vector<Person> order_vector(vector<Person>& rawPerson)
{
        //Ok, lets check which one is the first
    vector<Person> ret;
    vector<unsigned> values;
    for(unsigned i = 0; i < rawPerson.size(); i++) values.push_back(rawPerson[i].num());
    sort(values.begin(), values.end(), greater<unsigned>());
    for(unsigned a = 0; a < rawPerson.size(); a++)
    for(unsigned i = 0; i < rawPerson.size(); i++)
    {
        if(values[a] == rawPerson[i].num()) ret.push_back(rawPerson[i]);
    }
    return ret;
}

int main()
{
    string raw_strings;
    vector<Person> rawPerson;
    vector<Person> ordened_Person;
    stringstream rawstring(string("10 tennent\n9 Eccleston\n12 Capaldi\n11 Smith"));
    while(getline(rawstring, raw_strings)) //get info before '\n'
    {
        if(!raw_strings.empty()) //If it is not an '\n'
        {
            rawPerson.push_back(Person(raw_strings)); // add an object
        }
    }
    ordened_Person = order_vector(rawPerson);
    for(auto& a : ordened_Person) std::cout << a.num() << '\t' << a.name() << std::endl;
}

Or even better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream> //cout - main
#include <string> //string - main ,class 
#include <algorithm> //reverse, sort, greater - class and main
#include <vector> //vector - main
#include <sstream> //stringstream - main

using namespace std;
class Person
{
    private:
        string name_;
        unsigned num_;
    public:
        Person(string raw)
        {
            this->num_ = stoi(raw);
            reverse(raw.begin(), raw.end());
            raw = raw.substr(0, raw.find(' '));
            reverse(raw.begin(), raw.end());
            this->name_ = raw;
        }
        string name() {return this->name_;}
        unsigned num() {return this->num_;}
        bool operator<(const Person& person) const
        {
            return num_ < person.num_;
        } //No order_vector
};

int main()
{
    string raw_strings;
    vector<Person> ordened_Person; //No raw_person
    stringstream rawstring(string("10 tennent\n9 Eccleston\n12 Capaldi\n11 Smith"));
    while(getline(rawstring, raw_strings)) //get info before '\n'
    {
        if(!raw_strings.empty()) //If it is not an '\n'
        {
            ordened_Person.push_back(Person(raw_strings)); // add an object
        }
    }
    sort(ordened_Person.begin(), ordened_Person.end());
    for(auto& a : ordened_Person) std::cout << a.num() << '\t' << a.name() << std::endl;
}


PS: C++ 2011
Oh, if you want it descending, after sort(ordened_Person.begin(), ordened_Person.end());, write reverse(ordened_Person.begin(), ordened_Person.end());.
Compile it with
g++ -std=c++11 main.cpp -o programname


Bye ;)
Last edited on
Topic archived. No new replies allowed.