所以我基本上只是试图接受一些文件输入,然后把这些数据放到几个结构中。我遇到的唯一问题是指向结构体的指针。结构本身应该代表学生,我想将每个指针设置为它们的名称之一而不是任意变量。我试图以我假设的语法错误,因为它没有工作。在下面的代码中,我使用temp数组增加for循环,因为每个第四个位置都是一个新学生。关于如何去做这件事的任何想法?在C++中制作一个指向结构或对象的指针数组
#include<iostream>
#include<iomanip>
#include"student.h"
#include"creditcard.h"
#include<fstream>
using namespace std;
int main()
{
string creditcards[20];
int i;
int x;
int amount;
string temp[20];
ifstream infile;
string filename;
int count;
int numstudents;
string newstring="";
string pointers[20];
cout<<"enter the file name of which you've stored your"<<endl
<<"credit card infomation"<<endl;
getline(cin,filename,'\n');
infile.open(filename.c_str());
count=0;
getline(infile,temp[count],'\n');
while(! infile.eof())
{
count++;
getline(infile,temp[count],'\n');
numstudents= (count/4);
if(numstudents < 1 || count%4 != 0)
{
cout<<"incorrect data file"<<endl;
}
}
cout<<numstudents<<endl;
for(i=0,x=0; i<numstudents;i++,x+4)
{
student *temp[x];
temp[x] = new student;
pointers[i] = temp[x];
}
for(i=0;i<numstudents;i+4)
{
cout<<temp[i]<<endl;
}
return 0;
}
甜耶稣,请正确格式化您的代码(我会修复它,但在未来,缩进四个空格,使这不是每个人都完全可怕!谢谢)。 http://daringfireball.net/projects/markdown/syntax – 2011-04-16 02:46:57
看起来像@peachykeen击败了我! :)干杯 – 2011-04-16 02:49:02
Oooooopsy雏菊 – Sam 2011-04-16 02:49:03