簡體   English   中英

字符串值與C ++不匹配

[英]String values not matching C++

大家好,StackExchange

我正在使用C ++,但似乎遇到了無法正確匹配兩個字符串值的錯誤。 我的主要代碼如下:

/**
*   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;
}

如您所見,我注釋掉了代碼,以擺脫對我的問題不重要的所有內容。 當我讀取文件時,我嘗試匹配所有等於car的行。 它應該起作用,因為我嘗試使所有線相等,並且這行得通,但是僅使線與car相等就行了。 我現在只停留在這一件事上。

這是它正在讀取的文本文件:

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

對我來說,實際上有必要完成我的作業,但我不知道如何解決。 請幫我。

我感謝WhozCraig並給了我答案。 因為我使用!file.eof()而不是file >> data ,所以可能出現索引問題。

所以這是次要的代碼差異:

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