ok,i'm suppose to read all the "CP0X"(where x is a number) from a file and save it into a array.Then read the name(the YYYYY,ZZZZZ,etc) and save each them into array.

Example:
CP01 YYYYY
CP02 ZZZZZ
CP04 WWWW
CP01 RRRRR
CP03 UUUUU
.
.
.
CPO2 TTTTT

So....i did this

for(int i=0;i<10;i++)
{
infile.getline(rarray.module,4);
cout<<rarray.module;
}

where everything listed here are declared properly.It should by right show me something like CP01->rarray[0].module,CP02->rarray[1].module,CP04,CP01,CP03,where each "CP0X" is stored correctly in rarray.module.BUT it didn't.I want them reading the data downwards then saving the data correctly into the array,not overflowing into the next array for the name.Can someone please help me with this?Thanks:)

Recommended Answers

Could you post the complete code? (Where you define the structure, etc.)

Jump to Post

Don't forget that C++ gives you the opportunity to use string instead of char[]. Combining this with the usage of find() and substr() can solve your problems.

Jump to Post

All 8 Replies

Could you post the complete code? (Where you define the structure, etc.)

Here,i did some modifications but the problem persisted.It gave me garbage when i cout the supposedly stoed values to check. :(

#include<iostream>
#include<fstream>

using namespace std;

struct student
{
	int subj;
	int index;
	char name[21];
	float marks[5];
	char grade;
};

void readStudents(ifstream&,int,student*);
int conv (char*);

void main()
{
	int length;
	student slist[20];
	ifstream marks("marks.txt");
	marks>>length;
	readStudents(marks,length,slist);
}

void readStudents(ifstream& infile,int limit,student *slist)
{
	char score[4];
	for(int i=0;i<limit;i++)
	{
		infile.ignore(3);
		infile>>slist[i].subj;
		infile.ignore(3);
		infile>>slist[i].index;
		infile.ignore(1);
		infile.getline(slist[i].name,21,',');
		for(int j=0;j<4;j++)
		{
			infile.getline(score,4,',');
			slist[i].marks[j]=conv(score);
		}
		infile.ignore('\n');
		cout<<slist[i].subj;
	}
}
int conv(char *arr)
{
	if(arr[0]=='A')
		return 0;
	else if(arr[0]=='T')
		return -1;
	else if(arr[0]=='1'&&arr[2]=='0')
		return 100;
	else
		return (arr[0]-48)*10+(arr[1]-48);
}//this part of the code is another part of the qn.

Don't forget that C++ gives you the opportunity to use string instead of char[]. Combining this with the usage of find() and substr() can solve your problems.

and use code tags. code not tagged looks horrible and is enough to put me off even looking through it.

Yeah,but my purpose isn't to find a character or integer,but to read from the file.I can read the first CP04,i did infile.ignroe(3)to skip "CP0" and i got '4' in my array,first slot.However,after the first number,i can't store the rest properly,when i cout i got minus values.So can anyone tell me what's wrong?? :?:

Can you provide the contents of your marks.txt test file?

here: :)
10
CPO1,S0100,Albert A,85,6592,31
CPO3,S0100,Albert A,77,46,73,74
CPO0,S0101,Candy C,70,97,62,49
CPO0,S0101,Candy C,59,67,42,59
CPO2,S0102,Cecilia C,93,64,34,64
CPO4,S0102,Cecilia C,98,31,66,93
CPO4,S0103,Fanny F,71,50,83,87
CPO3,S0103,Fanny F,70,46,93,56
CPO2,S0104,Alec A,94,99,34,81
CPO4,S0104,Alec A,40,55,59,100
CPO3,S0105,Eric E,62,95,82,39
CPO4,S0105,Eric E,89,60,97,73
CPO3,S0106,Shawn S,91,49,88,89
CPO3,S0106,Shawn S,94,73,76,52
CPO2,S0107,Tiffy T,57,93,56,53
CPO4,S0107,Tiffy T,69,71,45,65
CPO3,S0108,Hua yi,73,39,60,68
CPO1,S0108,Hua yi,98,48,100,47
CPO3,S0109,Flash F,77,35,75,51
CPO1,S0109,Flash F,49,84,64,90

Yeah,but my purpose isn't to find a character or integer,but to read from the file.I can read the first CP04,i did infile.ignroe(3)to skip "CP0" and i got '4' in my array,first slot.However,after the first number,i can't store the rest properly,when i cout i got minus values.So can anyone tell me what's wrong?? :?:

read the file line by line, and parse each line and save it to your struct

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.