0

I am starting to learn c++ from java and because String in c++ is not data type stuff with switch is different to me.

This is my function in java

public static void fileCreationPo(Procesor pr,String name,String extension) {
    String filePath = "C:\\Created Files\\";
    String filePathFinal = filePath+name+extension;
    try {
        BufferedWriter bw= new BufferedWriter( new FileWriter(filePathFinal))
        switch(extension)
        {
        case ".txt":
            bw.write(pr.toStringN());
            break;
        case ".csv":
            bw.write(pr.toString());
            break;
        default:
        }
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And I was wondering in c++ what would be the most efficient way to make "fileNameFinal" relative to extension typed in parameters and working "switch" without creating two parameters for almost the same thing if you can understand what I mean from java code

This is my failed attempt using enum
Also if you see anything the code could improve on I'll be more than glad to see it

enum swEE {
txt = 0,
csv,
sri
};

void fileCreationPr(Predmet pr, string nz, swEE se) {
string fileName("J:\\Personal\\ukolos\\IT\\vis\\pec.txt");
string fileNameFinal(fileName + ???? );
ofstream outFs(fileNameFinal, ofstream::out);

switch (se) {
case txt:
    outFs << stringify(se) << pr.nazev << "\n" << pr.pocet << "\n" << pr.vaha;
    break;
case csv:
    outFs << pr.nazev << ";" << pr.pocet << ";" << pr.vaha;
    break;
}
outFs.close();
};
  • Which language, C++ or Java? In C++ you can't use strings with `case` labels. Use a lookup table instead. – Thomas Matthews Mar 24 '21 at 22:50
  • In C++, switch statements can only work with integrals. For anything else, you'd have to use cascading `if` -> `else if` statements. – SergeyA Mar 24 '21 at 22:50
  • @SergeyA: What about a lookup table? Can that be used? – Thomas Matthews Mar 24 '21 at 22:51
  • You could make a `constexpr` hashing function to create the integrals @SergeyA is talking about. It'd look like strings in the code, but would be compiled into integrals. – Ted Lyngmo Mar 24 '21 at 22:51
  • Learning C++ from Java can be a rough go because they look so similar and behave so differently. Definitely back yourself up with some [good reference materials](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if you haven't already. – user4581301 Mar 24 '21 at 22:52
  • @ThomasMatthews it certainly can, depending on what is ultimately required. For example, if you need to perform significantly different actions, using a lookup table would become syntactically cumbersome. It can also be more or less efficient based on runtime profile. – SergeyA Mar 24 '21 at 22:52
  • If you have a large number of possibilities a `map` or `unordered_map` mapping a string to a function that does do the dirty work can out-do the cascading `if`s. How large a list varies, but it's definitely more than 3. With three, `if`/`else if` is going to be the way to go. The mapping can be easier to write and maintain, though, so pick your poison. – user4581301 Mar 24 '21 at 23:06

0 Answers0