0

OK I have a txt file that appears like so

Tacoma Tacoma 0
Tacoma Seattle 15
Tacoma Spokane 24

I have it declared as such

inFile >> city1 >> city2 >> dist

My question is what do I need to do to have my program match my two cin locations to the txt file to get the correct integer on the third column. As an example my first cin location was Tacoma and my second cin location was Spokane, my result should have been 24 when I coded in outFile << dist but I keep getting 0 as a result.

Is there an example for this. I do not need someone writing my code for me but an example of how to get this to work would help me understand what I have been doing wrong.

int client()
{   
string city1;
string city2;
int distance;
bool city1found = false;
bool city2found = false;
ifstream inClient;
ifstream inDist;
ifstream inroute;
ofstream outClient;
ofstream outDist;
inClient.open("client.txt", ios::app);
inDist.open("post.txt", ios::app);
outClient.open("client.txt", ios::app);
outDist.open("post.txt", ios::app);
inroute.open("routes.txt");

inroute >> city1;
inroute >> city2;
inroute >> distance;

cout << "Business Name (First & Last Name): ";
cin >> cFirst >> cLast;
outClient << cFirst << " " << cLast;
outDist << cFirst << " " << cLast;

cout << "Origin of delivery: ";
cin >> clocation;
    if (clocation == city1) //if the current city is equal to the inputted from city
    {
        city1found = true;
    }
    else if (city1found == false)
    {
        cout << endl << "Origin city not found." << endl;
        cin >> clocation;
    }

outClient << "\t\t" << clocation;
outDist << setw(15) << clocation;

cout << "Seven digit phone number (EX. 671-689-5134): ";
cin >> cphone;
outClient << "\t\t" << cphone;

cout << "Email address: ";
cin >> cemail;
outClient << "\t\t" << cemail;

cout << "Destination of delivery: ";
cin >> cdestination;
    if (cdestination == city2) //if the current city is equal to the inputted from city
    {
        city2found = true;
    }
    else if (city2found == false)
    {
        cout << endl << "Detination not found." << endl;
        cin >> cdestination;
    }
outClient << "\t\t" << cdestination;
outDist << setw(15) << cdestination;

cout << "Weight of Shipment(Tons): ";
cin >> weight;
outClient << "\t\t" << weight;
outDist << setw(15) << weight;

//Distance established
if (inroute.is_open())
{
    while (!inroute.eof())
    {
        if (clocation && cdestination == city1 && city2)
        {
            distance;
        }

    }
}
outClient << "\t\t" << distance;
outDist << setw(15) << distance;
outClient << endl;
outDist << endl;

cout << "Order confirmed" << endl;

cout << "Enter '0' to return to the Main Menu or '1' to exit out of program" << endl;
cin >> ans;
if (ans == 0)
{
    system("CLS");
    main();
}
else (ans == 1);
{
    exit(0);
}

}

  • Welcome to StackOverflow. We prefer you to show us your code and an example with the expected output as described [here](http://stackoverflow.com/help/mcve), as it is easier for us to understand and help you. Even if you don't want us to write a solution for you. – Badda May 09 '17 at 13:13

1 Answers1

0

I would use the function getline()which allows to store in a string a file line by line. You would then compare each line with your "cin location" and output the last words of the line if it matches.
Tell me if you want an example code to illustrate this.

EDIT

After you show your code, I have some litte advices to give you. First of all, you should declare and initialize variables only before you need them. It'll be easier to understand for everyone, even yourself. As for streams, you should open them only before read or writing in a file, and close them after. It will also avoid a couple of bugs.

Also, checking a boolean like that :

if (city2found == false)

Is the same thing as doing it like that :

if (!city2found) // Which really means (city2found != true)

This is off-topic, but I recommend doing so since that's mainly why boolean are so cool to use.

How to use use streams and getline() :

ifstream file;
file.open("file.txt");
if (file.is_open())
{
   string line;
   while (getline(inFile, line)) // This will iterate through every line of the file until the end
   {
       if (line == "Tacoma Tacoma 0")  // If you could write a function to extract only the destination from the line, it'd be easier
           return 0;
       if (line == "Tacoma Seattle 15") 
           return 15;
   }
}
file.close();

I haven't had the time to check wether this really worked or not, but it should not be very far from being usable. You can check this link if you need more information about getline.

Badda
  • 1,329
  • 2
  • 15
  • 40
  • That is where I keep getting an error. I guess I'm not to clear on how to use the `getline()` properly. I've attached a part of my code so far in my previous post – Daniel Phillip May 09 '17 at 13:23
  • @DanielPhillip I edited my answer hoping this will help you. Also, I don't know why down-vote this and who did, but I would be glad to know how I could improve my answer. – Badda May 09 '17 at 13:51
  • Is there a way for `getline()` to be used to match the line from the inFile and the `cin` to pull the integer on the third column as a result? Because the code you provided works but only for a small list would it be efficient, but I have a list almost 100 lines long to sort through if I had to do it this way. – Daniel Phillip May 09 '17 at 14:48
  • Something like : 1) User input a destination, 2) we use `getline()` to search in `Infile` for that destination, 3) if we found it, 4) we return the distance ? – Badda May 09 '17 at 14:53
  • more like 1) User inputs an origin and destination. 2) `getline()` searches infile for a match. 3) outputs a distance. Distance being the third column in the infile. – Daniel Phillip May 09 '17 at 15:44
  • Easiest way is to use `strtok_r()` ( `strtok()` is a C function which does not work really safely) to split every line into 3 variables using the space blank as a delimitor. You will then just have to check if the result of your checking matches with the input or not. – Badda May 09 '17 at 18:39
  • I'm really sorry for asking a lot of questions but I am a beginner, so excuse me for asking but can you give me an example of using `strtok()`. Thank you for the help by the way. – Daniel Phillip May 09 '17 at 23:45
  • You can check [here](http://stackoverflow.com/questions/15961253/c-correct-usage-of-strtok-r) and [here](http://www.geeksforgeeks.org/strtok-strtok_r-functions-c-examples/), it is explained better than I'll ever do :) – Badda May 10 '17 at 06:38