Search code examples
c++console

How do I separate a name of something with spaces and numbers from a number in a single line of a file?


I have a file, it looks like this:

Car-in-blue 4
Truck 100 5
Jeep-43 434 6
Machine 123 5

The last digits of a line is an integer number that should be inserted into an int variable. The names containing hyphens, spaces, and numbers should be inserted into a string variable.

It should be like this:

int a = 4
string name = "Car-in-blue"
int a = 5
string name = "Truck 100"
int a = 6
string name = "Jeep-43 434"
int a = 5
string name = "Machine 123"

I added a screenshot of the original problem, it's not in English, but that doesn't matter, that's how it looks in the problem:

Screenshot

Text version:

Cap-de-bour 4
Romania 100 10
Cap-de-bour 7
Cap-de-bour 4
Romania 100 5
Romania 100 5
Romania 100 5
CRACIUN 2018 15
Romania 100 10

Solution

  • Not the safest code but detailed and clear:

    The most convenient way to do this for me (that would only work for this example) is to put these things into a string, and then sort through the string in reverse.

    I don't know how you're getting the information to begin with, but if you need to grab it from a text file first do this code:

    #include <fstream>
    #include <vector>
    #include <string>
    #include <iostream> //not sure if needed but I always include it
    
    int main()
    {
        std::vector<string> lines;
        std::string temp;
        ifstream is("FILENAME.txt");
        while (!is.eof()) //make sure it's not the end of the file (eof)
        {
            std::getline(is, temp); //get a line and toss it into "temp".
            lines.push_back(temp);//create new entry in our vector.
        }
    }
    

    Next to actually iterate:

    #include <vector>
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    
        vector<string> lines;
        string temp_string;
        ifstream is("FILENAME.txt");
        while (!is.eof()) //make sure it's not the end of the file (eof)
        {
            getline(is, temp_string); //get a line and toss it into "temp".
            lines.push_back(temp_string);//create new entry in our vector.
        }
    
    
        vector<string> model;
        vector<int> number;
        string line, temp_model;
        int temp_number;
        for (int i = 0; i < lines.size(); i++)
        {
            line = lines[i];
            temp_string = "";//reset string
            temp_number = 0;//reset integer
            for (int x = line.size() - 1; x >= 0; x--) //reverse iteration
            {
                if (line[x] == ' ')
                {
                    //x now is the breaking point between the mode name and the number. use that to get model name now:
                    temp_model = "";//reset string
                    for (int c = 0; c < x/*to make surewe don't pass the stopping point*/; c++)
                    {
                        temp_model += line[c];
                    }
    
                    break;//end loop
                }
                //if still for some reason need to check if it's a number, use std::isdigit()
                temp_string += line[x]; //adds the current character to our temporary string
            }
    
    
            //Reverse the string so that it's in proper order:
            for (int x = temp_string.size() - 1; x >= 0; x--);
            {
                //mini algorithm I came up with to store the number
                temp_number = 10 * temp_number;
                temp_number += stoi(temp_string); //attempt to convert string to integer (s to i)
            }
            number.push_back(temp_number);//store entry
            model.push_back(temp_model);//store entry
        }
    
        //output results:
        for (int i = 0; i < model.size(); i++)
        {
            cout << "Model: " << model[i] << endl;
            cout << "Number: " << number[i] << endl;
        }
    }
    
    

    Output: output