I'm not quite sure what is wrong with why the text file I put is not showing up in the console. I don't have to have them in ascending order, but at the very least I would like the list to show up.
#include<iostream>
#include<set>
#include <fstream>
#include<string>
class info {
public:
void ascending() {
std::string file;
std::set<std::string> list;
std::ifstream input(file.c_str());
std::string words;
do {
list.insert(words);
} while (input >> words);
}
};
int main() {
std::set<std::string> list;
std::string file;
info text;
std::cout << "Please enter the name the file that you would like to have read and \n placed in ascending order (duplicates will not appear):" << std::endl;
std::cin >> file;
//void ascending();
std::ifstream input(file.c_str());
if (input.is_open()) {
std::cout << "Here is your list of words in ascending order with no duplicates:" << std::endl;
text.ascending();
std::set<std::string>::iterator i = list.begin();
do {
i++;
std::cout << *i << std::endl;
} while (input.is_open() && i != list.end());
input.close();
}
else
{
do {
std::cout << "Please enter a valid filename." << std::endl;
std::cin >> file;
} while (input.fail());
}
return 0;
}
List example, though any txt file can be used.
[code]
burn
juice
secretary
ball
spiffy
When I enter anything into my if branch it does say the “here is your list...” section, but nothing shows up. i’m checking on the other sections for the empty file part because that would explain why nothing is happening.
I was able to get the list to display in ascending order, but for some reason debug assertion failed is still popping up. I removed the infinite loop and put everything into the function and then called the function in main. I’m going to look it up to try to figure out what that means. Thank you so much! At least it is recognizing the file now. =)
Ignoring the potential issues with reading from a file.....output using a ranged based for loop does work as expected showing the sorted elements in the set:
If you're still getting some sort of runtime debug assert error, then show your new code, and when you click on "Debug" from the pop-up, what line of code does it bring you to?
Edit: You have an "off-by-one" error in your loop, which is perhaps what FurryGuy is alluding to.
1 2
i++;
std::cout << *i << std::endl;
This increments the iterator, and THEN dereferences it. So i could be the last valid element, and then it's incremented and points to list.end(), which should not be dereferenced.
A do...while loop is also not the best choice of iterating through a C++ container. What happens if the container is empty? You have no check for that condition until after your first loop through.
You can use a for loop using iterators (using cbegin/cend instead of begin/end creates a constant iterator, one that won't allow alteration of the elements):
#include<iostream>
#include<set>
#include <fstream>
#include<string>
class info {
public:
void ascending() {
std::string fi;
std::set<std::string> li;
std::cin >> fi;
std::ifstream input(fi.c_str());
std::string words;
do {
li.insert(words);
} while (input >> words);
if (input.is_open()) {
std::cout << "Here is your list of words in ascending order with no duplicates:" << std::endl;
std::set<std::string>::iterator i = li.begin();
do {
i++;
std::cout << *i << std::endl;
} while (input.is_open() && i != li.end());
input.close();
}
else
{/*
do {
std::cout << "Please enter a valid filename." << std::endl;
std::cin >> fi;
} while (input.fail());*/
}
}
};
int main() {
std::set<std::string> list;
std::string file;
info text;
std::cout << "Please enter the name the file that you would like to have read and \n placed in ascending order (duplicates will not appear):" << std::endl;
//std::cin >> file;
text.ascending();
//std::ifstream input(file.c_str());
return 0;
}
I haven't made complete changes to the do while yet, but i did comment out a portion to test it. I plan on changing it for for based on what Furry Guy said.