【发布时间】:2011-04-16 02:44:29
【问题描述】:
所以我基本上只是尝试接收一些文件输入,然后将这些数据放入几个结构中。我遇到的唯一问题是指向结构的指针的命名。结构本身应该代表学生,我想将每个指针设置为他们的名字之一,而不是任意变量。我试图以一种我认为语法错误的方式来做到这一点,因为它不起作用。在下面的代码中,我使用 temp 数组递增 for 循环,因为每个第 4 个位置都是一个新学生。关于如何解决这个问题的任何想法?
#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;
}
【问题讨论】:
-
亲爱的耶稣,请正确格式化您的代码(我会修复它,但在未来,缩进四个空格以使这对每个人都不完全可怕!谢谢)。 daringfireball.net/projects/markdown/syntax
-
看起来@peachykeen 打败了我! :) 干杯
-
目前尚不清楚您要在这里实现什么。你是什么意思“获取数据并将其放入多个结构中”?什么结构?输入在文件中的样子如何?