dimanche 19 avril 2020

Stopping getLine() immediately after text is finished

So, instead of having the vector left at a size of 4, which is how many elements will be in it from the sample data, I want to increase this to 10 (or however many for my other files), which leaves the flexibility to add more data to the collection in the future. If I want to increase it, one set of blank customer fields is displayed (shown below.) With another file, my stoi function receives invalid input due to what I'm assuming is the getLine scanning an extra line and passing nothing to it causing an error (also shown below.). I was wondering, is there a way to stop the line-scanning immediately so it doesn't even read past the last piece of text? All the files are formatted similarly, with colons separating each bit of data till the full stop to end the object.)

P.S. Would anyone have any idea why the customer name is formatting like that? I can't really see why the name skips to the next line.

enter image description here

enter image description here enter image description here

  while (customerFile.good()) {

    std::getline(customerFile, str, ':' );
    custTemp.customerName = str;

    std::getline(customerFile, str, ':' );
    custTemp.projectName = str;

    if (custTemp.customerName == "" || custTemp.projectName == "") {
        cerr << "The customer/project name must not be empty." << endl;
        abort();
    }

    std::getline(customerFile, str, '.' );
    custTemp.listOfParts = str;

    if (custTemp.listOfParts.size() > 10) {
        cerr << "There cannot be more than 10 parts" << endl;
        abort();
    }

    cout << "Customer name: " << custTemp.customerName << endl;
    cout << "Project name: " << custTemp.projectName << endl;
    cout << "List of parts: " << custTemp.listOfParts << endl;
    cout << " " << endl;

    customers.push_back(custTemp);

    if (customers.size() == 4 || str.empty()) {
        break;
    }
}

Aucun commentaire:

Enregistrer un commentaire