큰 프로그램에 대한 데이터 파일의 유효성을 검사하고 있습니다. 나는 코드의 마지막 부분에 있으며 두 개의 개별 배열이 문자열 자체에 의해 서로 "동일"하도록 노력하고있다. 최신 시도 코드를 포함 시켰습니다. 전체 기능을 게시 했으므로 용서해주십시오.두 문자열 배열 비교 C++
는 여기있다 :
//
// validateDataFile.cpp
// P1
//
// Created by xxxxxxx on 3/26/13.
// Copyright (c) 2013 xxxxxxx. All rights reserved.
//
#include "p1.h"
void validateDataFile (string fileName) {
fstream file;
file.open (fileName.c_str());
if (file.is_open()) {
unsigned int i, j, x = 0, y = 0, a;
int flag = 0, check = 0;
string fileData, word, strg[200];
getline (file, fileData);
for (i = 0; i < fileData.length(); i++) {
if ((fileData[i] == ' ') || (fileData[i] < 48) || (fileData[i] > 57)) {
cout << "fileData[i]: " << fileData[i] << endl;
cout << "Incorrect DataFile!\nFirst line should contain a positive"
" integer and no white space" << endl;
return;
}
}
int numberOfNodes = convertToInt(fileData);
string list[numberOfNodes];
if (numberOfNodes < 0) {
cout << "Number of Nodes: " << numberOfNodes << endl;
cout << "Incorrect DataFile!\nFirst character should be a positive"
"integer" << endl;
return;
}
getline (file, fileData);
stringstream stream (fileData);
while (getline (stream, word, ' ')) {
list[x++] = word;
for (a = 0; a < numberOfNodes; a++) {
cout << "list of nodes: " << list[a] << endl; //testing only
}
}
if (x != numberOfNodes) {
cout << "Incorrect DataFile!\nList of strings has more strings than"
" the number of nodes specified in the first line." << endl;
return;
}
while (!file.eof()){
getline (file, fileData);
stringstream ss (fileData);
while (getline (ss, word, ' ')) {
if (convertToInt(word) < 0) {
flag = 0;
for (i = 0; i < y; i++) {
if (strg[i] == word) flag = 1;
}
if (flag == 0) strg[y++] = word;
}
}
}
for (i = 0; i < y; i++) { //<- my problem starts here
check = 0;
for (j = 0; j < x; j++) {
if (strg[i].compare (list[j]) == 0) {
check = 1; //<- my problem ends here
break;
}
}
}
if (check == 0) {
cout << "Incorrect DataFile!\nStrings listed should match Node Strings" << endl;
return;
}
}
else {
cout << "ERROR!\n DataFile not present." << endl;
return;
}
file.close();
}
그것은 오류없이 컴파일하지만가 원하는 것을하지 않습니다.
의도적으로 데이터 파일을 변경하여 오류를 만들었지 만 어떤 이유로 내 비교가 잘못되었다고 알려주지 않습니다. 여기
내 데이터 파일의 작은 부분이다. "애틀랜타"16
Cape Birmingham Boston Chicago Dallas Detroit KansasCity LosAngeles Memphis Minneapolis Omaha Orlando Richmond SanFrancisco Seattle StLouis
Atlanta Chicago 718
Atlanta Dallas 781
Atlanta Orlando 439
Birmingham Atlanta 146
Birmingham Detroit 723
Birmingham Richmond 678
Boston Atlanta 1099
Boston Detroit 716
Boston Memphis 1311
Chicago Atlanta 718
Chicago Boston 983
Chicago KansasCity 526
내가 의도적에서 최초의 도시 "케이프"로 변경 누군가 내 실수를 말하고이를 해결하기 위해해야 할 일을 보여줄 수 있습니까? 감사합니다.
많은 코드가 있습니다. 파일이 같은지 확인하거나 단어 배열이 동일한 모든 단어를 토큰 화하는 경우 확인하십시오. 그렇다면 왜 http://www.cplusplus.com/reference/algorithm/equal/을 사용할 수 없습니까? – rerun
@rerun 예. 문제가 시작되고 멈추는 곳에서 코드가 표시됩니다. –
@ user1318371 두 개의 배열이 보이지 않습니다. 코드가'strg'를'strg'에 비교합니다. 또한 두 문자열이 같은지 확인하는 쉬운 방법은 '=='를 사용하는 것이 아니라'비교 '를 사용하는 것입니다. – john