Looping through lines of floats/int I'm new to programming and started with some tutorials about C++ on YouTube. Now I was wondering how I should continue solving below. It does work but already it has been like a lot of code to write. And I have more linetypes to define. So hope someone can point out a better way

std::string filepath = "path/to/file.ext";
std::ifstream stream(filepath);
std::string line;
int i = 1; //For debug purpose
while (getline(stream, line))
{
    if (line!= "" &amp;&amp; line.at(0) == '0')
    {
        std::cout &lt;&lt; "Line "&lt;&lt; i &lt;&lt; " type 0: " &lt;&lt; line &lt;&lt; std::endl;
    } 
    else if (line != "" &amp;&amp; line.at(0) == '1')
    {
        std::cout &lt;&lt;  "Line " &lt;&lt; i &lt;&lt; " type 1: " &lt;&lt; line &lt;&lt; std::endl;
    }
    else if (line != "" &amp;&amp; line.at(0) == '2')
    {
        std::cout &lt;&lt; "Line " &lt;&lt; i &lt;&lt; " type 2: " &lt;&lt; line &lt;&lt; std::endl;
    }
    else if (line != "" &amp;&amp; line.at(0) == '3')
    {
        std::cout &lt;&lt; "Line " &lt;&lt; i &lt;&lt; " type 3: " &lt;&lt; line &lt;&lt; std::endl;
    }
    else if (line != "" &amp;&amp; line.at(0) == '4')
    {
        //Example string: 4 16 -40 0 -20 -40 24 -20 40 24 -20 40 0 -20
        std::cout &lt;&lt; "Line " &lt;&lt; i &lt;&lt; " type 4: " &lt;&lt; line &lt;&lt; std::endl;
        std::stringstream iss{ LDrawLine };

        float lineType{}, color{}, x1{}, y1{}, z1{}, x2{}, y2{}, z2{}, x3{}, y3{}, z3{}, x4{}, y4{}, z4{};
        iss &gt;&gt; lineType &gt;&gt; color &gt;&gt; x1 &gt;&gt; y1 &gt;&gt; z1 &gt;&gt; x2 &gt;&gt; y2 &gt;&gt; z2 &gt;&gt; x3 &gt;&gt; y3 &gt;&gt; z3 &gt;&gt; x4 &gt;&gt; y4 &gt;&gt; z4;
        //What I could do is make a line like below for every coordinate. The color and the lineType need to be handled different
        //The positions is passed in to the function. Afterwords i need to draw the quads.
        (*positions).push_back(  0.0f); //so else i need to write this code 16 times for this linetype and still have some other line types to go
    }
    else if (line != "" &amp;&amp; line.at(0) == '5')
    {
        std::cout &lt;&lt; "Line " &lt;&lt; i &lt;&lt; " type 5: " &lt;&lt; line &lt;&lt; std::endl;
    }
    else 
    {
        std::cout &lt;&lt; "Line " &lt;&lt; i &lt;&lt; " No type" &lt;&lt; std::endl;
    }
    i++;
}


If anyone could give me some advice on how to handle this or a tutorial which can help me on this. The code does what it needs to do. I only want it more elegant and why less to type.
Break some of the functionality into functions. For instance, notice that all line types except for 4 behave similarly. So how about you do:

bool isSimpleType(char c) {
    return (c &gt;= '0' &amp;&amp; c &lt;= '3') || (c == '5');
}


With this, you can now simplify your main loop:

for(int i = 1; std::getline(stream, line); ++i)
{
    if (line == "")
        continue;

    const char type = line.front();

    if (isSimpleType(type)) {
        std::cout &lt;&lt; "Line " &lt;&lt; i &lt;&lt; " type " &lt;&lt; type &lt;&lt; ": " &lt;&lt; line &lt;&lt; "\n";
    }
    else if (type == '4') {
        // Omitted to reduce clutter
    }
    else {
        std::cout &lt;&lt; "Line " &lt;&lt; i &lt;&lt; " No type" &lt;&lt; std::endl;
    }
}


Finally, let us proceed along similar lines to simplify the case of '4'. I'll let you take over from here, but read the inputs to a vector and pass that onto a function that is responsible for verifying if the line is OK and constructing a suitable object from the data then.

Also, instead of relying on "naked values", you could also consider using a class enum Type { ... } to define the different line types. I recommend you read more about enums for this.