944,045 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1583
  • C++ RSS
Aug 15th, 2005
0

Need Help,plz...><

Expand Post »
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[i].module,4);
cout<<rarray[i].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[i].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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DeVilSoul is offline Offline
4 posts
since Aug 2005
Aug 15th, 2005
0

Re: Need Help,plz...><

Could you post the complete code? (Where you define the structure, etc.)
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Aug 16th, 2005
0

Re: Need Help,plz...><

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


C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct student
  7. {
  8. int subj;
  9. int index;
  10. char name[21];
  11. float marks[5];
  12. char grade;
  13. };
  14.  
  15. void readStudents(ifstream&,int,student*);
  16. int conv (char*);
  17.  
  18. void main()
  19. {
  20. int length;
  21. student slist[20];
  22. ifstream marks("marks.txt");
  23. marks>>length;
  24. readStudents(marks,length,slist);
  25. }
  26.  
  27. void readStudents(ifstream& infile,int limit,student *slist)
  28. {
  29. char score[4];
  30. for(int i=0;i<limit;i++)
  31. {
  32. infile.ignore(3);
  33. infile>>slist[i].subj;
  34. infile.ignore(3);
  35. infile>>slist[i].index;
  36. infile.ignore(1);
  37. infile.getline(slist[i].name,21,',');
  38. for(int j=0;j<4;j++)
  39. {
  40. infile.getline(score,4,',');
  41. slist[i].marks[j]=conv(score);
  42. }
  43. infile.ignore('\n');
  44. cout<<slist[i].subj;
  45. }
  46. }
  47. int conv(char *arr)
  48. {
  49. if(arr[0]=='A')
  50. return 0;
  51. else if(arr[0]=='T')
  52. return -1;
  53. else if(arr[0]=='1'&&arr[2]=='0')
  54. return 100;
  55. else
  56. return (arr[0]-48)*10+(arr[1]-48);
  57. }//this part of the code is another part of the qn.
Last edited by Dave Sinkula; Aug 16th, 2005 at 11:30 am. Reason: Added [code][/code] tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DeVilSoul is offline Offline
4 posts
since Aug 2005
Aug 16th, 2005
0

Re: Need Help,plz...><

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
freemind is offline Offline
62 posts
since Jun 2005
Aug 16th, 2005
0

Re: Need Help,plz...><

and use code tags. code not tagged looks horrible and is enough to put me off even looking through it.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Aug 17th, 2005
0

Re: Need Help,plz...><

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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DeVilSoul is offline Offline
4 posts
since Aug 2005
Aug 17th, 2005
0

Re: Need Help,plz...><

Can you provide the contents of your marks.txt test file?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Aug 18th, 2005
0

Re: Need Help,plz...><

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DeVilSoul is offline Offline
4 posts
since Aug 2005
Aug 18th, 2005
0

Re: Need Help,plz...><

Quote originally posted by DeVilSoul ...
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
Reputation Points: 10
Solved Threads: 3
Newbie Poster
nattylife is offline Offline
14 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Difficult
Next Thread in C++ Forum Timeline: fstream to char and int array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC