简体   繁体   English

字符串值与C ++不匹配

[英]String values not matching C++

Hi generous people of StackExchange, 大家好,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. 我正在使用C ++,但似乎遇到了无法正确匹配两个字符串值的错误。 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. 当我读取文件时,我尝试匹配所有等于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. 它应该起作用,因为我尝试使所有线相等,并且这行得通,但是仅使线与car相等就行了。 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. 请帮我。

I thank WhozCraig and for giving me the answer. 我感谢WhozCraig并给了我答案。 There was probably come indexing issue that occured because I used !file.eof() instead of file >> data . 因为我使用!file.eof()而不是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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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