Reading in a file and sorting alphabetically. need Help please!

So the assignment is to read in a file of names then sort it alphabetically. This is the code I have so far but it wont work. Can someone help me?

#include <iostream>
#include <string>
using namespace std;

int main(){
// String holding each name
string word;

// Temp value
string temp;

// Empty vector holding all names from file
vector<string> names;

// Read names from file LineUp.txt
ifstream in(Desktop/LineUp.txt);
if(in.is_good()){
while(in.good()){
getline(in, word);
names.push_back(word);
}
in.close();
}else{
cout << "Unable to open file";
}

int size = names.size();

int i;

// Loop to sort vector name values
for( i = 1 ; i < size ; ++i){
// new value to be inserted into temp location
temp = names.at(i);

// index to the left of i
int k;

// sort names
for(k = i-1; k>=0 && names.at(k) > temp; k--){
names.at(k+1) = names.at(k);
}
names.at(k+1) = temp;
}

// Loop to print names
for (i = 0; i < size; i++)
cout << names.at(i) << endl;

return 0;
}
Last edited on
Does the assignment insists on using that, particularly inefficient, approach to sorting? C++ has the sort() function

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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;

int main() {
    // Empty vector holding all names from file
    vector<string> names;

    // Read names from file LineUp.txt
    ifstream in("Desktop/LineUp.txt");
    if(!in.is_open())
        cout << "Unable to open file\n";

    // this is wrong, by the way: while(in.good()){
    string word;
    while(getline(in, word))
            names.push_back(word);

    sort(names.begin(), names.end());

    // Loop to print names
    for (size_t i = 0; i < names.size(); i++)
        cout << names[i] << '\n';
}


It also has automatically sorted containers, such as multiset
Last edited on
so I changed the code to this:


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;

int main(){
// String holding each name
std::string;

// Temp value
string temp;

// Empty vector holding all names from file
vector<string> names;

// Read names from file LineUp.txt
ifstream in("Desktop/LineUp.txt");
if(!in.is_open())
cout << "Unable to open file \n";

// Loop to sort vector name values
string word;
while(getline(in, word))
names.push_back(word);

sort(names.begin(), names.end());

// Loop to print names
for (size_t i=0; i < names.size(); i++)
cout << names[i] << '/n';

return 0;
}

its supposed to open the file, read the names and sort alphabetically but it says it cant open the file like it's supposed to...
Last edited on
Just out of curiosity, why is that, Cubbi?

Cubbi wrote:
// this is wrong, by the way: while(in.good())


Also, sorry for the hijack of the thread!


@Rebel, I know it may sound stupid, but are you sure the file is where you're trying to opening it from? What IDE are you running? Visual Studio... etc.
Last edited on
Well, the problem with this code:
1
2
3
4
while (in.good()) {
    getline(in, word);
    names.push_back(word);
}

... is that line 3 is executed regardless of whether or not the getline() was successful.
Just out of curiosity, why is that

because testing the completion status of an input operation *before* the input operation takes place is always wrong. In this case, the loop adds one more string to the vector.

it says it cant open the file

That means the file is not present where the program is looking for it (that is, the program's current working directory doesn't have a subdirectory Desktop, or that subdir doesn't have the file, or it is not readable). I tested with a file in the same directory as the executable, which I ran from console. As Lynx876 points out, Visual Studio in particular has special rules about the current working directories.
Last edited on
ahh I see! I've always used file.good(). Guess it's time to move on, aha!

Thanks!
Topic archived. No new replies allowed.