My teacher gave us an assignment where she wanted us to write a program that would read test score data from a text file,drop the lowest test score,average the other three test scores, and output the data to a text file. I've spent three days trying to figure out how to drop the lowest test score. My teacher won't let us use void statements or arrays. The test data is just numbers with no characters involved, but i'm really tired so if anybody can please take a look at my code and tell me what's wrong so I can fix it and go to sleep that would be so coool.

# include <iostream>
# include <fstream>
# include <string>
# include <iomanip>

using namespace std;

double FindFirst(double,double,double,double,double);

int main()
{
	ifstream inFile;
	ofstream outFile;
                double test1;
	double test2;
	double test3;
	double test4;
	double first;
	
	
	inFile.open("H:\\tests.txt");
	outFile.open("H:\\outData_2.txt");

	
	inFile >> test1>>test2>>test3>>test4;

	while (!inFile.eof())
	{
		outFile<< fixed<< showpoint<<endl;
		outFile<<setprecision(1)<<endl;
		outFile<<setw(6)<<"Test 1"<<setw(6)<<"Test2                                         "<<setw(6)<<"Test3"<<setw(6)<<"Test4                                         "<<setw(6)<<"Average"<<endl;
		
		if(!inFile.eof())
		{
			first=0;
			first = FindFirst(test1,test2,test3,test4,first);				
		}
		outFile<<setw(6)<<test1<<setw(6)<<test2<<setw(6)<<test3<<setw(6)<<test4<<setw(6)<<first<<endl;
		inFile>>test1>>test2>>test3>>test4;
		if (inFile.eof())
			outFile<<"This program was created by Gainor Whitaker.End of program."<<endl;
	}
	inFile.close();
	outFile.close();

return 0;
}
double FindFirst(double test1,double test2,double test3,double test4,double first)
{
	if(test1>test2&&test3>test4)
		first = test1;
	else if(test4>test1&&test2>test3)
		first = test4;
	else if(test3>test4&&test1>test2)
		first = test3;
	else if(test2>test3&&test4>test1)
		first = test2;
	return first;
}

the following is the input data:
75 85 95 65
89 90 79 87
70 68 77 82
50 92 86 88
87.5 91.5 66.5 78.5
I am at my wits end here. I can get the program to store the data and average the scores, but finding the lowest one and dropping it is killing me. If you can help me you are the greatest person ever.

Edited 7 Years Ago by __avd: Corrected [code] tags. Encase your code in: [code] and [/code]

Comments
Excellent first post

Sorry but the main problem is that you have over complicated the if structure in FindFirst.

Several points

(i) You pass the value first BUT don't actually use it.
Let me elaborate. You use a variable first BUT you don't use the value you pass. So I put the variable IN the function and you just don't have to pass it.

(ii) You also seem to be hung up on putting ONE if / else structure. BUT the trick here (and alot of other places) is to have an if statement and then another if statement. Since I think this idea is very powerful BUT very non-obvious especially people who are still thinking in terms of translating maths I am giving a correct version of FindFirst. It is very important to notice that NONE/ ONE or SEVERAL of the if statements can get executed. This about what happens with an input 1,2,3,4 and also say 2,3,4,1 and 4,3,2,1
All of them return 4.

double 
FindFirst(double test1,double test2,double test3,double test4)
{

   // Finds the lowest value out of test1->4.
 
  double first=test1;
  if (test2<first) first=test2;
  if (test3<first) first=test3;
  if (test4<first) first=test4;
  return first;
}

Finally, have long think about how to understand if statements. The problem you seem to be displaying is you are not breaking them down into their constituent components. So please consider:

if (A>B && C>D)
   std::cout<<"This is true"<<std::endl;

Now that translates to If (A>B) AND (C>D) which implies that
if A=4, B=3 , C=1, D=0 then it is true since
A>B == true and C>D == true

Edited 7 Years Ago by StuXYZ: n/a

I left out a few things like setting the precision of the floats but other than that I think this code will work fine (I made it fast and didn't debug it very much).

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

double findLowest(double t1, double t2, double t3, double t4)//finds the lowest
{
	double lowest = t1;
	
	if( t2 < lowest )
		lowest = t2;
	if( t3 < lowest )
		lowest = t3;
	if( t4 < lowest )
		lowest = t4;
		
	return lowest;
}

int main()
{
	double test1, test2, test3, test4, lowest, average;
	
	ifstream in;
	ofstream out;
	
	in.open("input.txt");//change to your input file
	out.open("output.txt");//change to your output file
	
	for( int i = 0; i < 1; i++ )// change the 1 to however many lines you have in your "input.txt"
	{
		
		in >> test1 >> test2 >> test3 >> test4;
		
		lowest = findLowest(test1, test2, test3, test4); //finds and saves lowest
		
		average = (test1 + test2 + test3 + test4 - lowest )/3; //takes average
		
		if( test1 == lowest ) //since you dont want to use arrays this is kinda of long (to me it is)
		{
			out << "Test2 | Test3 | Test4 | Average" << endl;
			out << "  " << test2 << setw(8) << test3 << setw(8) << test4 << setw(8) << average << endl;
		}
		else if( test2 == lowest )
		{
			out << "Test1 | Test3 | Test4 | Average" << endl;
			out << "  " << test1 << setw(8) << test3 << setw(8) << test4 << setw(8) << average << endl;
		}
		else if( test3 == lowest )
		{
			out << "Test1 | Test2 | Test4 | Average" << endl;
			out << "  " << test1 << setw(8) << test2 << setw(8) << test4 << setw(8) << average << endl;
		}
		else if( test4 == lowest )
		{
			out << "Test1 | Test2 | Test3 | Average" << endl;
			out << "  " << test1 << setw(8) << test2 << setw(8) << test3 << setw(8) << average << endl;
		}
		
	out.close();
	in.close();
	
	
	system("PAUSE");
	return 0;
}

I love it how we post at the same time and our find lowest test score function is pretty much identical.

Indeed sfou, it is a method that is absolutely instinctive for most programmers. It is mainly mathematicians that have the big hang up with that sort of construct. They (and theoretical physists) have the most complex if constructs on the planet, they just seem to HATE repeated multiple action if constructs, you always get an if ( conditional) else if (conditional) else from them.

[Quick note: Nothing fundamentally wrong with the way they do if statements, just it is so distinctive ]

Second quick note: Please drop the system("pause") . It really really is evil.

Edited 7 Years Ago by StuXYZ: n/a

Comments
StuXYZ Rocks thanks for the info.
This article has been dead for over six months. Start a new discussion instead.