C++ C++;101-看起来我在某个地方有一个无限循环,但不知道我';我做错了
因此,我有一个编程项目,这是迄今为止我不得不做的最困难的项目。我们收到了一份文件,上面列有人们的姓名、生日和薪水。除其他事项外,我们必须确保每个姓名的格式正确(LASTNAME,FIRST MIDDLE(如果他们有))……目前,假设姓氏格式不正确(FIRST MIDDLE last),下面的代码旨在提取他们的姓氏。文件的第一行是 马修·艾伦·阿贝格1963 452627 正确的输出是 阿伯雷格,马修·艾伦1963 452627 我必须做一些其他的事情,但这就是我被困的地方。这尤其困难,因为有些名字只有第一个和最后一个,只有一些格式不正确。我注释掉了第一个while循环,因为我试图让它甚至只为一个名字工作 作为旁注,只有在我添加一个endl时,才会显示开头的注释“cout”;到最后…否则它就完全被忽略了。似乎整个代码都是这样。我真的不明白怎么会这样,但不管怎样,我还有更大的问题 在Mac上使用Eclipse,如果这有什么改变的话 我确信存在滥用cctype函数或缩短代码的方法的问题。欢迎提出任何建议! 谢谢你的帮助,说真的,我在这件事上耽搁太久了C++ C++;101-看起来我在某个地方有一个无限循环,但不知道我';我做错了,c++,C++,因此,我有一个编程项目,这是迄今为止我不得不做的最困难的项目。我们收到了一份文件,上面列有人们的姓名、生日和薪水。除其他事项外,我们必须确保每个姓名的格式正确(LASTNAME,FIRST MIDDLE(如果他们有))……目前,假设姓氏格式不正确(FIRST MIDDLE last),下面的代码旨在提取他们的姓氏。文件的第一行是 马修·艾伦·阿贝格1963 452627 正确的输出是 阿伯雷格,马修·艾伦1963 452627 我必须做一些其他的事情,但这就是我被困的地方。这尤其困难,因为有些名
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
int main() {
//cout << "test";
ifstream fileIn; // for input files
ofstream fileOut; // for output files
string currentLine; // for analyzing current line
fileIn.open("oldretirement.txt");
fileOut.open("newretirement.txt"); // TODO: move this closer to where it's needed
// TODO: make sure file opens first (while loop?)
int count = 0; // for parsing through each line
int count2 = 0; // for rearranging the name TODO: rename/is this needed?
int pos; // finds the position of first comma
string name; // for rearranging the name if not in correct format
bool rearranged = false; // for rearranging, true if the name is rearranged
//while(!fileIn.eof()){ // while the file is not at its end
getline(fileIn, currentLine);
//cout << currentLine;
for(count = 0; count < currentLine.length(); count++){
pos = currentLine.find_first_of(',');
// cout << pos<<endl;
if(isdigit(currentLine[pos-1])){
count2 = 0;
while(count2 < currentLine.length()){ // change this condition?
if(isdigit(currentLine[count2])){
cout << count2 <<endl; //test
count2 = count2 - 2; // goes back
while(!isspace(currentLine[count2])){
count--;
}
name = currentLine.substr(count2, currentLine.find(" "));
}
count++;
}
}
}
// }
fileIn.close();
fileOut.close();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
//我看到的直接问题是while条件是count2
,但在循环内部,您只执行count2=count2-2
,因此count2
将始终小于currentLine.length()
。在循环结束时,您增加了count
,您是不是想增加count2
?它被注释掉了,但是…@JJJ非常感谢,不敢相信我错过了它。@user4581301注意到,我可能能够在循环中包含一个静态数字,因为我只处理一个文件。谢谢!这是原因之一这就是为什么使用描述性变量名很重要的原因。如果您有例如lineCount
和characterCount
,那么就不容易将它们相互混淆。