Snagged yet again...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 45
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

Snagged yet again...

 
0
  #1
Jul 4th, 2009
Here we go again. As you can see, I have gotten much further. There are some elements however that I am unsure how to apply (i.e. bool tooMany). I haven't the slightest how to apply that. That is one snag that I have. Another, and the main one, is this:

The following code does work. It calls a file called "studentData.txt". Said file contains the ID#s and Scores on their own lines :
(id) 101
(score) 100
102
95
103
90
...
...
121
0

Now, if I comment that out and just have it read from the arrays that I have hardcoded it works great. I can't quite figure out how to compute the .txt items into the individual arrays to make it use those as opposed to the hardcoded arrays. One of my main issues with reading from a .txt file, is that the only way I know how is using the getline feature. Is there anything better?

Currently I have the code calling the .txt file.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5.  
  6. using namespace std;
  7. void printTable(int score[], int id[], int count);
  8. void printGrade(int oneScore, float average);
  9.  
  10. void readStudentData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany)
  11. {
  12. const int MAX_SIZE = 21;
  13. rss.open("studentData.txt");
  14. string line;
  15. id[MAX_SIZE];
  16. int score[MAX_SIZE];
  17. count = 0;
  18. int oneScore = 0;
  19. float average = 0;
  20. string grade;
  21.  
  22. for(count = 0; count < MAX_SIZE; count++)
  23. {
  24. getline(rss,line);
  25. cout << line;
  26. getline(rss,line);
  27. cout << " " << line;
  28. cout << " " << grade << endl;
  29. }
  30.  
  31. // printTable(score, id, count);
  32.  
  33. }
  34. float computeAverage(int scores[], int count[])
  35. {
  36. const int MAX_SIZE = 21;
  37.  
  38. return 0;
  39. }
  40. void printTable(int score[], int id[], int count)
  41. {
  42. void printGrade(int oneScore, float average);
  43. const int MAX_SIZE = 21;
  44. int oneScore = 0;
  45. float average = 0;
  46. string grade;
  47. id[MAX_SIZE];
  48. score[MAX_SIZE];
  49.  
  50. cout << left << setw(9) << "ID#s" << setw(9) << "Scores" << setw(9) << "Grades" << endl << endl;
  51. //for(count = 0; count < MAX_SIZE; count++)
  52. //{
  53. printGrade(oneScore,average);
  54. //}
  55.  
  56. }
  57. void printGrade(int oneScore, float average)
  58. {
  59. const int MAX_SIZE = 21;
  60.  
  61. int id[MAX_SIZE] = {101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121};
  62. int scores[MAX_SIZE] = {100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5,0};
  63. oneScore = 0;
  64. average = 0;
  65. string grade;
  66.  
  67. int sum = 0;
  68. for(int i = 0; i < MAX_SIZE; i++)
  69. sum += scores[i];
  70. average = sum / MAX_SIZE;
  71.  
  72. for(int i = 0; i < MAX_SIZE; i++)
  73. {
  74. if(scores[i] > average + 10)
  75. {
  76. grade = "outstanding";
  77. }
  78. else if(scores[i] < average - 10)
  79. {
  80. grade = "unsatisfactory";
  81. }
  82. else
  83. {
  84. grade = "satisfactory";
  85. }
  86.  
  87. // cout << left << setw(9) << id[i] << setw(9) << scores[i] << setw(9) << grade << endl;
  88. }
  89.  
  90. }
  91. int main()
  92. {
  93. ifstream rss;
  94. string line;
  95. const int MAX_SIZE = 21;
  96. int scores[MAX_SIZE];
  97. int id[MAX_SIZE];
  98. int count;
  99. bool tooMany;
  100.  
  101. readStudentData(rss, scores, id, count, tooMany);
  102.  
  103.  
  104.  
  105. return 0;
  106. }
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Snagged yet again...

 
0
  #2
Jul 4th, 2009
DO NOT declare new arrays inside your functions!

You've already declared them and provided storage for them in main here:

	const int MAX_SIZE = 21;
	rss.open("studentData.txt");
	string line;
	id[MAX_SIZE];
	int score[MAX_SIZE];

Don't do it anywhere else, like here:

void readStudentData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany)
{
	const int MAX_SIZE = 21;
	rss.open("studentData.txt");
	string line;
	id[MAX_SIZE];
	int score[MAX_SIZE];
	count = 0;
	int oneScore = 0;
	float average = 0;
	string grade;

	for(count = 0; count < MAX_SIZE; count++)
	{
		getline(rss,line);
		cout << line;
		getline(rss,line);
		cout << "  " << line;
		cout << "  " << grade << endl;
	}

//	printTable(score, id, count);

}

Red is where you're redeclaring your array. Don't do it. Green are the arrays the function has been passed. Delete the red lines. Anything having to do with the arrays needs to involve the array names given in green above.

Ditto for any declaration of arrays or MAX_SIZE in any other function. I imagine that, assuming MAX_SIZE needs to be visible inside the functions, you need to make it a global variable. However, I'm not sure MAX_SIZE needs to be seen in any other function. But I don't know. The tooMany variable and the function specifications puzzle me a bit. Keep in mind that this is a new thread, so most people, unlike me, haven't seen your last threads and thus don't know about it or the function specifications in it.

Google "variable scoping" and "global versus local variable" to get a better feel of why I am suggesting you should NOT declare these arrays inside of your functions. Declare them once in main, then pass them everywhere to your functions. When you do that, you don't need to redeclare them.

You should get clarification on what the boolean tooMany variable stands for. Note you have MAX_SIZE set equal to 21. Do you know for sure you'll have EXACTLY 21? Could you not have less? If so, you have a for-loop that tries to read in 21 lines. What if there are only 14 lines? Since count is passed by reference, I am guessing that you don't know how many lines there will be.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 45
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

Re: Snagged yet again...

 
0
  #3
Jul 4th, 2009
Thanks. That helped trim down my code a bit and I managed to get a few other things done with it. Appreciate the help.
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
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
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC