#Level of expertise in C++

19 messages ยท Page 1 of 1 (latest)

hearty crow
#

What level of expertise would you guys rate the following code in C++?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

// Save a vector of names to a file
void saveNames(const std::vector<std::string> &names, const std::string &filename) {
    std::ofstream outFile(filename);
    if (!outFile) {
        std::cout << "Error opening " << filename << " for writing!" << std::endl;
        return;
    }

    for (const std::string &name: names) {
        outFile << name << std::endl; // Each name on a new line
    }
    outFile.close();
    std::cout << "Names saved to " << filename << std::endl;
}

// Load names from a file into a vector
std::vector<std::string> loadNames(const std::string &filename) {
    std::vector<std::string> names;
    std::ifstream inFile(filename);

    if (!inFile) {
        std::cout << "Error opening " << filename << " for reading!" << std::endl;
        return names; // Return empty vector on failure
    }

    std::string name;
    while (std::getline(inFile, name)) {
        if (!name.empty()) { // Skip empty lines
            names.push_back(name);
        }
    }
    inFile.close();
    return names;
}

int main() {
    std::vector<std::string> names;
    std::string input;

    // Collect names from user
    std::cout << "Enter names (type 'done' to stop): ";
    std::getline(std::cin, input);
    while (input != "done") {
        names.push_back(input);
        std::cout << "Next name (or 'done'): ";
        std::getline(std::cin, input);
    }

    // Save names to file
    saveNames(names, "names.txt");

    // Load names from file and print
    std::vector<std::string> loadedNames = loadNames("names.txt");
    if (loadedNames.empty()) {
        std::cout << "No names loaded." << std::endl;
    } else {
        std::cout << "Loaded names:" << std::endl;
        for (const std::string &name: loadedNames) {
            std::cout << "- " << name << std::endl;
        }
    }

    return 0;
}
delicate arrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

knotty walrus
#

std::endl

#

noob level

hearty crow
knotty walrus
#

'\n'

hearty crow
knotty walrus
#

yes, why would you flush

hearty crow
torn dagger
#

there's a performance hit especially inside big loops

woven crater
#

This will blow up on unicode symbols in paths on Windows

steel salmon
# hearty crow What level of expertise would you guys rate the following code in C++? ```cpp #...

const std::string &filename
Should probably just be a std::string_view.

Your code in general could profit from using auto in a few places.

Why does it have to be a std::vector<std::string> in the saveNames function? Why can't it be any iterable container, containing some datatype that can be converted to a string?

Why does it have to be a std::vector<std::string> return type in loadNames? Why can't it be a std::vector<T> return type?
To be honest I would say this point is the least relevant, because one could argue that it would put too much responsibility into a single function, to also perform the conversion.

Also your loadNames function suffers from the fact that if the names are not perfectly distributed (i.e. each name on its own line), then your whole code doesn't work, but you document that nowhere.

There should be no actual code in your main function, other than gluing together function calls.
You should probably not call std::getline nor std::cout from your main. Move all functionality into functions other than main, like e.g. asking for user input.
Btw, while we're at getting user input: A do-while would be more appropriate here.

#

Other than that there's really not much to say about the code.
I mean, it is incredibly basic, probably even ChatGPT could write something similar.

hearty crow
woven crater
#

You really should fix the bugs (unicode handling) before thinking about the code style

hearty crow
woven crater
#

First make sure you can reproduce the problem ๐Ÿ™‚

#

It should throw an exception, I believe