比较两个字符串数组
comparing two string arrays 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
我故意把第一个城市从"亚特兰大"改成了"开普"。有人能告诉我我的错误并告诉我需要做些什么来纠正它吗?谢谢!
只有当它是您根据列表检查的最后一个节点时,您才会发现一个坏值。您需要将检查循环更改为如下内容:
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 ) // value not found, break out of verification loop to report this.
break;
}
一旦被检查的项不在列表中,就会停止验证。
有太多的代码,很多人(像我)甚至不会开始阅读它。试着写一个更短的程序来重现这个问题。
我也建议你读一下http://www.cplusplus.com/reference/string/string/compare小心。
相关文章:
- 两个字符串在 c++ 中不相等
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 比较两个字符串后卡在无限循环中
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 两个字符串之间的数学运算
- sscanf() 有两个字符串参数
- 我的字符串数组一次打印出前两个字符串
- 两个 c++ 字符串上的异或
- 函数返回两个字符串的第一个常用字符
- 目标是找到两个 c 字符串之间的公共前缀(必须使用特定的函数标头)
- 我需要在C++的两个字符串之间找到共同的前缀
- 我有一个返回字符串向量的函数.它需要两个字符串,并且返回一个字符串中缺少的字符串
- 给定两个字符串 S 和 T.确定与 T 相差最小的 S 子字符串?
- gtest:检查字符串是否等于两个字符串之一
- 交换两个字符串时访问正确的内存时,我遇到了分段错误
- 我可以在系统()中使用两个字符串吗?