Join 136,099 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,642 people online right now. Registration is fast and FREE... Join Now!
while (getline(infile,number,'\n')) { cout<<"The number of test scores : "<<num<<endl; double average = (number/num);//average of all the grades cout<<"The average of the test scores : "<<average<<endl; cout<<"The highest test score was "<<Hi<<" and the lowest test grade was "<<Low<<endl; cout<<"Grade "<<setw(5)<<"Status"<<endl;
if (number<average) { cout<<number<<setw(5)<<" Below"<<endl; } else if (number>average) { cout<<number<<setw(5)<<" Above"<<endl; }
return 0; }
The assignment is as follows: Write a c++ program USING FUNCTIONS, that will read in an unknown number (not more than 60) of test grades from a text file named "functest.txt"(make your own). The program is to calculate and output the following: 1. Number of Test Grades 2. Average of the Test Grades ( 2 decimal places) 3. The Highest and Lowest Test Grades 4. A list of the grades and whether the grade is ABOVE or BELOW the average. The grade and status should take the following format: Grade Status XX ABOVE XX BELOW etc. 5. Output the results of all the calculations You may assume that no grade will be exactly equal to the average.
A few things: 1. How can you determine the number of grades in the infile? 2. How can you create a function for just reading in the information from the infile? 3. To see which of the grade is higher and lower...i'm sure that a series of if/else statements could work, but it wouldn't be practical is you had several hundred records. How do you find out the highest and lowest grade from the infile? 4. With regards to the output, to get it formatted as is requested (above)...i'm familiar with using something like if (i%2==0) print<<endl; info..... But in this case how could I use that to manipulate the headers Grade & Status? My infile is as follows:
Well, I think we can start with question #2. The main() is a function, so you could just make another function. Place all of the code that reads information in inside of it and then call the function. eg.
while (getline(infile,number,'\n')) { cout<<"The number of test scores : "<<num<<endl; double average = (number/num);//average of all the grades cout<<"The average of the test scores : "<<average<<endl; cout<<"The highest test score was "<<Hi<<" and the lowest test grade was "<<Low<<endl; cout<<"Grade "<<setw(5)<<"Status"<<endl;
if (number<average) { cout<<number<<setw(5)<<" Below"<<endl; } else if (number>average) { cout<<number<<setw(5)<<" Above"<<endl; }
return 0; }
But that is not all you want to do, is it? Lets get back to question #1. How do I find out how many values where red in? The specs say that there will be 60 or less numbers. So an array of integers can be used. eg.
CODE
... int storedGrades[60];
Now you can iterate through that array to fill it with the values from the file. When you are done reading in integers, the counter will have the number of grades. Or if you don't want to store the grades, you could just put the counter in there and skip the array, however, I personally would return the array from the function. eg.
CODE
... int i = 0; while (getline(infile,number,'\n')) { storedGrades[i] = number; ... i++; }
// in order to return an array from a function use: int *readInfo(); // to define the function
This should give you a start into increasing the complexity of your program. And remember, build a little, test a little, build a little, test a little, etc... This will decrease frustration and increase fun.
int main() { int returnVal; returnVal = readInfo(); return returnVal; }
int readInfo() { ifstream infile; ofstream outfile;
int num = 0;//number of grades int Hi = 0;//highest test grade int Low = 0;//lowest test grade string stat;//status of grade, whether higher or lower than average
int i = 0; int storedGrades[60]; while (infile>>number) { storedGrades[i] = number; cout<<"The number of test scores : "<<num<<endl; double average = (number/num);//average of all the grades cout<<"The average of the test scores : "<<average<<endl; cout<<"The highest test score was "<<Hi<<" and the lowest test grade was "<<Low<<endl; cout<<"Grade "<<setw(5)<<"Status"<<endl;
if (number<average) { cout<<number<<setw(5)<<" Below"<<endl; } else if (number>average) { cout<<number<<setw(5)<<" Above"<<endl; } }
return 0; }
My infile:
CODE
48 69 38 79 92
My output:
CODE
The number of test scores : 0 The average of the test scores : 1.#INF The highest test score was 0 and the lowest test grade was 0 Grade Status 48 Below The number of test scores : 0 The average of the test scores : 1.#INF The highest test score was 0 and the lowest test grade was 0 Grade Status 69 Below The number of test scores : 0 The average of the test scores : 1.#INF The highest test score was 0 and the lowest test grade was 0 Grade Status 38 Below The number of test scores : 0 The average of the test scores : 1.#INF The highest test score was 0 and the lowest test grade was 0 Grade Status 79 Below The number of test scores : 0 The average of the test scores : 1.#INF The highest test score was 0 and the lowest test grade was 0 Grade Status 92 Below
The problems: 1. It's not telling me the number of test scores. 2. The average is not calculating. 3.Because the above 2 aren't working, the status is wrong. 4.How can I get it to show the highest and lowest grade. 5.The grade & status should be in colums form with the headers only appearing once. I guess because of how it is in the loop, the headers will repeat. Thanks to all for their input.
nope, beccause you don't keep track of them. you need to actually add one to the value of the number of grades each time you go through the loop.
QUOTE
2. The average is not calculating.
you need to actually calculate the average. there isn't anywhere in your code where you calculate a sum of the grades. the expression (number/num) does not calculate an average, it divides the single number that you just inputted by zero, since you aren't actually incrementing the number of grades each time through the loop
QUOTE
4.How can I get it to show the highest and lowest grade.
you need to actually calculate the highest and lowest values. if the grade just entered is smaller than the lowest grade, set its value to be the lowest; if it is larger than the highest grade, set its value to the highest. you should also set the highest and lowest value to the first entry from the file to start out with, not 0, otherwise your lowest grade will always be zero.
QUOTE
5.The grade & status should be in colums form with the headers only appearing once. I guess because of how it is in the loop, the headers will repeat.
if you just want them to show up once, move them out of the while loop
you should also consider separating your data reading and calculation steps from the output steps. otherwise, you're going to be comparing numbers to a running average, not the overall average. i would recommend creating a second while loop for the display steps, and move all of the display code (your cout << statements) to there. that way you can also calculate the average, and hi and lo values between the two loops, so they don't show up every time.
int main() { int returnVal; returnVal = readInfo(); return returnVal; }
int readInfo() { ifstream infile; ofstream outfile;
int num = 0;//number of grades int Hi;//highest test grade int Low;//lowest test grade string stat;//status of grade, whether higher or lower than average
int i = 0; int storedGrades[60]; while (infile>>number) { storedGrades[i] = number; average = (number/num);//average of all the grades if (number<average) { cout<<number<<setw(5)<<" Below"<<endl; } else if (number>average) { cout<<number<<setw(5)<<" Above"<<endl; } num++;//increment for number of grades? }
cout<<"The number of test scores : "<<num<<endl; cout<<"The average of the test scores : "<<average<<endl; cout<<"The highest test score was "<<Hi<<" and the lowest test grade was "<<Low<<endl; cout<<"Grade "<<setw(5)<<"Status"<<endl;
infile.close(); outfile.close(); return 0; }
So, its read from the infile ok, the average is way off, so does the status. The hi & low don't work. Even though it shows each grade from the infile, when it shows the status, it only shows the first record. Thx to all for their input.
This post has been edited by zandiago: 12 Oct, 2007 - 01:43 PM
(number/num) won't give you an average. you have to create a loop to run through each element of the array, sum them up, and then divide by num to get an average. the name variable name number is just a pointer to the first element of the array; you need to access each element using the [] operator. and you shouldn't be trying to recalculate the average every time a new value is entered, anyway, unless the grades are in some particular order for which a running average makes sense.
here's my advice. create 3 separate loops.
first loop - run through the file and input the scores into your array.
second loop - before the loop starts, the the value of Hi and Low to the first element. in the loop, run through your array and add up all of the grades to calculate the average. at the same time, test whether each number you go through is greater than Hi or less than Low; if so, change the appropriate value to the current number.
third loop - run through the array and display the grades, and whether they are greater than or less than the average grade you calculated in loop 2.
then output the number of scores, average, and high and low values.
For some reason i keep getting this error message. The strange thing is, if i open a new window and copy the same thing it works fine and then it stops. Any idea why?
CODE
1>LINK : fatal error LNK1168: cannot open C:\Users\Documents\Visual Studio 2005\Projects\vulgar\Debug\vulgar.exe for writing 1>Build log was saved at "file://c:\Users\Documents\Visual Studio 2005\Projects\vulgar\vulgar\Debug\BuildLog.htm" 1>vulgar - 1 error(s), 1 warning(s)
it's possible that your program has not yet terminated, so the linker step is failing because the program is open and in use.
and one other thing i just noticed - don't return returnVal from main(). 0 or EXIT_SUCCESS are the standard return values upon the successful completion of the program.
hi, im finaldestiny_016 im a new member here in dreamincode, regarding in your question. we are already done in that topic, just w8 for my code i will post next time.