C++ 使用C+中的getline读取数字和名称+;?

C++ 使用C+中的getline读取数字和名称+;?,c++,string,pointers,input,getline,C++,String,Pointers,Input,Getline,所以我已经在这个问题上工作了好几个小时了,我想得到一些帮助。我明天有个节目。基本上,我有一个输入文件,有名字和姓氏,然后是四个浮点数。看起来是这样的: John W. Smith 78.8 56.5 34.5 23.3 Jane Doe 34.5 23.4 35.7 87.0 No More 我需要将名字和姓氏读入指针数组。到目前为止,我只是在尝试读取变量“name”的每一行,并将其输出到一个文本文件,以查看是否正确读取了数据。不幸的是,它在读入浮动后停止,而不会读入下一个名称 char *

所以我已经在这个问题上工作了好几个小时了,我想得到一些帮助。我明天有个节目。基本上,我有一个输入文件,有名字和姓氏,然后是四个浮点数。看起来是这样的:

John W.
Smith
78.8 56.5 34.5 23.3
Jane 
Doe
34.5 23.4 35.7 87.0
No
More
我需要将名字和姓氏读入指针数组。到目前为止,我只是在尝试读取变量“name”的每一行,并将其输出到一个文本文件,以查看是否正确读取了数据。不幸的是,它在读入浮动后停止,而不会读入下一个名称

char *newPtr;               
char *nameList[50] = {0};   
char name[15];  
int i = 0; 
infile.getline(name, 15);

while (strcmp (name, "No") != 0)
{
    newPtr = new char[15];
    strcpy(newPtr, name);
    nameList[i] = newPtr;
    infile.getline(name, 15);
    outfile << name << endl;
    i++;
}

我们还没有学过弦,我肯定我不能用我们在课堂上没有讲过的东西。感谢您迄今为止对所有发表评论的人的帮助。

您就快到了。。。最重要的是你忘了读数字。下面的代码将它们读入
w
x
y
z
,但随后忽略它们。。。您应该对它们执行任何需要的操作,例如,可能为姓氏创建第二个数组,为浮点数创建第三个二维数组,或者创建一个4倍长的一维数组

char *nameList[50] = {0};   
char name[15];  
int i = 0; 

while (infile.getline(name, sizeof name) && strcmp(name, "No") != 0)
{
    char* newPtr = new char[sizeof name];
    strcpy(newPtr, name);
    nameList[i] = newPtr;
    double w, x, y, z;
    if (infile.getline(name, sizeof name) && infile >> w >> x >> y >> z)
        outfile << name << endl;
    else
    {
        std::cerr << "missing rest of data for " << nameList[i] << " found\n";
        exit(EXIT_FAILURE)
    }
    i++;
}
char*名称列表[50]={0};
字符名[15];
int i=0;
while(infle.getline(name,sizeof name)和&strcmp(name,“No”)!=0)
{
char*newPtr=newchar[sizeof name];
strcpy(newPtr,名称);
名称列表[i]=新PTR;
双w,x,y,z;
if(infle.getline(name,sizeof name)和&infle>>w>>x>>y>>z)

outfile与您的问题无关,但您的while循环中存在内存泄漏。为什么要使用字符数组而不是
std::string
s?此外,您的第一个
strcmp
比较已分配但未初始化的
名称
(意味着没有空终止字符).这是我的老师告诉全班同学如何做的。我也很困惑。如果你对老师让你做的事情感到困惑,也许你应该和他谈谈?你没有在循环中增加数组索引。你正在覆盖数组中的同一个插槽。问你的老师关于智能指针的问题,以及为什么你的作业不需要智能指针。
char *nameList[50] = {0};   
char name[15];  
int i = 0; 

while (infile.getline(name, sizeof name) && strcmp(name, "No") != 0)
{
    char* newPtr = new char[sizeof name];
    strcpy(newPtr, name);
    nameList[i] = newPtr;
    double w, x, y, z;
    if (infile.getline(name, sizeof name) && infile >> w >> x >> y >> z)
        outfile << name << endl;
    else
    {
        std::cerr << "missing rest of data for " << nameList[i] << " found\n";
        exit(EXIT_FAILURE)
    }
    i++;
}