Feb 26, 2013 at 5:40pm UTC
this is my code:
#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("C:\Users\Keon\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;
}
it's supposed to read in a file of names then sort them alphabetically. It says it cant read in the file of names. Can anyone help me with this?
Last edited on Feb 26, 2013 at 5:43pm UTC
Feb 26, 2013 at 5:42pm UTC
Are you running the program from your user directory? It's probably looking for the Desktop folder in the same folder it's being run from.
Also,
1 2 3
int main(){
// String holding each name
std::string; //this line doesn't do anything, it's pointless
Last edited on Feb 26, 2013 at 5:43pm UTC
Feb 26, 2013 at 5:44pm UTC
yeah I just realized that the location of the folder was off and fixed it but it still won't do what it's supposed to...It doesnt sort the names like it should.
Feb 26, 2013 at 5:46pm UTC
Well for one thing you're printing a new line with /n instead of \n
What output are you getting? What is the original list of names?
Feb 26, 2013 at 5:52pm UTC
output says "unable to open file." its only open for a split second then it closes.
and the list of names is:
Cooper
Gavin
Joseph
Sierra
Kacie
Dylan
Kaylee
Will
Cameron
Kieron
Samuel
Alexis
Eryn
Emily
McKenna
Brandon
Thomas
Luke
Carlee
Matthew
Elizabeth
Danny
Rachel
Haley
Colleen
Brian
Trey
Noah
Rena
Gillan
Caroline
Cassidy
Kevin
Jason
Zach
Hannah
Dalton
Ian
Sarah
Brandy
Brittany
Jessica
Mia
Victoria
Mark
Michael
Feb 26, 2013 at 9:37pm UTC
You need to use forward slashes in the path, not back-slashes - the compiler thinks those are escape sequences like \n
Last edited on Feb 26, 2013 at 9:37pm UTC
Feb 27, 2013 at 2:33am UTC
Or escape the back slashes with backslashes.
Double backslashes.
Feb 27, 2013 at 5:07pm UTC
It's far easier to just use forward slashes for file paths...