Need Help,plz...><

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 4
Reputation: DeVilSoul is an unknown quantity at this point 
Solved Threads: 0
DeVilSoul DeVilSoul is offline Offline
Newbie Poster

Need Help,plz...><

 
0
  #1
Aug 15th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need Help,plz...><

 
0
  #2
Aug 15th, 2005
Could you post the complete code? (Where you define the structure, etc.)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 4
Reputation: DeVilSoul is an unknown quantity at this point 
Solved Threads: 0
DeVilSoul DeVilSoul is offline Offline
Newbie Poster

Re: Need Help,plz...><

 
0
  #3
Aug 16th, 2005
Here,i did some modifications but the problem persisted.It gave me garbage when i cout the supposedly stoed values to check.


  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: Need Help,plz...><

 
0
  #4
Aug 16th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: Need Help,plz...><

 
0
  #5
Aug 16th, 2005
and use code tags. code not tagged looks horrible and is enough to put me off even looking through it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 4
Reputation: DeVilSoul is an unknown quantity at this point 
Solved Threads: 0
DeVilSoul DeVilSoul is offline Offline
Newbie Poster

Re: Need Help,plz...><

 
0
  #6
Aug 17th, 2005
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??
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need Help,plz...><

 
0
  #7
Aug 17th, 2005
Can you provide the contents of your marks.txt test file?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 4
Reputation: DeVilSoul is an unknown quantity at this point 
Solved Threads: 0
DeVilSoul DeVilSoul is offline Offline
Newbie Poster

Re: Need Help,plz...><

 
0
  #8
Aug 18th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: nattylife is an unknown quantity at this point 
Solved Threads: 3
nattylife nattylife is offline Offline
Newbie Poster

Re: Need Help,plz...><

 
0
  #9
Aug 18th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1479 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC