简体   繁体   中英

Is there a way to read one specific row of info from a .txt file?

so im new to c++ and i have a project that i am currently doing in school however im stuck on how to get an entire row of info from a user input selection.

here is what my txt file looks like

1 Home Work 5
2 Work Home 5
3 Home School 6
4 School Home 6
5 Work School 8
6 School Work 8

so basically if they input/ cin option 3 but how do i print and obtain the values in row 3 to display and compute later on?

heres my code

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <cstdlib>
#include <Windows.h>
#include <cmath>

void bookingmenu();
void confirmedbooking(double calc);

double calc;
using namespace std;

int main()
{

    ifstream inFile;
    string PUP, Dropoff, others, otherstwo;
    double distance = 0, calc;
    string ON;
    int sel;
    char choice;

    inFile.open("blist.txt");

    if (!inFile)
        cout << "Error, unable to open text file.\n" << endl;

    else
        cout << left << fixed << setprecision(2);//how many dp
    cout << left << setw(25) << "Option Number" << left << setw(25) << "Pick Up Point" << "Dropoff Point" << endl; //display column header

    while (!inFile.eof())
    {
        inFile >> ON >> PUP >> Dropoff;
        if (inFile.fail()) break;
        cout << left << setw(25) << ON << left << setw(25) << PUP << Dropoff << endl;

    }

    cout << "Please select an option for your trip: ";
    cin >> sel;
    
    //(im stuck after here)
    
    cout << "You have selection option number " << ON << " and your pickup point is " << PUP << " and your dropoff point is " << Dropoff << endl;


    system("pause");
    return 0;

}

Make a list of things you need to do:

  1. read file (almost done)
  2. print options in file (done)
  3. put data somewhere we can look it up later (not done)
  4. get option from user (done)
  5. look up option (not done)
  6. print selected option (almost done)

Fixing point 1

The current code reads three fields from the row of data. There are 4 fields. The fourth field needs to be removed from the stream.

int unused;
while (inFile >> ON >> PUP >> Dropoff >> unused) // read unused field into junk variable
{
    //print
}

Implementing point 3

Define a structure that can store the data

struct record
{
    string PUP;
    string Dropoff;
};

Option number will be likely used as the key for looking up the structure, so it doesn't need to be in the structure.

Next we need a container to store these records

If the file is guaranteed to be start with a predictable option number and be listed in a continuous sequential order, we can use a std::vector to store the record s. If the list is predictable, sequentially ordered and of known size we can use std::array If not, use an associative table to map the option number to the record . We're not given any guarantees of ordering or size, so I'll demonstrate with std::map

record in;
map<int, record> records; // convenient data structure for look-up
int ON;
int unused; // need to read and dum that last field on the line
while (inFile >> ON >> in.PUP >> in.Dropoff >> unused)
{
    records[ON] = in; // store row in datastructure at the option number.
}

If you're smart, you will read the file and print the menu at the same time. Otherwise you're reading the file twice or unnecessary looping through the container of record s.

Implementing point 5

Look up the record in the container. If std::map is used, that can be as simple as

record & out  = records.at(sel);

at will throw an exception if the option is not in the list. You can either catch and handle the exception or let the program crash.

Fixing point 6

We use the reference to the record to update the existing code to use the correct record .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM