1

Hi generous people of StackExchange,

I'm working with C++ and I've seemed to run into the error of not being able to match two string values properly. I have the code in main:

/**
*   Author: Kevin Hill
*   Assignment: Project5
*   Description: VehicleDB
**/

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include "Vehicle.h"
//#include "Truck.h"
//#include "Car.h"

using namespace std;

bool checkLine(string, string);

int main(int argc, char const *argv[])
{

    string fileName; //This is the file name
     //Vehicle array
    ifstream file; //Instantiate file
    string sLine;
    Vehicle **vehArr;

    //Tell user to enter data
    cout << "Enter the file name: ";
    cin >> fileName;
    file.open(fileName.c_str());
    if(!file.is_open()){
        cerr << "The file is not there" << endl;
        return 1;
    }


    //Get first line of file to determine size of vehArr    
    for (int i = 0; !file.eof(); ++i)
    {
        if (i == 0){
            getline(file, sLine); 
            break;
        }
    }

    int arrSize = atoi(sLine.c_str());
    vehArr =  new (nothrow) Vehicle*[arrSize];
    string truck = "truck";
    string car = "car";
    while(!file.eof()){
        getline(file, sLine);
        string stuff = sLine;
        checkLine(stuff, car);
    }

    // Test add Truck and Car objects
    // vehArr[0] = new Car(file);
    // vehArr[1] = new Truck();

    string theLine = "";

    // checkLine("one", "one");

    // cout << "Beginning loop" << endl;

    // for (int i = 0; !file.eof(); ++i){
    //  getline(file, theLine);



    //  // if(theLine == "car"){
    //  //  vehArr[i] = new Car(file);
    //  //  i++;
    //  // }
    //  // if (theLine == "truck"){
    //  //  vehArr[i] = new Truck(file);
    //  //  i++;
    //  // }
    //  // if (i == arrSize){
    //  //  break;
    //  // }    
    // }

    return 0;
}

bool checkLine(string one, string two){

    // if (one == two)
    // {
    //  /* code */
    // }



    if (one == two)
    {
        cout << "It's a " << two << endl;
    }
    // cout << one << endl;
    // cout << two << endl;

    return true;
}

As you can see, I commented the code out to get rid of all the stuff that's not important for my problem. When I read my file I try to make a match of all the lines that equal car. It should work because I tried having all lines equal themselves and that worked, but having the lines equal car alone just didn't work out. I'm now stuck on this one thing.

This is the text file it's reading from:

5
car
11111
Ford
Fusion FWD
Red
24999.00
3
AC
795.00
XM
295.00
Leather Seats
495.00
20
28
car
22222
Volvo
V70 FWD
White
42999.00
2
Child Seats
198.00
17" wheels
698.00
14
22
car
33333
Honda
Accord
Silver
19999.00
1
Tinted Windows
175.00
22
31
truck
44444
Freightliner
FLD SD
Red
119999.00
1
Detroit Diesel Series 60
4999.00
10 speed manual
455
truck
55555
Mack
TerraPro Cabover MRU612
White
125000.00
1
Dual Air Brake System
6000.00
8 speed manual
325

It's necessary for me to actually complete my assignment, but I can't figure out how fix this. Please help me.

Kivo360
  • 691
  • 3
  • 8
  • 12
  • 1
    Note: see this: `while(!file.eof())` ? Now read this: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Then consider how *every* `std::getline()` that you're invoking should be checked in a conditional of some kind. – WhozCraig Dec 02 '13 at 00:59
  • Is it intentional for your `checkLine()` function to be returning true all the time, no matter whether or not the strings are equal? – Commander Coriander Salamander Dec 02 '13 at 01:02
  • Wow, that worked out well. Thanks a lot dude. – Kivo360 Dec 02 '13 at 01:08
  • Now I've run into an even bigger issue. When I have any spaces it thinks I switched my line. How do I fix this – Kivo360 Dec 02 '13 at 01:48

1 Answers1

1

I thank WhozCraig and for giving me the answer. There was probably come indexing issue that occured because I used !file.eof() instead of file >> data.

So here was the minor code difference:

string truck = "truck";
string car = "car";
//Old version
 while(!file.eof()){
    getline(file, sLine);
    string stuff = sLine;
    checkLine(stuff, car);
 }

string data;
while(file >> data){    
// cout << data << endl;
checkLine(data, car);
}
Kivo360
  • 691
  • 3
  • 8
  • 12